Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding existing CRUD action for specific model #1855

Open
chrisbloom7 opened this issue Dec 6, 2013 · 6 comments
Open

Overriding existing CRUD action for specific model #1855

chrisbloom7 opened this issue Dec 6, 2013 · 6 comments

Comments

@chrisbloom7
Copy link

I'd like to set an attribute, based on the current user, before the model is created. I've reviewed https://github.com/sferik/rails_admin/wiki/How-to-set-default-values and https://github.com/sferik/rails_admin/wiki/How-to-set-current_user-to-the-field, but I would like to do it during the #create action in the controller to prevent tampering. I saw a similar request at #756, but that was closed with the note "Closing because custom actions are already implemented." Ideally I'd just like to override the default create action to set the field and then call super to finish the processing. Is that possible, or do I need to impliment a custom action for this? And if so, how do I use that as the default create action for this particular model? Can you point me at an example?

@fmh
Copy link
Contributor

fmh commented Dec 13, 2013

+1 #1865

or maybe use an AR observer : http://api.rubyonrails.org/v3.2.15/classes/ActiveRecord/Observer.html

@fmh
Copy link
Contributor

fmh commented Dec 13, 2013

it works with AR observer , but AR Observers officially deprecated in Rails 4.0, i think it's not a good (clean) solution.

@cristianogregnanin
Copy link

i have same problem

@peresleguine
Copy link
Contributor

I have one idea on this: http://stackoverflow.com/questions/22027759/rails-admin-setting-current-user-in-controller/22229565#22229565

@simonc
Copy link
Contributor

simonc commented Sep 19, 2014

Same issue here. I use a service object in the application and calling it from the admin would avoid ugly hacks :/

@alecslupu
Copy link

If you are using the authorization layer (I am using Pundit), you can create a custom policy, that will allow you to override as much as possible.
The action is implemented here: https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/new.rb
The interest area is on line 39: https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/new.rb#L39

@authorization_adapter && @authorization_adapter.attributes_for(:create, @abstract_model).each do |name, value|
  @object.send("#{name}=", value)
end

Using the pundit, add in your policy class a method like this:

class RoomPolicy < ApplicationPolicy
  def attributes_for(action)
    case action
    when :create
      { public: true, created_by_id: user.id }
    else
      {}
    end
  end

  class Scope < ApplicationPolicy::Scope
    # def resolve
    #   super
    # end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants