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
59 60 61 62 63 |
# File 'lib/graphiti/resource/polymorphism.rb', line 59 def children @children ||= polymorphic.map { |klass| klass.is_a?(String) ? klass.safe_constantize : klass } end |
#inherited(klass) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/graphiti/resource/polymorphism.rb', line 36 def inherited(klass) klass.type = nil klass.model = klass.infer_model klass.endpoint = klass.infer_endpoint klass.polymorphic_child = true super end |
#resource_for_model(model) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/graphiti/resource/polymorphism.rb', line 74 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
65 66 67 68 69 70 71 72 |
# File 'lib/graphiti/resource/polymorphism.rb', line 65 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/graphiti/resource/polymorphism.rb', line 44 def sideload(name) if (split_on = name.to_s.split(/^on__/)).length > 1 on_type, name = split_on[1].split("--").map(&:to_sym) end sl = super(name) 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 |