Skip to content

Commit 0212c33

Browse files
authored
Merge pull request #340 from plural/illustrator-includes
Fix relationship problem for illustrators <-> printings.
2 parents 5805b84 + 6ebd8ac commit 0212c33

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

app/controllers/illustrators_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
class IllustratorsController < ApplicationController
55
def index
66
add_total_stat(params)
7-
illustrators = IllustratorResource.all(params)
7+
base_scope = Illustrator
8+
if params.include?('include') && params[:include].include?('printings')
9+
base_scope = Illustrator.includes(%i[printings])
10+
end
11+
illustrators = IllustratorResource.all(params, base_scope)
812
respond_with(illustrators)
913
end
1014

app/resources/illustrator_resource.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
class IllustratorResource < ApplicationResource
55
primary_endpoint '/illustrators', %i[index show]
66

7+
self.attributes_writable_by_default = false
8+
79
attribute :id, :string
810
attribute :name, :string
911
attribute :num_printings, :integer
1012
attribute :updated_at, :datetime
1113

12-
many_to_many :printings, relation_name: :printings
14+
many_to_many :printings do
15+
assign_each do |illustrator, printings|
16+
printings.select { |p| p.illustrator_ids_in_database.include?(illustrator.id) }
17+
end
18+
end
1319
end

app/resources/printing_resource.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Public resource for Printing objects.
44
class PrintingResource < ApplicationResource # rubocop:disable Metrics/ClassLength
55
primary_endpoint '/printings', %i[index show]
6+
self.attributes_writable_by_default = false
67
self.default_page_size = 1000
78
self.max_page_size = 10_000
89

@@ -130,6 +131,10 @@ class PrintingResource < ApplicationResource # rubocop:disable Metrics/ClassLeng
130131
end
131132

132133
many_to_many :illustrators do
134+
assign_each do |printing, illustrators|
135+
illustrators.select { |i| printing.illustrator_ids_in_database.include?(i.id) }
136+
end
137+
133138
link do |p|
134139
format('%<url>s?filter[id]=%<ids>s', url: Rails.application.routes.url_helpers.illustrators_url,
135140
ids: p.illustrator_ids_in_database.join(','))

0 commit comments

Comments
 (0)