diff --git a/app/controllers/alchemy/admin/resources_controller.rb b/app/controllers/alchemy/admin/resources_controller.rb index b61b20726a..0fefed1a5c 100644 --- a/app/controllers/alchemy/admin/resources_controller.rb +++ b/app/controllers/alchemy/admin/resources_controller.rb @@ -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. diff --git a/spec/dummy/app/models/event.rb b/spec/dummy/app/models/event.rb index 124db65122..81696dd983 100644 --- a/spec/dummy/app/models/event.rb +++ b/spec/dummy/app/models/event.rb @@ -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(*) [ @@ -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 diff --git a/spec/dummy/config/locales/alchemy.en.yml b/spec/dummy/config/locales/alchemy.en.yml index e1bf024fe2..fc4c8d178b 100644 --- a/spec/dummy/config/locales/alchemy.en.yml +++ b/spec/dummy/config/locales/alchemy.en.yml @@ -4,7 +4,7 @@ en: event: start: name: Start - by_location_name: + by_location_id: name: Location element_names: gallery_picture: Gallery picture diff --git a/spec/features/admin/resources_integration_spec.rb b/spec/features/admin/resources_integration_spec.rb index d8b152c18e..3ceb94a3d6 100644 --- a/spec/features/admin/resources_integration_spec.rb +++ b/spec/features/admin/resources_integration_spec.rb @@ -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) @@ -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