Module: Graphiti::Resource::Sideloading::ClassMethods
- Defined in:
- lib/graphiti/resource/sideloading.rb
Instance Method Summary collapse
- #all_sideloads(memo = {}) ⇒ Object
- #allow_sideload(name, opts = {}, &blk) ⇒ Object
- #apply_sideload_to_serializer(name) ⇒ Object
- #apply_sideloads_to_serializer ⇒ Object
- #association_names(memo = []) ⇒ Object
- #association_types(memo = []) ⇒ Object
- #belongs_to(name, opts = {}, &blk) ⇒ Object
-
#eagerly_apply_sideload?(sideload) ⇒ Boolean
If eager loading, ensure routes are loaded first, then apply This happens in Railtie.
- #has_many(name, opts = {}, &blk) ⇒ Object
- #has_one(name, opts = {}, &blk) ⇒ Object
- #many_to_many(name, opts = {}, &blk) ⇒ Object
- #polymorphic_belongs_to(name, opts = {}, &blk) ⇒ Object
- #polymorphic_has_many(name, opts = {}, &blk) ⇒ Object
- #polymorphic_has_one(name, opts = {}, &blk) ⇒ Object
- #sideload(name) ⇒ Object
Instance Method Details
#all_sideloads(memo = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/graphiti/resource/sideloading.rb', line 96 def all_sideloads(memo = {}) sideloads.each_pair do |name, sideload| unless memo[name] memo[name] = sideload memo.merge!(sideload.resource.class.all_sideloads(memo)) end end memo end |
#allow_sideload(name, opts = {}, &blk) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/graphiti/resource/sideloading.rb', line 9 def allow_sideload(name, opts = {}, &blk) klass = Class.new(opts.delete(:class) || Sideload) klass.class_eval(&blk) if blk opts[:parent_resource] = self relationship_option(opts, :readable) relationship_option(opts, :writable) sideload = klass.new(name, opts) if (parent = opts[:parent]) parent.children[name] = sideload else config[:sideloads][name] = sideload apply_sideload_to_serializer(name) if eagerly_apply_sideload?(sideload) end sideload end |
#apply_sideload_to_serializer(name) ⇒ Object
25 26 27 |
# File 'lib/graphiti/resource/sideloading.rb', line 25 def apply_sideload_to_serializer(name) Util::SerializerRelationships.new(self, config[:sideloads].slice(name)).apply end |
#apply_sideloads_to_serializer ⇒ Object
29 30 31 |
# File 'lib/graphiti/resource/sideloading.rb', line 29 def apply_sideloads_to_serializer Util::SerializerRelationships.new(self, config[:sideloads]).apply end |
#association_names(memo = []) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/graphiti/resource/sideloading.rb', line 106 def association_names(memo = []) all_sideloads.each_pair do |name, sl| unless memo.include?(sl.name) memo << sl.name memo |= sl.resource.class.association_names(memo) end end memo end |
#association_types(memo = []) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/graphiti/resource/sideloading.rb', line 117 def association_types(memo = []) all_sideloads.each_pair do |name, sl| unless memo.include?(sl.resource.type) memo << sl.resource.type memo |= sl.resource.class.association_types(memo) end end memo end |
#belongs_to(name, opts = {}, &blk) ⇒ Object
38 39 40 41 |
# File 'lib/graphiti/resource/sideloading.rb', line 38 def belongs_to(name, opts = {}, &blk) opts[:class] ||= adapter.sideloading_classes[:belongs_to] allow_sideload(name, opts, &blk) end |
#eagerly_apply_sideload?(sideload) ⇒ Boolean
If eager loading, ensure routes are loaded first, then apply This happens in Railtie
130 131 132 133 134 135 136 137 |
# File 'lib/graphiti/resource/sideloading.rb', line 130 def eagerly_apply_sideload?(sideload) # TODO: Maybe handle this in graphiti-rails if defined?(::Rails) && (app = ::Rails.application) app.config.eager_load ? false : true else sideload.resource_class_loaded? end end |
#has_many(name, opts = {}, &blk) ⇒ Object
33 34 35 36 |
# File 'lib/graphiti/resource/sideloading.rb', line 33 def has_many(name, opts = {}, &blk) opts[:class] ||= adapter.sideloading_classes[:has_many] allow_sideload(name, opts, &blk) end |
#has_one(name, opts = {}, &blk) ⇒ Object
43 44 45 46 |
# File 'lib/graphiti/resource/sideloading.rb', line 43 def has_one(name, opts = {}, &blk) opts[:class] ||= adapter.sideloading_classes[:has_one] allow_sideload(name, opts, &blk) end |
#many_to_many(name, opts = {}, &blk) ⇒ Object
48 49 50 51 |
# File 'lib/graphiti/resource/sideloading.rb', line 48 def many_to_many(name, opts = {}, &blk) opts[:class] ||= adapter.sideloading_classes[:many_to_many] allow_sideload(name, opts, &blk) end |
#polymorphic_belongs_to(name, opts = {}, &blk) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/graphiti/resource/sideloading.rb', line 53 def polymorphic_belongs_to(name, opts = {}, &blk) opts[:resource] ||= Class.new(::Graphiti::Resource) { self.polymorphic = [] self.abstract_class = true } # adapters *probably* don't need to override this, but it's allowed opts[:class] ||= adapter.sideloading_classes[:polymorphic_belongs_to] opts[:class] ||= ::Graphiti::Sideload::PolymorphicBelongsTo allow_sideload(name, opts, &blk) end |
#polymorphic_has_many(name, opts = {}, &blk) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/graphiti/resource/sideloading.rb', line 64 def polymorphic_has_many(name, opts = {}, &blk) as = opts.delete(:as) opts[:foreign_key] ||= :"#{as}_id" opts[:polymorphic_as] ||= as model_ref = model has_many name, opts do params do |hash| hash[:filter][:"#{as}_type"] = {eql: model_ref.name} end instance_eval(&blk) if blk end end |
#polymorphic_has_one(name, opts = {}, &blk) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/graphiti/resource/sideloading.rb', line 78 def polymorphic_has_one(name, opts = {}, &blk) as = opts.delete(:as) opts[:foreign_key] ||= :"#{as}_id" opts[:polymorphic_as] ||= as model_ref = model has_one name, opts do params do |hash| hash[:filter][:"#{as}_type"] = {eql: model_ref.name} end instance_eval(&blk) if blk end end |
#sideload(name) ⇒ Object
92 93 94 |
# File 'lib/graphiti/resource/sideloading.rb', line 92 def sideload(name) sideloads[name] end |