Skip to content

Commit

Permalink
Switch update_attributes for update (globalize#749)
Browse files Browse the repository at this point in the history
This handles a deprecation warning from Rails. Only affects test files.
  • Loading branch information
parndt authored Feb 12, 2020
1 parent 6207a3b commit 8af95a1
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions test/globalize/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AttributesTest < MiniTest::Spec

it 'returns the correct translation for a saved record after locale switching' do
post = Post.create(:title => 'title', published: false)
post.update_attributes(:title => 'Titel', :locale => :de, published: true)
post.update(:title => 'Titel', :locale => :de, published: true)
post.reload

assert_translated post, :en, :title, 'title'
Expand Down Expand Up @@ -227,7 +227,7 @@ class AttributesTest < MiniTest::Spec
describe '#<attr>_before_type_cast' do
it 'works for translated attributes' do
post = Post.create(:title => 'title')
post.update_attributes(:title => "Titel", :locale => :de)
post.update(:title => "Titel", :locale => :de)

with_locale(:en) { assert_equal 'title', post.title_before_type_cast }
with_locale(:de) { assert_equal 'Titel', post.title_before_type_cast }
Expand Down
4 changes: 2 additions & 2 deletions test/globalize/destroy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class DestroyTest < MiniTest::Spec
before do
@posts = [Post.create(:title => 'title'), Post.create(:title => 'title')]
Globalize.with_locale(:ja) do
@posts[0].update_attributes(:title => 'タイトル1')
@posts[1].update_attributes(:title => 'タイトル2')
@posts[0].update(:title => 'タイトル1')
@posts[1].update(:title => 'タイトル2')
end
end

Expand Down
20 changes: 10 additions & 10 deletions test/globalize/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class FallbacksTest < MiniTest::Spec
assert_equal 'foo', post.title

I18n.locale = :'en-US'
post.update_attributes(:title => 'bar')
post.update(:title => 'bar')

I18n.locale = :'de-DE'
assert_equal 'bar', post.title
Expand All @@ -89,7 +89,7 @@ class FallbacksTest < MiniTest::Spec
I18n.locale = :'de-DE'
assert_equal 'foo', post.title

post.update_attributes :title => nil
post.update :title => nil
assert_equal 'foo', post.title
end

Expand Down Expand Up @@ -165,7 +165,7 @@ class FallbacksTest < MiniTest::Spec
I18n.locale = :'de-DE'
assert_equal 'foo', task.name

task.update_attributes :name => ''
task.update :name => ''
assert_equal 'foo', task.name
end

Expand All @@ -176,7 +176,7 @@ class FallbacksTest < MiniTest::Spec
params = { title: '' }

I18n.locale = 'de-DE'
question.update_attributes(params)
question.update(params)

assert_equal question.errors.first, [:title, "can't be blank"]
end
Expand All @@ -193,7 +193,7 @@ class FallbacksTest < MiniTest::Spec
assert_equal 'foo', child.content

I18n.locale = :'en-US'
child.update_attributes(:content => 'bar')
child.update(:content => 'bar')

I18n.locale = :'de-DE'
assert_equal 'bar', child.content
Expand Down Expand Up @@ -237,24 +237,24 @@ class FallbacksTest < MiniTest::Spec
assert_equal translations, user.name_translations
end
end

describe 'query with fallbacks' do
it 'does not result in duplicated records' do
I18n.fallbacks.clear
I18n.fallbacks.map :en => [ :de, :fr ]
I18n.fallbacks.map :fr => [ :en ]
I18n.locale = :en

product = Product.create(:name => 'foooooooo')
with_locale(:de) { product.name = 'bar' }
product.save!

assert_equal 1, Product.with_translations.where(id: product.id).length
assert_equal 'foooooooo', Product.find(product.id).name

I18n.locale = :de
assert_equal 'bar', Product.find(product.id).name

I18n.locale = :fr
assert_equal 'foooooooo', Product.find(product.id).name
end
Expand Down
4 changes: 2 additions & 2 deletions test/globalize/locale_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require File.expand_path('../../test_helper', __FILE__)

class LocaleTest < MiniTest::Spec

describe Globalize do
it 'has locale accessors' do
assert Globalize.respond_to?(:locale)
Expand Down Expand Up @@ -96,7 +96,7 @@ class LocaleTest < MiniTest::Spec
assert_translated Post.first, :en, :title, 'title'

Globalize.locale = :de
post.update_attributes(:title => 'Titel')
post.update(:title => 'Titel')

assert_translated Post.first, :en, :title, 'title'
assert_translated Post.first, :de, :title, 'Titel'
Expand Down
2 changes: 1 addition & 1 deletion test/globalize/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class OrderTest < MiniTest::Spec
end

it "should not return products with ID from translations table" do
with_locale(:fr) { @first_product.update_attributes(name: 'premier') }
with_locale(:fr) { @first_product.update(name: 'premier') }
premier_product = with_locale(:fr) { Product.order(name: :asc).first }
assert_equal @first_product.name, premier_product.name, 'products should be the same record as shown by their default name'
assert_equal @first_product, premier_product, 'returned product records should have the same ID from the products table'
Expand Down
8 changes: 4 additions & 4 deletions test/globalize/set_translations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AttributesTest < MiniTest::Spec
describe '#set_translations' do
it 'sets multiple translations at once' do
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
post.update(:title => 'Titel', :content => 'Inhalt', :locale => :de)

post.set_translations(
:en => { :title => 'updated title', :content => 'updated content' },
Expand All @@ -21,7 +21,7 @@ class AttributesTest < MiniTest::Spec

it 'does not touch existing translations for other locales' do
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
post.update(:title => 'Titel', :content => 'Inhalt', :locale => :de)

post.set_translations(:en => { :title => 'updated title', :content => 'updated content' })
post.reload
Expand All @@ -48,7 +48,7 @@ class AttributesTest < MiniTest::Spec

it 'does not touch existing translations for other attributes' do
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
post.update(:title => 'Titel', :content => 'Inhalt', :locale => :de)

post.set_translations(
:en => { :title => "updated title" },
Expand All @@ -62,7 +62,7 @@ class AttributesTest < MiniTest::Spec

it 'raises ::NoMethodError on unknown attributes' do
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
post.update(:title => 'Titel', :content => 'Inhalt', :locale => :de)

assert_raises(NoMethodError, 'unknown attribute: does_not_exist') do
post.set_translations(:de => { :does_not_exist => 'should raise' })
Expand Down
16 changes: 8 additions & 8 deletions test/globalize/translated_attributes_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ def self.it_supports_translated_columns(method)
Post.create(:title => 'title'),
Post.create(:title => 'title') ]
Globalize.with_locale(:ja) do
@posts[0].update_attributes(:title => 'タイトル1')
@posts[1].update_attributes(:title => 'タイトル2')
@posts[2].update_attributes(:title => 'タイトル3')
@posts[0].update(:title => 'タイトル1')
@posts[1].update(:title => 'タイトル2')
@posts[2].update(:title => 'タイトル3')
end
end

Expand Down Expand Up @@ -401,9 +401,9 @@ def self.it_supports_translated_columns(method)
Post.create(:id => 2, :title => 'title2'),
Post.create(:id => 3, :title => 'title3') ]
Globalize.with_locale(:ja) do
@posts[0].update_attributes(:title => 'タイトル1')
@posts[1].update_attributes(:title => 'タイトル2')
@posts[2].update_attributes(:title => 'タイトル3')
@posts[0].update(:title => 'タイトル1')
@posts[1].update(:title => 'タイトル2')
@posts[2].update(:title => 'タイトル3')
end
end

Expand Down Expand Up @@ -478,8 +478,8 @@ def self.it_supports_translated_columns(method)

it 'returns translations in fallback locales' do
post = Post.create(:title => 'a title')
Globalize.with_locale(:ja) { post.update_attributes :title => 'タイトル' }
Globalize.with_locale(:fr) { post.update_attributes :title => 'titre' }
Globalize.with_locale(:ja) { post.update :title => 'タイトル' }
Globalize.with_locale(:fr) { post.update :title => 'titre' }

# where
assert_equal post, Post.where(:title => 'タイトル').first
Expand Down
4 changes: 2 additions & 2 deletions test/globalize/translation_for_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TranslationForTest < MiniTest::Spec
describe '#translation_for' do
it 'returns translation for locale passed in as an argument' do
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
post.update(:title => 'Titel', :content => 'Inhalt', :locale => :de)

assert_equal Post::Translation, post.translation_for(:en).class
assert_equal 'title', post.translation_for(:en).title
Expand All @@ -19,7 +19,7 @@ class TranslationForTest < MiniTest::Spec
describe '#translation' do
it 'returns translation for current locale' do
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
post.update(:title => 'Titel', :content => 'Inhalt', :locale => :de)

assert_equal Post::Translation, post.translation.class
assert_equal Globalize.locale, post.translation.locale
Expand Down
16 changes: 8 additions & 8 deletions test/globalize/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ class ValidationsTest < MiniTest::Spec
# assert Post.create(:title => 'foo', :locale => :de).valid?
# end

describe '#update_attributes' do
describe '#update' do
it 'succeeds with valid values' do
post = Post.create(:title => 'foo')
post.update_attributes(:title => 'baz')
post.update(:title => 'baz')
assert post.valid?
assert_equal 'baz', Post.first.title
end

it 'fails with invalid values' do
post = Post.create(:title => 'foo')
assert !post.update_attributes(:title => '')
assert !post.update(:title => '')
assert !post.valid?
assert !post.reload.attributes['title'].nil?
assert_equal 'foo', post.title
Expand Down Expand Up @@ -109,18 +109,18 @@ class ValidationsTest < MiniTest::Spec

# update
Validatee.create!(:string => 'b')
assert validatee.update_attributes(:string => 'a')
assert !validatee.update_attributes(:string => 'b')
Globalize.with_locale(:de) { assert validatee.update_attributes(:string => 'b') }
assert validatee.update(:string => 'a')
assert !validatee.update(:string => 'b')
Globalize.with_locale(:de) { assert validatee.update(:string => 'b') }
end

it 'validates uniqueness correctly with nested model' do
# nested model (to check for this: https://github.com/resolve/refinerycms/pull/1486 )
Nested::NestedValidatee.class_eval { validates_uniqueness_of :string }
nested_validatee = Nested::NestedValidatee.create!(:string => 'a')
Nested::NestedValidatee.create!(:string => 'b')
assert nested_validatee.update_attributes(:string => 'a')
assert !nested_validatee.update_attributes(:string => 'b')
assert nested_validatee.update(:string => 'a')
assert !nested_validatee.update(:string => 'b')
end

it 'accepts :scope option' do
Expand Down
20 changes: 10 additions & 10 deletions test/globalize_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ class GlobalizeTest < MiniTest::Spec
end
end

describe '#update_attributes' do
describe '#update' do
it "saves translations record for locale passed in" do
post = Post.create(:title => 'Titel', :locale => :de)
post.update_attributes(:title => 'title', :locale => :en)
post.update(:title => 'title', :locale => :en)

assert_equal 2, post.translations.size
assert_translated post, :de, :title, 'Titel'
Expand All @@ -99,7 +99,7 @@ class GlobalizeTest < MiniTest::Spec

it "saves translations record for current I18n locale if none is passed in" do
post = with_locale(:de) { Post.create(:title => 'Titel') }
with_locale(:en) { post.update_attributes(:title => 'title') }
with_locale(:en) { post.update(:title => 'title') }

assert_equal 2, post.translations.size
assert_translated post, :en, :title, 'title'
Expand All @@ -108,7 +108,7 @@ class GlobalizeTest < MiniTest::Spec

it "does not add tralations if no words changed" do
product = with_locale(:en) { Product.create(:name => 'name') }
with_locale(:de) { product.update_attributes(:updated_at => Time.now) }
with_locale(:de) { product.update(:updated_at => Time.now) }

assert_equal 1, product.reload.translations.size
end
Expand All @@ -117,7 +117,7 @@ class GlobalizeTest < MiniTest::Spec
describe '#write_attribute' do
it "saves translations record for locale passed in" do
post = Post.create(:title => 'title', :locale => :de)
post.update_attributes(:title => 'title', :locale => :en)
post.update(:title => 'title', :locale => :en)

post.reload

Expand All @@ -139,7 +139,7 @@ class GlobalizeTest < MiniTest::Spec
post = Post.create(:title => 'foo')
post.title # make sure its fetched from the DB

Post.find_by_id(post.id).update_attributes! :title => 'bar'
Post.find_by_id(post.id).update! :title => 'bar'

post.reload

Expand All @@ -155,7 +155,7 @@ class GlobalizeTest < MiniTest::Spec
describe '#destroy' do
it "destroy destroys dependent translations" do
post = Post.create(:title => "title")
post.update_attributes(:title => 'Titel', :locale => :de)
post.update(:title => 'Titel', :locale => :de)
assert_equal 2, PostTranslation.count
post.destroy
assert_equal 0, PostTranslation.count
Expand All @@ -182,10 +182,10 @@ class GlobalizeTest < MiniTest::Spec
describe '#translated_locales' do
it "returns locales that have translations" do
first = Post.create!(:title => 'title', :locale => :en)
first.update_attributes(:title => 'Title', :locale => :de)
first.update(:title => 'Title', :locale => :de)

second = Post.create!(:title => 'title', :locale => :en)
second.update_attributes(:title => 'titre', :locale => :fr)
second.update(:title => 'titre', :locale => :fr)

assert_equal [:de, :en, :fr], Post.translated_locales
assert_equal [:de, :en], first.translated_locales
Expand Down Expand Up @@ -220,7 +220,7 @@ class GlobalizeTest < MiniTest::Spec
post = Post.create(:title => 'title')
translated_comment = TranslatedComment.create(:post => post, :content => 'content')

assert translated_comment.update_attributes(:content => 'Inhalt', :locale => :de)
assert translated_comment.update(:content => 'Inhalt', :locale => :de)
assert_translated translated_comment, :en, :content, 'content'
assert_translated translated_comment, :de, :content, 'Inhalt'
end
Expand Down

0 comments on commit 8af95a1

Please sign in to comment.