Class: GraphQL::Models::AssociationLoadRequest
- Inherits:
-
Object
- Object
- GraphQL::Models::AssociationLoadRequest
- Defined in:
- lib/graphql/models/association_load_request.rb
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#base_model ⇒ Object
readonly
Returns the value of attribute base_model.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
-
#ensure_cardinality(result) ⇒ Object
If the value should be an array, make sure it’s an array.
-
#fulfilled(result) ⇒ Object
When the request is fulfilled, this method is called so that it can do whatever caching, etc.
-
#initialize(base_model, association_name, context) ⇒ AssociationLoadRequest
constructor
A new instance of AssociationLoadRequest.
- #load ⇒ Object
- #load_target ⇒ Object
-
#load_type ⇒ Object
Public members that all load requests should implement.
-
#target_class ⇒ Object
Public members specific to an association load request.
Constructor Details
#initialize(base_model, association_name, context) ⇒ AssociationLoadRequest
Returns a new instance of AssociationLoadRequest.
6 7 8 9 10 |
# File 'lib/graphql/models/association_load_request.rb', line 6 def initialize(base_model, association_name, context) @base_model = base_model @association = base_model.association(association_name) @context = context end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
4 5 6 |
# File 'lib/graphql/models/association_load_request.rb', line 4 def association @association end |
#base_model ⇒ Object (readonly)
Returns the value of attribute base_model.
4 5 6 |
# File 'lib/graphql/models/association_load_request.rb', line 4 def base_model @base_model end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
4 5 6 |
# File 'lib/graphql/models/association_load_request.rb', line 4 def context @context end |
Instance Method Details
#ensure_cardinality(result) ⇒ Object
If the value should be an array, make sure it’s an array. If it should be a single value, make sure it’s single. Passed in result could be a single model or an array of models.
45 46 47 48 49 50 51 52 |
# File 'lib/graphql/models/association_load_request.rb', line 45 def ensure_cardinality(result) case reflection.macro when :has_many Array.wrap(result) else result.is_a?(Array) ? result[0] : result end end |
#fulfilled(result) ⇒ Object
When the request is fulfilled, this method is called so that it can do whatever caching, etc. is needed
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/graphql/models/association_load_request.rb', line 55 def fulfilled(result) association.loaded! if reflection.macro == :has_many association.target.concat(result) result.each do |m| association.set_inverse_instance(m) end else association.target = result association.set_inverse_instance(result) if result end end |
#load ⇒ Object
69 70 71 |
# File 'lib/graphql/models/association_load_request.rb', line 69 def load loader.load(self) end |
#load_target ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/graphql/models/association_load_request.rb', line 25 def load_target case reflection.macro when :belongs_to base_model.send(reflection.foreign_key) when :has_many base_model.send(association.reflection.name) else # has_one, need to construct our own relation, because accessing the relation will load the model condition = { reflection.foreign_key => base_model.id } if reflection..include?(:as) condition[reflection.type] = base_model.class.name end target_class.where(condition) end end |
#load_type ⇒ Object
Public members that all load requests should implement
16 17 18 19 20 21 22 23 |
# File 'lib/graphql/models/association_load_request.rb', line 16 def load_type case reflection.macro when :belongs_to :id else :relation end end |
#target_class ⇒ Object
Public members specific to an association load request
77 78 79 80 81 82 83 |
# File 'lib/graphql/models/association_load_request.rb', line 77 def target_class case when reflection.polymorphic? base_model.send(reflection.foreign_type).constantize else reflection.klass end end |