-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #442 from platanus/enum-translations
Enum translations
- Loading branch information
Showing
14 changed files
with
299 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
16 |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
--format=doc | ||
--require spec_helper | ||
--color |
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,17 @@ | ||
module ActiveAdminAddons | ||
class EnumUtils | ||
def self.options_for_select(klass, enum_name, use_db_value: false) | ||
enum_options_hash = klass.defined_enums[enum_name] | ||
enum_options_hash.map do |enum_option_name, db_value| | ||
value = use_db_value ? db_value : enum_option_name | ||
[translate_enum_option(klass, enum_name, enum_option_name), value] | ||
end | ||
end | ||
|
||
def self.translate_enum_option(klass, enum_name, enum_option_name) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
difernandez
Author
Contributor
|
||
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) | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
lib/activeadmin_addons/support/select_filter_input_extension.rb
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,19 @@ | ||
module ActiveAdminAddons | ||
module SelectFilterInputExtension | ||
def collection_from_enum? | ||
klass.respond_to?(:defined_enums) && klass.defined_enums.has_key?(method.to_s) | ||
end | ||
|
||
def collection | ||
if !options[:collection] && collection_from_enum? | ||
EnumUtils.options_for_select(klass, method.to_s, use_db_value: true) | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
|
||
::ActiveAdmin::Inputs::Filters::SelectInput.send( | ||
:prepend, ActiveAdminAddons::SelectFilterInputExtension | ||
) |
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,80 @@ | ||
require "rails_helper" | ||
|
||
describe "Select Filter Input", type: :feature do | ||
context 'when used with an enum column' do | ||
let!(:active_invoice) { Invoice.create!(status: :active) } | ||
let!(:archived_invoice) { Invoice.create!(status: :archived) } | ||
let(:option_selector) { '.filter_form select[name="q[status_eq]"] option' } | ||
|
||
context "with collection option" do | ||
before do | ||
register_page(Invoice) do | ||
filter :status, as: :select, collection: [ | ||
['Activo, si se considera', 0], | ||
['Archivado, ya no se considera', 1], | ||
['Estado que no existe', 42] | ||
] | ||
end | ||
|
||
visit admin_invoices_path | ||
end | ||
|
||
it "uses collection directly" do | ||
expect(page.find("#{option_selector}[value='0']").text).to eq('Activo, si se considera') | ||
expect(page.find("#{option_selector}[value='1']").text).to( | ||
eq('Archivado, ya no se considera') | ||
) | ||
expect(page.find("#{option_selector}[value='42']").text).to eq('Estado que no existe') | ||
end | ||
|
||
it 'displays all records' do | ||
expect(page).to have_css('.col-id', text: /#{active_invoice.id}/) | ||
expect(page).to have_css('.col-id', text: /#{archived_invoice.id}/) | ||
end | ||
|
||
context 'when selecting a value', js: true do | ||
before do | ||
pick_select2_entered_option('Activo, si se considera') | ||
click_filter_btn | ||
end | ||
|
||
it 'displays only elements with that value' do | ||
expect(page).to have_css('.col-id', text: /#{active_invoice.id}/) | ||
expect(page).not_to have_css('.col-id', text: /#{archived_invoice.id}/) | ||
end | ||
end | ||
end | ||
|
||
context "without collection option" do | ||
before do | ||
register_page(Invoice) do | ||
filter :status, as: :select | ||
end | ||
|
||
visit admin_invoices_path | ||
end | ||
|
||
it "renders options with enum db values and translated labels" do | ||
expect(page.find("#{option_selector}[value='0']").text).to eq('Activo') | ||
expect(page.find("#{option_selector}[value='1']").text).to eq('Archivado') | ||
end | ||
|
||
it 'displays all records' do | ||
expect(page).to have_css('.col-id', text: /#{active_invoice.id}/) | ||
expect(page).to have_css('.col-id', text: /#{archived_invoice.id}/) | ||
end | ||
|
||
context 'when selecting a value', js: true do | ||
before do | ||
pick_select2_entered_option('Archivado') | ||
click_filter_btn | ||
end | ||
|
||
it 'displays only elements with that value' do | ||
expect(page).to have_css('.col-id', text: /#{archived_invoice.id}/) | ||
expect(page).not_to have_css('.col-id', text: /#{active_invoice.id}/) | ||
end | ||
end | ||
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
Oops, something went wrong.
if value of enum_option_name is blank, the view of tag_column is broken.
if it is blank, just need to return empty string, ""