Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/enumlingo/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ def enumlingo(*attributes)
attributes.each do |attribute|
pluralized_attribute = attribute.to_s.pluralize

define_method "#{attribute}_lingo" do
define_method "#{attribute}_lingo" do |locale: I18n.locale|
value = send(attribute)
# return nil if value.nil?
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{value}")
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{value}", locale: locale)
end

define_singleton_method "#{pluralized_attribute}_lingo" do
define_singleton_method "#{pluralized_attribute}_lingo" do |locale: I18n.locale|
send(attribute.to_s.pluralize).map do |key, value|
[I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}"), key]
[I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}", locale: locale), key]
end
end

define_singleton_method "#{attribute}_lingo" do |key|
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}")
define_singleton_method "#{attribute}_lingo" do |key, locale: I18n.locale|
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}", locale: locale)
end

define_singleton_method "#{pluralized_attribute}_lingo_values" do |locale = I18n.locale|
define_singleton_method "#{pluralized_attribute}_lingo_values" do |locale: I18n.locale|
send(attribute.to_s.pluralize).map do |key, value|
[I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}", locale: locale), value]
end
Expand Down
33 changes: 13 additions & 20 deletions test/enumlingo_test.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
require "test_helper"

class EnumlingoTest < ActiveSupport::TestCase
setup do
@book = products(:book)
@other = products(:other)
end

test "it has a version number" do
assert Enumlingo::VERSION
end

test "it return English product kind of book" do
I18n.locale = "en"
book = products(:book)

assert_equal "Book", book.kind_lingo
assert_equal "Book", @book.kind_lingo(locale: :en)
end

test "it return Chinese product kind of book" do
I18n.locale = "zh-CN"
book = products(:book)

assert_equal "书籍", book.kind_lingo
assert_equal "书籍", @book.kind_lingo(locale: "zh-CN")
end

test "it return missing translation of product kind of book" do
I18n.locale = "zh-CN"
book = products(:other)
assert_equal "Translation missing: zh-CN.activerecord.attributes.product.kinds.other", book.kind_lingo
assert_equal "Translation missing: zh-CN.activerecord.attributes.product.kinds.other", @other.kind_lingo(locale: "zh-CN")
end

test "it return options of product kind" do
I18n.locale = "en"
assert_equal [["Book", "book"], ["Food", "food"], ["Medical", "medical"], ["Other", "other"]], Product.kinds_lingo
assert_equal [["Book", "book"], ["Food", "food"], ["Medical", "medical"], ["Other", "other"]], Product.kinds_lingo(locale: :en)
end

test "it return options of product kind in Chinese" do
I18n.locale = "zh-CN"
assert_equal [["书籍", "book"], ["食品", "food"], ["药品", "medical"], ["Translation missing: zh-CN.activerecord.attributes.product.kinds.other", "other"]], Product.kinds_lingo
assert_equal [["书籍", "book"], ["食品", "food"], ["药品", "medical"], ["Translation missing: zh-CN.activerecord.attributes.product.kinds.other", "other"]], Product.kinds_lingo(locale: "zh-CN")
end

test "it return text of product kind" do
I18n.locale = "en"
assert_equal "Book", Product.kind_lingo(:book)
assert_equal "Book", Product.kind_lingo(:book, locale: :en)
end

test "it return text of product kind in Chinese" do
I18n.locale = "zh-CN"
assert_equal "书籍", Product.kind_lingo(:book)
assert_equal "书籍", Product.kind_lingo(:book, locale: "zh-CN")
end

test "return options of product kind value with custom translation" do
assert_equal [["Book", 0], ["Food", 1], ["Medical", 2], ["Other", 3]], Product.kinds_lingo_values(:en)
assert_equal [["Book", 0], ["Food", 1], ["Medical", 2], ["Other", 3]], Product.kinds_lingo_values(locale: :en)
end
end