Skip to content
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

Fix filtering associated models by id #3067

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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