It seems to me something that can be set directly from your controller code in the class for this request. For example.
Employee.accessible = :all Company.create(params[:company]) Employee.accessible = nil
What can be extracted into a block like
def with_accessible(*types) types.flatten! types.each{|type| type.accessible = :all} yield types.each{|type| type.accessible = nil} end
So your final controller code
with_accessible(Employee, OtherClass, YetAnotherClass) do Company.create(params[:company]) end
Pretty expressively what happens for all attributes
In the case of only certain attributes, I can change it to the following
def with_accessible(*types, &block) types.flatten! return with_accessible_hash(types.first, &block) if types.first.is_a?(Hash) types.each{|type| type.accessible = :all} ret = yield types.each{|type| type.accessible = nil} ret end def with_accessible_hash(hash, &block) hash.each_pair do |klass, accessible| Object.const_get(klass).accessible = accessible end ret = yield hash.keys.each{|type| type.accessible = nil} ret end
What gives you
with_accessible(:Employee => [:a, :b, :c], :OtherClass => [:a, :b]) do Company.create(params[:company]) end
dlangevin
source share