ruby - Is there a better way than scoping to apply user-specific changes to models in Rails 3.2? -
in application i'm attempting update rails 3.2 working fine in rails 3.0, there scoping rules use with_scope
apply user-specific manipulations of model depending on user's permissions.
trivial model:
class item < activerecord::base attr_accessible :value end
testing scope behaviour:
require 'test_helper' class itemtest < activesupport::testcase test "scoping create" item.with_scope({ :create => { :value => 42 }}) item = item.create! assert_equal 42, item.value end end test "scoping create when caller tries override" item.with_scope({ :create => { :value => 42 }}) item = item.create!(:value => 37) assert_equal 42, item.value end end test "scoping update" item.with_scope({ :create => { :value => 42 }}) item = item.create! item.update_attributes!(:value => 37) assert_equal 42, item.value end end end
under rails 3.2, "scoping create" passes , other 2 fail. seems in rails 3.2 (possibly in 3.1 well, i'm not sure), :create
helps initialise model on construction, whereas in past versions, overwrite other values had set @ save time.
in real application, decision of scope use depends on user's permissions.
i realise clean map before passing through controller, seems result in putting business logic controller.
so, there perhaps proper way deal sort of current-user-dependant business logic?
Comments
Post a Comment