Hellos

Working on a project I came across the scenario that certain attributes 
should only be accessible if the model instance is in a certain state. 
Here's a completely fake example:

class Car < ActiveRecord::Base
  attr_accessible :name, :color

  private

  def mass_assignment_authorizer(role)
    accessible = super
    accessible += [:location] if state == :parked
    accessible += [:driver, :passengers] if state == :driving
    accessible
  end
end

Similar to the "if" option of "validates", a nicer way to do something like 
this would be:

class Car < ActiveRecord::Base
  attr_accessible :name, :color
  attr_accessible :driver, :passenger, if: -> {|car| car.state == :driving }
end

Or:

class Car < ActiveRecord::Base
  attr_accessible :name, :color
  attr_accessible :driver, :passenger, if: 'driving?'

  def driving?
    state == :driving
  end
end

Would such a feature make sense - or is there maybe an existing mechanism 
to achieve this without abusing "mass_assignment_authorizer"?

Cheers!

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-core/-/JHzcnUEnKwEJ.
To post to this group, send email to rubyonrails-core@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-core+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to