Skip to content

Commit

Permalink
Merge pull request #3067 from AlchemyCMS/fix-resource-filter
Browse files Browse the repository at this point in the history
Fix filtering associated models by id
  • Loading branch information
tvdeyen authored Oct 14, 2024
2 parents d5a5059 + 2fbee96 commit 2caa660
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def sanitize_filter_params!
end

def eligible_resource_filter_values
resource_filters.map(&:values).flatten
resource_filters.map(&:values).flatten!.map!(&:to_s)
end

# Returns a translated +flash[:notice]+ for current controller action.
Expand Down
6 changes: 3 additions & 3 deletions spec/dummy/app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Event < ActiveRecord::Base

scope :starting_today, -> { where(starts_at: Time.current.at_midnight..Date.tomorrow.at_midnight) }
scope :future, -> { where("starts_at > ?", Date.tomorrow.at_midnight) }
scope :by_location_name, ->(name) { joins(:location).where(locations: {name: name}) }
scope :by_location_id, ->(id) { where(location_id: id) }

def self.ransackable_attributes(*)
[
Expand All @@ -37,8 +37,8 @@ def self.alchemy_resource_filters
values: %w[starting_today future]
},
{
name: :by_location_name,
values: Location.distinct.pluck(:name)
name: :by_location_id,
values: Location.all.map { |l| [l.name, l.id] }
}
]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ en:
event:
start:
name: Start
by_location_name:
by_location_id:
name: Location
element_names:
gallery_picture: Gallery picture
Expand Down
21 changes: 20 additions & 1 deletion spec/features/admin/resources_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
end

it "can combine multiple filters" do
visit "/admin/events?filter[start]=starting_today&filter[by_location_name]=#{location.name}"
visit "/admin/events?filter[start]=starting_today&filter[by_location_id]=#{location.id}"

within "div#archive_all table.list tbody" do
expect(page).to have_selector("tr", count: 1)
Expand Down Expand Up @@ -136,6 +136,25 @@
expect(page).not_to have_content("yesterday")
end
end

context "selecting a associated model by it's id" do
it "should filter the list to only show matching items", :js do
visit "/admin/events"

within "div#archive_all table.list tbody" do
expect(page).to have_selector("tr", count: 3)
end

within "#library_sidebar #filter_bar" do
select2(location.name, from: "Location")
end

within "div#archive_all table.list tbody" do
expect(page).to have_selector("tr", count: 1)
expect(page).to have_content("today 2")
end
end
end
end
end

Expand Down

0 comments on commit 2caa660

Please sign in to comment.