Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix association_parser #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/i18n_alchemy/association_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(target_class, association_name)
#
def parse(attributes)
if association.macro == :has_many
attributes = attributes.is_a?(Hash) ? attributes.values : attributes
attributes = attributes.respond_to?(:to_hash) ? attributes.to_hash.values : attributes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: won't to_hash raise a deprecation warning in 5.0, and an exception in 5.1, when unpermitted? I guess we might need to add some tests with AC::Parameters to be sure.

attributes.map { |value_attributes| proxy.send(:parse_attributes, value_attributes) }
else
proxy.send(:parse_attributes, attributes)
Expand Down
19 changes: 19 additions & 0 deletions test/i18n_alchemy/proxy/attributes_parsing_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
require "test_helper"

class Something
ATTRIBUTES = { :products_attributes => {"0" => {:price => '100,90', "_destroy"=>"false"} } }

def to_hash
ATTRIBUTES
end

def stringify_keys
ATTRIBUTES.stringify_keys
end
end

class ProxyAttributesParsingTest < I18n::Alchemy::ProxyTestCase
# Attributes
def test_initializes_proxy_with_attributes
Expand Down Expand Up @@ -92,6 +104,13 @@ def test_should_assign_for_nested_attributes_for_one_to_one_association
assert_equal '100,87', account.localized.total_money
end

def test_should_assign_for_nested_attributes_for_one_to_one_association_with_any_object
supplier_localized = Supplier.new.localized(Something.new)
product = supplier_localized.products.first

assert_equal '100,90', product.localized.price
end

def test_update_attributes_for_nested_attributes
@supplier_localized.update_attributes(:account_attributes => {:total_money => '99,87'})
assert_equal '99,87', @supplier_localized.account.localized.total_money
Expand Down