Merge pull request #4445 from nragaz/role_based_params_wrapping
specify a role for identifying accessible attributes when wrapping params
This commit is contained in:
parent
2919fd8980
commit
7a093606f6
@ -166,8 +166,9 @@ def _set_wrapper_defaults(options, model=nil)
|
|||||||
|
|
||||||
unless options[:include] || options[:exclude]
|
unless options[:include] || options[:exclude]
|
||||||
model ||= _default_wrap_model
|
model ||= _default_wrap_model
|
||||||
if model.respond_to?(:accessible_attributes) && model.accessible_attributes.present?
|
role = options.has_key?(:as) ? options[:as] : :default
|
||||||
options[:include] = model.accessible_attributes.to_a
|
if model.respond_to?(:accessible_attributes) && model.accessible_attributes(role).present?
|
||||||
|
options[:include] = model.accessible_attributes(role).to_a
|
||||||
elsif model.respond_to?(:attribute_names) && model.attribute_names.present?
|
elsif model.respond_to?(:attribute_names) && model.attribute_names.present?
|
||||||
options[:include] = model.attribute_names
|
options[:include] = model.attribute_names
|
||||||
end
|
end
|
||||||
|
@ -174,7 +174,7 @@ def test_derived_wrapped_keys_from_specified_model
|
|||||||
|
|
||||||
def test_accessible_wrapped_keys_from_matching_model
|
def test_accessible_wrapped_keys_from_matching_model
|
||||||
User.expects(:respond_to?).with(:accessible_attributes).returns(true)
|
User.expects(:respond_to?).with(:accessible_attributes).returns(true)
|
||||||
User.expects(:accessible_attributes).twice.returns(["username"])
|
User.expects(:accessible_attributes).with(:default).twice.returns(["username"])
|
||||||
|
|
||||||
with_default_wrapper_options do
|
with_default_wrapper_options do
|
||||||
@request.env['CONTENT_TYPE'] = 'application/json'
|
@request.env['CONTENT_TYPE'] = 'application/json'
|
||||||
@ -186,7 +186,7 @@ def test_accessible_wrapped_keys_from_matching_model
|
|||||||
def test_accessible_wrapped_keys_from_specified_model
|
def test_accessible_wrapped_keys_from_specified_model
|
||||||
with_default_wrapper_options do
|
with_default_wrapper_options do
|
||||||
Person.expects(:respond_to?).with(:accessible_attributes).returns(true)
|
Person.expects(:respond_to?).with(:accessible_attributes).returns(true)
|
||||||
Person.expects(:accessible_attributes).twice.returns(["username"])
|
Person.expects(:accessible_attributes).with(:default).twice.returns(["username"])
|
||||||
|
|
||||||
UsersController.wrap_parameters Person
|
UsersController.wrap_parameters Person
|
||||||
|
|
||||||
@ -195,6 +195,19 @@ def test_accessible_wrapped_keys_from_specified_model
|
|||||||
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
|
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_accessible_wrapped_keys_with_role_from_specified_model
|
||||||
|
with_default_wrapper_options do
|
||||||
|
Person.expects(:respond_to?).with(:accessible_attributes).returns(true)
|
||||||
|
Person.expects(:accessible_attributes).with(:admin).twice.returns(["username"])
|
||||||
|
|
||||||
|
UsersController.wrap_parameters Person, :as => :admin
|
||||||
|
|
||||||
|
@request.env['CONTENT_TYPE'] = 'application/json'
|
||||||
|
post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
|
||||||
|
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_not_wrapping_abstract_model
|
def test_not_wrapping_abstract_model
|
||||||
User.expects(:respond_to?).with(:accessible_attributes).returns(false)
|
User.expects(:respond_to?).with(:accessible_attributes).returns(false)
|
||||||
|
Loading…
Reference in New Issue
Block a user