File tree Expand file tree Collapse file tree 4 files changed +39
-6
lines changed
app/controllers/alchemy/admin
controllers/alchemy/admin Expand file tree Collapse file tree 4 files changed +39
-6
lines changed Original file line number Diff line number Diff line change @@ -18,12 +18,7 @@ class ResourcesController < Alchemy::Admin::BaseController
1818 before_action :authorize_resource
1919
2020 def index
21- @query = resource_handler . model . ransack ( search_filter_params [ :q ] )
22- @query . sorts = default_sort_order if @query . sorts . empty?
23- items = @query . result
24-
25- items = items . includes ( *resource_relations_names ) if contains_relations?
26- items = items . tagged_with ( search_filter_params [ :tagged_with ] ) if search_filter_params [ :tagged_with ] . present?
21+ items = collection
2722
2823 respond_to do |format |
2924 format . html do
@@ -121,6 +116,16 @@ def authorize_resource
121116 authorize! ( action_name . to_sym , resource_instance_variable || resource_handler . model )
122117 end
123118
119+ def collection
120+ @query = resource_handler . model . ransack ( search_filter_params [ :q ] )
121+ @query . sorts = default_sort_order if @query . sorts . empty?
122+ items = @query . result ( distinct : true )
123+
124+ items = items . includes ( *resource_relations_names ) if contains_relations?
125+ items = items . tagged_with ( search_filter_params [ :tagged_with ] ) if search_filter_params [ :tagged_with ] . present?
126+ items
127+ end
128+
124129 # Permits all editable resource attributes as default.
125130 #
126131 # Define this method in your inheriting controller if you want to permit additional attributes.
Original file line number Diff line number Diff line change @@ -85,6 +85,20 @@ def resource_handler
8585 end
8686 end
8787 end
88+
89+ describe "searching for records with multiple associated records" do
90+ controller ( Admin ::LocationsController ) { }
91+
92+ let ( :bauwagen ) { create ( :location , name : "Bauwagen" ) }
93+ let! ( :peter ) { create ( :event , name : "Peter" , location : bauwagen ) }
94+ let! ( :lustig ) { create ( :event , name : "Lustig" , location : bauwagen ) }
95+
96+ it "returns only one bauwagen" do
97+ get :index , params : { q : { events_name_cont : "t" } }
98+ expect ( response ) . to be_successful
99+ expect ( assigns ( :locations ) ) . to eq ( [ bauwagen ] )
100+ end
101+ end
88102 end
89103
90104 describe "#update" do
Original file line number Diff line number Diff line change 11# frozen_string_literal: true
22
33class Admin ::LocationsController < Alchemy ::Admin ::ResourcesController
4+ private
5+
6+ def permitted_ransack_search_fields
7+ super + [
8+ "events_name_cont"
9+ ]
10+ end
411end
Original file line number Diff line number Diff line change 33class Location < ActiveRecord ::Base
44 extend Alchemy ::SearchableResource
55 include Alchemy ::Taggable
6+
67 has_many :events
8+
9+ def self . ransackable_associations ( _auth_object = nil )
10+ [
11+ :events
12+ ]
13+ end
714end
You can’t perform that action at this time.
0 commit comments