rails_admin how to exclude actions in models - ruby-on-rails

I am using rails_admin for the first time and needed to create a new action for one of my models.
This is what I added to initializer.rails_admin.rb:
config.model Movie do
...
....
config.actions do
collection :top_five do
action_name :top_five
end
end
end
I thought this would create a new action for my Movie model, but it is present as an option (tab) for all models.
How can I make it to appear only in one model?
Thank you!
Fernanda

The only clean way I know of that currently allows this is by using CanCan for authorization, described in the RailsAdmin documentation here.
You may also override rails_admin views to show the tab only for the specific model, but that's not quite as clean.

I know this is an old question, but for everyone else landing here, here is my solution:
config.actions do
...
show_in_app
collection :top_five do
only ['Movie']
i18n_key :top_five
end
...
end
This has been added to the documentation

Related

Rails 4 Devise Multiple User Models STI

I am using Devise and Rails 4. I want to add multiple User Models(admin, usertype1, usertype2) such that they inherit from the main User Model. I have searched many posts and come to the conclusion that I may use CanCan, which I do not want, or I may use Single Table Inheritance.
The way I see it is to add a type string-column to my main User model I created with Devise. I will also need to extend each sub-class from the parent as in:
class Admin < User
end
class Usertype1 < User
end
class Usertype2 < User
end
My question is: what do I do next? How exactly do I know how to access the type column? Do I also need to override the Devise Controller for the current_user helper method such that I can have current_admin for example?
I'm not sure this really answers the original question. I am trying to set up multiple session controllers for Devise, and it seems like it really does require multiple models for this use case. Will post back when I've got a working version.
You can also use the easy_roles gem. It is available on github. This gem provides a bitmask solution for your different user roles. You just must have one model, e.g User, this model gets an attribute "role". Just checkout the docs on github.
Devise + easy_roles + CanCan is a very good setup, it is very convenient in my opinion. I use this quite often.
Link to github: https://github.com/platform45/easy_roles
STI will give you current_admin, current_user1 and current_user2 Devise methods.
In application_controller.rb, create a custom current_user method like this:
def current_user
if current_admin
current_admin
elsif current_user1
current_user1
else
current_user2
end
end
helper_method :current_user
You will need some work on routes.rb and a custom sessions_controller.rb. See the accepted answer here Rails: Using Devise with single table inheritance

How to hide Add new option in Rails Admin

I am customizing Rails Admin : https://github.com/sferik/rails_admin , i need to disable/hide "Add new" option for some model.
Any help will save lot time for me. Thanks in advance
I use the following to achieve this on a specific model. Hopefully, this helps:
config.actions do
new do
except ['Some Model']
end
end
The answer is in the configuration documentation for actions. By default, all actions are possible, including new. To customize the possible actions, in config.actions in config/initilizers/rails_admin.rb, list all the actions you want to support, leaving out the ones you don’t want to support. For example, here is a config block that allows all of the default actions except for new:
# config/initilizers/rails_admin.rb
RailsAdmin.config do |config|
config.actions do
# root actions
dashboard
# collection actions
index
# `new` is NOT allowed
export
history_index
bulk_delete
# member actions
show
edit
delete
history_show
show_in_app
end
end
To have multiple models, you must put each model in single quotes. For example, consider the following configuration:
config.actions do
dashboard
index do
except ['Address']
end
new do
except ['Address', 'Employee', 'Setting']
end
export
show
edit do
except ['Employee']
end
end
This means that:
Addresses are not included on the navbar on the left
You cannot add a new address employee or setting with the "add new" button
There is no pencil icon in the index view for Employees for editing.
If you had a User model you could see it in the navbar, edit it, and add a new one on the index page.
You can export every model, but not bulk delete them.
Implemented it with Cancan. You can refer to above answer to do it in rails admin way.
URL : https://github.com/sferik/rails_admin/wiki/CanCan

Rails: activeadmin overriding create action

I have an activeadmin resource which has a belongs_to :user relationship.
When I create a new Instance of the model in active admin, I want to associate the currently logged in user as the user who created the instance (pretty standard stuff I'd imagine).
So... I got it working with:
controller do
def create
#item = Item.new(params[:item])
#item.user = current_curator
super
end
end
However ;) I'm just wondering how this works? I just hoped that assigning the #item variable the user and then calling super would work (and it does). I also started looking through the gem but couldn't see how it was actually working.
Any pointers would be great. I'm assuming this is something that InheritedResources gives you?
Thanks!
I ran into a similar situation where I didn't really need to completely override the create method. I really only wanted to inject properties before save, and only on create; very similar to your example. After reading through the ActiveAdmin source, I determined that I could use before_create to do what I needed:
ActiveAdmin.register Product do
before_create do |product|
product.creator = current_user
end
end
Another option:
def create
params[:item].merge!({ user_id: current_curator.id })
create!
end
You are right active admin use InheritedResources, all other tools you can see on the end of the page.
As per the AA source code this worked for me:
controller do
def call_before_create(offer)
end
end

remove action for model in rails_admin

I have a problem. In my app I am using rails_admin gem. Everything is good except one thing. For some models I want to make possible only to delete them. Is there an easy way to do this?
In your rails_admin.rb file, you can add the default actions for your models in wich you can add exceptions as shown here.
Here is an exemple :
config.actions do
dashboard # mandatory
index # mandatory
new do
except ['SomeModel']
end
export
bulk_delete
show
edit do
except ['SomeOtherModel']
end
delete
show_in_app
end
Here is the link to the rails_admin documentation about actions : https://github.com/sferik/rails_admin/wiki/Actions
try changing in the file: config/initilizers/rails_admin.rb you can comment out the actions that you don't want to allow!
You can do this using CanCan: https://github.com/sferik/rails_admin/wiki/Cancan
add this to your ability.rb file:
cannot :manage, Model # disable all actions for this model
can :destroy, Model # enable only to remove

how do i get the "current_user" in a "visible do" block in config/initializers/rails_admin.rb

I'm trying to implement something similar to this:
RailsAdmin.config do |config|
config.model Team do
list do
field :name
field :created_at
field :revenue do
visible do
current_user.roles.include?(:accounting) # metacode
end
end
end
end
end
I know in the README it says the example is theoretical, but I keep getting that current_user is not found within that block. I'm authorizing with CanCan:
RailsAdmin.authorize_with :cancan
At the top of rails_admin.rb in config/initializers
Can anyone tell me how to get current_user available within the visible block? I'd like to only show certain fields in the "List" view if a user is an admin.
Since current_user is included as a helper in the view, you can get it through the bindings hash:
visible do
bindings[:view].current_user.has_role? :admin # This works for Devise
end
I got the idea from this other question: How to set the user to current_user when creating a new model using rails_admin
More info about rails_admin bindings in its wiki: https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL
Hope it helps.
Initializers are run once on application startup, so I don't think you can get a current user from within an initializer like this.
I have not tried this myself, but you may find this useful:
https://github.com/sferik/rails_admin/issues/559
In short, define RailsAdmin::Config.current_user_method in the rails_admin initializer.

Resources