Skip to content

Commit ab3eb3f

Browse files
authored
Merge pull request #3766 from mamhoff/resources-controller-distinct
Resources controller: Return distinct records
2 parents ed95633 + 45d8fa5 commit ab3eb3f

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

app/controllers/alchemy/admin/resources_controller.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff 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.

spec/controllers/alchemy/admin/resources_controller_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# frozen_string_literal: true
22

33
class Admin::LocationsController < Alchemy::Admin::ResourcesController
4+
private
5+
6+
def permitted_ransack_search_fields
7+
super + [
8+
"events_name_cont"
9+
]
10+
end
411
end

spec/dummy/app/models/location.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
class 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
714
end

0 commit comments

Comments
 (0)