From e36913a87f94129b81127e6d82dca182973f0578 Mon Sep 17 00:00:00 2001 From: Chris Salzberg Date: Fri, 7 Oct 2016 08:01:11 +0900 Subject: [PATCH] Fix assign attributes so it behaves like original --- lib/globalize/active_record/instance_methods.rb | 16 ++++++++-------- test/globalize/attributes_test.rb | 14 ++++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/globalize/active_record/instance_methods.rb b/lib/globalize/active_record/instance_methods.rb index d1c16e57..128815e3 100644 --- a/lib/globalize/active_record/instance_methods.rb +++ b/lib/globalize/active_record/instance_methods.rb @@ -12,14 +12,15 @@ def attributes end def attributes=(new_attributes, *options) - return unless new_attributes.is_a?(Hash) - assign_attributes(new_attributes, *options) + super unless new_attributes.respond_to?(:stringify_keys) && new_attributes.present? + attributes = new_attributes.stringify_keys + with_given_locale(attributes) { super(attributes.except("locale"), *options) } end def assign_attributes(new_attributes, *options) - return unless new_attributes - attributes = new_attributes.symbolize_keys - with_given_locale(attributes) { super(attributes.except(:locale), *options) } + super unless new_attributes.respond_to?(:stringify_keys) && new_attributes.present? + attributes = new_attributes.stringify_keys + with_given_locale(attributes) { super(attributes.except("locale"), *options) } end def write_attribute(name, value, options = {}) @@ -200,9 +201,9 @@ def save_translations! end def with_given_locale(_attributes, &block) - attributes = _attributes.symbolize_keys + attributes = _attributes.stringify_keys - if locale = attributes.try(:delete, :locale) + if locale = attributes.try(:delete, "locale") Globalize.with_locale(locale, &block) else yield @@ -216,7 +217,6 @@ def without_fallbacks ensure self.fallbacks_for_empty_translations = before end - end end end diff --git a/test/globalize/attributes_test.rb b/test/globalize/attributes_test.rb index 4cd7e3fd..9688dbf4 100644 --- a/test/globalize/attributes_test.rb +++ b/test/globalize/attributes_test.rb @@ -164,12 +164,10 @@ class AttributesTest < MiniTest::Spec assert_equal post.title, 'newtitle' end - it 'does nothing if attributes is not a hash' do + it 'raises ArgumentError if attributes is blank' do post = Post.create(:title => 'title') - post.attributes = nil - assert_equal 'title', post.title - post.attributes = [] - assert_equal 'title', post.title + assert_raises(ArgumentError) { post.attributes = nil } + assert_raises(ArgumentError) { post.attributes = [] } end it 'does not modify arguments passed in' do @@ -193,10 +191,10 @@ class AttributesTest < MiniTest::Spec assert_equal post.title, 'newtitle' end - it 'does nothing if attributes is nil' do + it 'raises ArgumentError if attributes is blank' do post = Post.create(:title => 'title') - post.assign_attributes(nil) - assert_equal 'title', post.title + assert_raises(ArgumentError) { post.assign_attributes(nil) } + assert_raises(ArgumentError) { post.assign_attributes([]) } end it 'does not modify arguments passed in' do