-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set proxy getter and setters for localized translations
- Loading branch information
Showing
9 changed files
with
103 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Rosetta | ||
class Translated::Create | ||
attr_reader :value | ||
|
||
def initialize(record, locale, value) | ||
@record = record | ||
@locale = locale | ||
@value = value | ||
end | ||
|
||
def save | ||
translation.value = @value | ||
@record.public_send(:"#{@locale.code}_translation=", translation) | ||
end | ||
|
||
def translation | ||
@translation ||= @record.public_send(:"build_#{@locale.code}_translation") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Rosetta | ||
class Translated::Delete | ||
attr_reader :value | ||
|
||
def initialize(record, locale) | ||
@record = record | ||
@locale = locale | ||
@value = nil | ||
end | ||
|
||
def save | ||
@record.public_send(:"#{@locale.code}_translation=", nil) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require "test_helper" | ||
|
||
class Rosetta::TranslatedTest < ActiveSupport::TestCase | ||
include ActiveJob::TestHelper | ||
|
||
test "setting a new translation" do | ||
translation_key = rosetta_translation_keys(:goodbye) | ||
translation_key.update(value_fr: "au revoir") | ||
translation_key.reload | ||
|
||
assert_equal "au revoir", translation_key.value_fr | ||
assert_not_nil translation_key.fr_translation | ||
end | ||
|
||
test "setting a translation to blank removes the translation" do | ||
translation_key = rosetta_translation_keys(:hello) | ||
|
||
translation_key.update(value_fr: "") | ||
translation_key.reload | ||
|
||
assert_nil translation_key.value_fr | ||
assert_nil translation_key.fr_translation | ||
end | ||
|
||
test "setting a translation without saving returns the updated translation" do | ||
translation_key = rosetta_translation_keys(:hello) | ||
translation_key.value_fr = "salut" | ||
|
||
assert_equal "salut", translation_key.value_fr | ||
assert_equal "bonjour", translation_key.reload.value_fr | ||
end | ||
end |