Class: Graphiti::Sideload::PolymorphicBelongsTo
- Inherits:
-
BelongsTo
- Object
- Graphiti::Sideload
- BelongsTo
- Graphiti::Sideload::PolymorphicBelongsTo
- Defined in:
- lib/graphiti/sideload/polymorphic_belongs_to.rb
Defined Under Namespace
Constant Summary
Constants inherited from Graphiti::Sideload
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
Attributes inherited from Graphiti::Sideload
#group_name, #link, #name, #parent, #parent_resource_class, #polymorphic_as
Class Method Summary collapse
Instance Method Summary collapse
- #child_for_type(type) ⇒ Object
- #child_for_type!(type) ⇒ Object
- #infer_foreign_key ⇒ Object
-
#initialize(name, opts) ⇒ PolymorphicBelongsTo
constructor
A new instance of PolymorphicBelongsTo.
- #resolve(parents, query, graph_parent) ⇒ Object
- #type ⇒ Object
Methods inherited from BelongsTo
#base_filter, #ids_for_parents, #load_params
Methods inherited from Graphiti::Sideload
after_save, #always_include_resource_ids?, assign, #assign, #assign_each, assign_each, #associate, #associate_all, #association_name, #base_scope, #build_resource_proxy, #clear_resources, #create_remote_resource, #description, #disassociate, #errors, #fire_hooks!, #foreign_key, hooks, #ids_for_parents, link, #link?, #link_extra_fields, #link_filter, #load, #load_params, params, #parent_resource, #performant_assign?, #polymorphic_child?, #polymorphic_has_many?, #polymorphic_has_one?, #polymorphic_parent?, pre_load, #primary_key, #readable?, #remote?, #resource, #resource_class, #resource_class_loaded?, #scope, scope, #shared_remote?, #single?, #writable?
Constructor Details
#initialize(name, opts) ⇒ PolymorphicBelongsTo
Returns a new instance of PolymorphicBelongsTo.
89 90 91 92 93 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 89 def initialize(name, opts) super self.children = {} grouper.apply(self, parent_resource_class) end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
73 74 75 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 73 def children @children end |
Class Method Details
Instance Method Details
#child_for_type(type) ⇒ Object
95 96 97 98 99 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 95 def child_for_type(type) children.values.find do |sideload| sideload.resource.type == type end end |
#child_for_type!(type) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 101 def child_for_type!(type) if (child = child_for_type(type)) child else err = ::Graphiti::Errors::PolymorphicSideloadTypeNotFound raise err.new(self, type) end end |
#infer_foreign_key ⇒ Object
80 81 82 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 80 def infer_foreign_key :"#{name}_id" end |
#resolve(parents, query, graph_parent) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 110 def resolve(parents, query, graph_parent) parents.group_by(&grouper.field_name).each_pair do |group_name, group| next if group_name.nil? || grouper.ignore?(group_name) match = ->(c) { c.group_name == group_name.to_sym } if (sideload = children.values.find(&match)) duped = remove_invalid_sideloads(sideload.resource, query) sideload.resolve(group, duped, graph_parent) else err = ::Graphiti::Errors::PolymorphicSideloadChildNotFound raise err.new(self, group_name) end end end |
#type ⇒ Object
76 77 78 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 76 def type :polymorphic_belongs_to end |