Skip to content

Commit 2a1a574

Browse files
committed
feat(support): override select filter to translate values
1 parent 6b0b2fd commit 2a1a574

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module ActiveAdminAddons
2+
module SelectFilterInputExtension
3+
def collection_from_enum?
4+
klass.respond_to?(:defined_enums) && klass.defined_enums.has_key?(method.to_s)
5+
end
6+
7+
def collection
8+
if !options[:collection] && collection_from_enum?
9+
EnumUtils.options_for_select(klass, method.to_s, use_db_value: true)
10+
else
11+
super
12+
end
13+
end
14+
end
15+
end
16+
17+
::ActiveAdmin::Inputs::Filters::SelectInput.send(
18+
:prepend, ActiveAdminAddons::SelectFilterInputExtension
19+
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
require "rails_helper"
2+
3+
describe "Select Filter Input", type: :feature do
4+
context 'when used with an enum column' do
5+
let!(:active_invoice) { Invoice.create!(status: :active) }
6+
let!(:archived_invoice) { Invoice.create!(status: :archived) }
7+
let(:option_selector) { '.filter_form select[name="q[status_eq]"] option' }
8+
9+
context "with collection option" do
10+
before do
11+
register_page(Invoice) do
12+
filter :status, as: :select, collection: [
13+
['Activo, si se considera', 0],
14+
['Archivado, ya no se considera', 1],
15+
['Estado que no existe', 42]
16+
]
17+
end
18+
19+
visit admin_invoices_path
20+
end
21+
22+
it "uses collection directly" do
23+
expect(page.find("#{option_selector}[value='0']").text).to eq('Activo, si se considera')
24+
expect(page.find("#{option_selector}[value='1']").text).to(
25+
eq('Archivado, ya no se considera')
26+
)
27+
expect(page.find("#{option_selector}[value='42']").text).to eq('Estado que no existe')
28+
end
29+
30+
it 'displays all records' do
31+
expect(page).to have_css('.col-id', text: /#{active_invoice.id}/)
32+
expect(page).to have_css('.col-id', text: /#{archived_invoice.id}/)
33+
end
34+
35+
context 'when selecting a value', js: true do
36+
before do
37+
pick_select2_entered_option('Activo, si se considera')
38+
click_filter_btn
39+
end
40+
41+
it 'displays only elements with that value' do
42+
expect(page).to have_css('.col-id', text: /#{active_invoice.id}/)
43+
expect(page).not_to have_css('.col-id', text: /#{archived_invoice.id}/)
44+
end
45+
end
46+
end
47+
48+
context "without collection option" do
49+
before do
50+
register_page(Invoice) do
51+
filter :status, as: :select
52+
end
53+
54+
visit admin_invoices_path
55+
end
56+
57+
it "renders options with enum db values and translated labels" do
58+
expect(page.find("#{option_selector}[value='0']").text).to eq('Activo')
59+
expect(page.find("#{option_selector}[value='1']").text).to eq('Archivado')
60+
end
61+
62+
it 'displays all records' do
63+
expect(page).to have_css('.col-id', text: /#{active_invoice.id}/)
64+
expect(page).to have_css('.col-id', text: /#{archived_invoice.id}/)
65+
end
66+
67+
context 'when selecting a value', js: true do
68+
before do
69+
pick_select2_entered_option('Archivado')
70+
click_filter_btn
71+
end
72+
73+
it 'displays only elements with that value' do
74+
expect(page).to have_css('.col-id', text: /#{archived_invoice.id}/)
75+
expect(page).not_to have_css('.col-id', text: /#{active_invoice.id}/)
76+
end
77+
end
78+
end
79+
end
80+
end

0 commit comments

Comments
 (0)