From c223bdae45591fa77ba9b2e752d8de3a5e82fc24 Mon Sep 17 00:00:00 2001 From: Rodrigo Ulisses e Silva Date: Thu, 31 Aug 2017 13:57:28 -0300 Subject: [PATCH] Fix association_parser ActionController::Parameters does not implement method map. --- lib/i18n_alchemy/association_parser.rb | 2 +- .../proxy/attributes_parsing_test.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/i18n_alchemy/association_parser.rb b/lib/i18n_alchemy/association_parser.rb index e649073..d66de09 100644 --- a/lib/i18n_alchemy/association_parser.rb +++ b/lib/i18n_alchemy/association_parser.rb @@ -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 attributes.map { |value_attributes| proxy.send(:parse_attributes, value_attributes) } else proxy.send(:parse_attributes, attributes) diff --git a/test/i18n_alchemy/proxy/attributes_parsing_test.rb b/test/i18n_alchemy/proxy/attributes_parsing_test.rb index b23c0e2..e3e2fdf 100644 --- a/test/i18n_alchemy/proxy/attributes_parsing_test.rb +++ b/test/i18n_alchemy/proxy/attributes_parsing_test.rb @@ -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 @@ -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