8000 Prioritize filters with predicates in active filter labels (#7702) · activeadmin/activeadmin@d46e559 · GitHub
[go: up one dir, main page]

Skip to content

Commit d46e559

Browse files
committed
Prioritize filters with predicates in active filter labels (#7702)
When there are filters for a single field and a multi-field ransack search with predicate, the multi-field label on the active filter should use the custom label from the filter input box. Reproduction steps: ./app/admin/posts.rb ``` ActiveAdmin.register Post do filter :title_or_body_contains, as: :string, label: 'Title or Body' filter :title, as: :string ... end ``` Navigate to the posts path for a user like `localhost:3000/admin/users/4/posts` Complete search in both fields, note that both active filters use the label "Title contains". The `title_or_body_contains` should use the label from the filter "Title or Body contains".
1 parent 7467fc1 commit d46e559

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/active_admin/filters/active_filter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ def find_class
9898
end
9999

100100
def filter
101-
resource.filters[name.to_sym] || resource.filters[condition.key.to_sym]
101+
resource.filters[condition.key.to_sym] ||
102+
resource.filters[condition.key.remove("_#{condition.predicate.name}").to_sym] ||
103+
resource.filters[name.to_sym]
102104
end
103105

104106
def related_primary_key

spec/unit/filters/active_filter_spec.rb

Lines changed: 18 additions & 0 deletions
202
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@
190190
resource.add_filter(:title_or_body_cont, label: label)
191191
expect(subject.label).to eq("#{label} contains")
192192
end
193+
194+
context "when an additional filter exists for one of the fields" do
195+
it "should use the filter label as the label prefix" do
196+
label = "#{user.first_name}'s Post"
197+
resource.add_filter(:title_or_body_cont, label: label)
198+
resource.add_filter(:title)
199+
expect(subject.label).to eq("#{label} contains")
200+
end
201+
end
+
203+
context "when a filter with a dropdown is searched" do
204+
it "should use the filter label as the label prefix" do
205+
label = "#{user.first_name}'s Post"
206+
resource.add_filter(:title_or_body, filters: [:contains, :equals], label: label)
207+
resource.add_filter(:title)
208+
expect(subject.label).to eq("#{label} contains")
209+
end
210+
end
193211
end
194212
end
195213

0 commit comments

Comments
 (0)
0