1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #38261 from vipulnsward/wrapper-support-alias-attrs

Adds support to wrap aliased attributed in object hash in params wrapper
This commit is contained in:
Eugene Kenny 2020-11-01 12:42:37 +00:00 committed by GitHub
commit de0f59f1ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View file

@ -107,10 +107,14 @@ module ActionController
unless super || exclude
if m.respond_to?(:attribute_names) && m.attribute_names.any?
self.include = m.attribute_names
if m.respond_to?(:stored_attributes) && !m.stored_attributes.empty?
self.include = m.attribute_names + m.stored_attributes.values.flatten.map(&:to_s)
else
self.include = m.attribute_names
self.include += m.stored_attributes.values.flatten.map(&:to_s)
end
if m.respond_to?(:attribute_aliases) && m.attribute_aliases.any?
self.include += m.attribute_aliases.keys
end
if m.respond_to?(:nested_attributes_options) && m.nested_attributes_options.keys.any?

View file

@ -293,6 +293,10 @@ class NamespacedParamsWrapperTest < ActionController::TestCase
def self.attribute_names
["username"]
end
def self.attribute_aliases
{ "nick" => "username" }
end
end
class SampleTwo
@ -328,6 +332,19 @@ class NamespacedParamsWrapperTest < ActionController::TestCase
end
end
def test_namespace_lookup_from_model_alias
Admin.const_set(:User, Class.new(SampleOne))
begin
with_default_wrapper_options do
@request.env["CONTENT_TYPE"] = "application/json"
post :parse, params: { "nick" => "sikachu", "title" => "Developer" }
assert_parameters({ "nick" => "sikachu", "title" => "Developer", "user" => { "nick" => "sikachu" } })
end
ensure
Admin.send :remove_const, :User
end
end
def test_hierarchy_namespace_lookup_from_model
Object.const_set(:User, Class.new(SampleTwo))
begin