Module: Graphiti::Resource::Polymorphism::ClassMethods
- Defined in:
- lib/graphiti/resource/polymorphism.rb
Instance Method Summary collapse
- #children ⇒ Object
- #inherited(klass) ⇒ Object
- #resource_for_model(model) ⇒ Object
- #resource_for_type(type) ⇒ Object
- #sideload(name) ⇒ Object
Instance Method Details
#children ⇒ Object
69 70 71 72 73 |
# File 'lib/graphiti/resource/polymorphism.rb', line 69 def children @children ||= polymorphic.map { |klass| klass.is_a?(String) ? klass.safe_constantize : klass } end |
#inherited(klass) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/graphiti/resource/polymorphism.rb', line 42 def inherited(klass) return super unless polymorphic? klass.type = nil klass.model = klass.infer_model klass.endpoint = klass.infer_endpoint klass.polymorphic_child = true super end |
#resource_for_model(model) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/graphiti/resource/polymorphism.rb', line 84 def resource_for_model(model) resource = children.find { |c| model.instance_of?(c.model) } || children.find { |c| model.is_a?(c.model) } if resource.nil? raise Errors::PolymorphicResourceChildNotFound.new(self, model: model) else resource end end |
#resource_for_type(type) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/graphiti/resource/polymorphism.rb', line 75 def resource_for_type(type) resource = children.find { |c| c.type.to_s == type.to_s } if resource.nil? raise Errors::PolymorphicResourceChildNotFound.new(self, type: type) else resource end end |
#sideload(name) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/graphiti/resource/polymorphism.rb', line 52 def sideload(name) return super unless polymorphic? if (split_on = name.to_s.split(/^on__/)).length > 1 on_type, name = split_on[1].split("--").map(&:to_sym) end sl = super if !polymorphic_child? && sl.nil? children.each do |c| next if on_type && c.type != on_type break if (sl = c.sideloads[name]) end end sl end |