Skip to content

Commit

Permalink
Merge pull request #3 from jtomaszewski/fix/dont-create-default-trans…
Browse files Browse the repository at this point in the history
…lation-on-save

Don't create translation record for default_locale on save (globalize#328)
  • Loading branch information
Ceda committed Aug 2, 2018
2 parents a5ba772 + a3876cf commit 51abfca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/globalize/active_record/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def globalize_fallbacks(locale)
end

def save(*)
result = Globalize.with_locale(translation.locale || I18n.default_locale) do
result = Globalize.with_locale(Globalize.locale || I18n.default_locale) do
without_fallbacks do
super
end
Expand Down
30 changes: 30 additions & 0 deletions test/globalize/translated_attributes_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,34 @@ class TranslatedAttributesQueryTest < MiniTest::Spec
assert_equal 1, Comment.count
end
end

describe "without a record for default locale" do
before do
@artwork = Artwork.new
end

it "doesn't create translation for default locale on save" do
@artwork.translations.build(locale: :de, title: "Titel")
@artwork.save

assert_equal 1, @artwork.translations.count
assert_equal :de, @artwork.translations.first.locale
end

it "doesn't create any translation on save" do
@artwork.save

assert_equal 0, @artwork.translations.count
end

it "creates a translation for default locale after the assignment of translated attributes" do
@artwork.save
@artwork.title = "Title"
@artwork.save

assert_equal 1, @artwork.translations.count
assert_equal :en, @artwork.translations.first.locale
assert_equal "Title", @artwork.title
end
end
end

0 comments on commit 51abfca

Please sign in to comment.