-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enum translations #442
Merged
Merged
Enum translations #442
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e5b6955
chore(rspec): use doc format
difernandez 8b3d091
feat(support): add enum utils
difernandez 6b0b2fd
feat(tag-builder): translate rails enum values
difernandez 2a1a574
feat(support): override select filter to translate values
difernandez 7b6d42d
docs(readme-and-docs): document enum translations in filters and tag …
difernandez aa283d5
docs(changelog): add enum translations entry
difernandez 5817f2c
fix(circleci): use specific node version
difernandez f2745bc
fix(circleci): install file command
difernandez e2bd5f9
fix(select2-spec): reset default_select after each test
difernandez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be good to test that the collection is actually filtered, what do you think? maybe it's not necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, especially considering that the filter only works with the db value, that way we make sure it's working. I'll add the test soon before merging