Skip to content

Commit 6703353

Browse files
committed
active storage attachment association support
1 parent 82fa808 commit 6703353

File tree

7 files changed

+38
-7
lines changed

7 files changed

+38
-7
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
graphql-eager_loader (0.1.0)
4+
graphql-eager_loader (0.2.0)
55

66
GEM
77
remote: https://rubygems.org/

lib/graphql/eager_loader/builder.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def self.call(selections:, model:)
3636
builder = new(selection: selection, model: model)
3737

3838
if builder.association?
39-
includes[builder.field_name] = builder.includes
39+
includes[builder.association_name] = builder.includes
4040
else
4141
includes.merge!(builder.includes)
4242
end
@@ -56,24 +56,31 @@ def association?
5656
association.present?
5757
end
5858

59-
def field_name
60-
selection.name
59+
def association_name
60+
association.name
6161
end
6262

6363
private
6464

6565
attr_reader :selection, :model
6666

6767
def includes_model
68+
return ActiveStorage::Attachment if active_storage_attachment?
69+
6870
association&.klass || model
6971
end
7072

7173
def association
7274
return if use_custom_method?
75+
return model.reflect_on_association("#{field_name}_attachment") if active_storage_attachment?
7376

7477
model.reflect_on_association(field_name)
7578
end
7679

80+
def active_storage_attachment?
81+
model.reflect_on_attachment(field_name).present?
82+
end
83+
7784
def use_custom_method?
7885
custom_method_defined? && !ignore_custom_method?
7986
end
@@ -91,6 +98,10 @@ def custom_method_defined?
9198
def field_owner
9299
selection.field.owner
93100
end
101+
102+
def field_name
103+
selection.name
104+
end
94105
end
95106
end
96107
end

lib/graphql/eager_loader/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Graphql
44
module EagerLoader
5-
VERSION = '0.1.0'
5+
VERSION = '0.2.0'
66
end
77
end

spec/graphql/eager_loader_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@
6565
end
6666
end
6767

68-
describe 'cursor based pagination, has_many, belongs_to' do
68+
describe 'cursor based pagination, has_many, belongs_to, attachment' do
6969
let(:model) { ::User }
7070
let(:query_string) do
7171
<<-QUERY
7272
query {
7373
users {
7474
nodes {
7575
id
76+
photo { filename }
7677
jobs {
7778
id
7879
user { id }
@@ -89,7 +90,8 @@
8990
it do
9091
expect(options).to eq(
9192
proposal_documents: {},
92-
jobs: { user: {} }
93+
jobs: { user: {} },
94+
photo_attachment: {}
9395
)
9496
end
9597
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
module Types
4+
class File < GraphQL::Schema::Object
5+
field :filename, String, null: false
6+
end
7+
end

spec/internal/app/graphql/types/user.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ class User < GraphQL::Schema::Object
66
field :jobs, [Job], null: false
77
field :proposal_documents, [ProposalDocument], null: false
88
field :order, Order, null: false
9+
field :photo, Types::File, null: true
910

1011
def order
1112
{ code: SecureRandom.uuid }
1213
end
14+
15+
def photo
16+
object.photo if object.photo.attached?
17+
end
18+
19+
def self.allow_include_builder_fields
20+
[:photo]
21+
end
1322
end
1423
end

spec/internal/app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
class User < ActiveRecord::Base
44
has_many :jobs
55
has_many :proposal_documents
6+
7+
has_one_attached :photo
68
end

0 commit comments

Comments
 (0)