Skip to content

Commit

Permalink
Merge pull request #447 from platanus/fix-enum-translation-when-blank
Browse files Browse the repository at this point in the history
fix(enum-utils): return nil if option name is blank
  • Loading branch information
difernandez authored Mar 6, 2023
2 parents beb1cd8 + 4ae482e commit c95be7d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lib/activeadmin_addons/support/enum_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def self.options_for_select(klass, enum_name, use_db_value: false)
end

def self.translate_enum_option(klass, enum_name, enum_option_name)
return if enum_option_name.blank?

klass_key = klass.model_name.i18n_key
key = "activerecord.attributes.#{klass_key}.#{enum_name.pluralize}.#{enum_option_name}"
I18n.t(key, default: enum_option_name)
Expand Down
23 changes: 19 additions & 4 deletions spec/features/tag_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,28 @@
register_index(Invoice) do
tag_column :status
end
end

create_invoice(status: :archived)
visit admin_invoices_path
context 'with value' do
before do
create_invoice(status: :archived)
visit admin_invoices_path
end

it "shows translated text as value" do
expect(page.find('td.col-status').text).to eq('Archivado')
end
end

it "shows translated text as value" do
expect(page.find('td.col-status').text).to eq('Archivado')
context 'without value' do
before do
create_invoice(status: nil)
visit admin_invoices_path
end

it "shows text for nil value" do
expect(page.find('td.col-status').text).to eq('No')
end
end
end

Expand Down
42 changes: 36 additions & 6 deletions spec/lib/support/enum_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,43 @@ def translation_key(enum_option_to_translate)
end

describe '#translate_enum_option' do
let(:enum_option_to_translate) { 'value1' }
context 'with enum option name present in enum' do
let(:enum_option_to_translate) { 'value1' }

it 'returns correct translation' do
translation = described_class.translate_enum_option(
model_class, 'some_enum', enum_option_to_translate
)
expect(translation).to eq(enum_translations[enum_option_to_translate.to_sym])
it 'returns correct translation' do
translation = described_class.translate_enum_option(
model_class, 'some_enum', enum_option_to_translate
)
expect(translation).to eq(enum_translations[enum_option_to_translate.to_sym])
end
end

context 'with enum option name not present in enum' do
let(:enum_option_to_translate) { 'unknown_value' }

before do
allow(I18n).to receive(:t).with(
translation_key(enum_option_to_translate), default: enum_option_to_translate.to_s
).and_return(enum_option_to_translate)
end

it 'returns untranslated enum option name' do
translation = described_class.translate_enum_option(
model_class, 'some_enum', enum_option_to_translate
)
expect(translation).to eq(enum_option_to_translate)
end
end

context 'with blank enum option name' do
let(:enum_option_to_translate) { nil }

it 'returns nil' do
translation = described_class.translate_enum_option(
model_class, 'some_enum', enum_option_to_translate
)
expect(translation).to eq(nil)
end
end
end
end

0 comments on commit c95be7d

Please sign in to comment.