Module: Graphiti::ActiveGraph::ResourceProxy
- Includes:
- SideloadResolve
- Defined in:
- lib/graphiti/active_graph/resource_proxy.rb
Instance Attribute Summary collapse
-
#preloaded ⇒ Object
readonly
Returns the value of attribute preloaded.
Instance Method Summary collapse
- #data ⇒ Object
- #data_for_preloaded_record ⇒ Object
- #data_for_preloaded_records ⇒ Object
- #initialize(resource, scope, query, payload: nil, single: false, raise_on_missing: false, preloaded: false) ⇒ Object
- #save(action: :create) ⇒ Object
- #stats ⇒ Object
Methods included from SideloadResolve
Instance Attribute Details
#preloaded ⇒ Object (readonly)
Returns the value of attribute preloaded.
4 5 6 |
# File 'lib/graphiti/active_graph/resource_proxy.rb', line 4 def preloaded @preloaded end |
Instance Method Details
#data ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/graphiti/active_graph/resource_proxy.rb', line 20 def data return @data if @data return super unless @preloaded resolve_sideloads(@preloaded) @single ? data_for_preloaded_record : data_for_preloaded_records end |
#data_for_preloaded_record ⇒ Object
29 30 31 32 33 |
# File 'lib/graphiti/active_graph/resource_proxy.rb', line 29 def data_for_preloaded_record @preloaded = @preloaded.is_a?(Array) ? @preloaded[0] : @preloaded @resource.decorate_record(@preloaded) @data = @preloaded end |
#data_for_preloaded_records ⇒ Object
35 36 37 38 39 40 |
# File 'lib/graphiti/active_graph/resource_proxy.rb', line 35 def data_for_preloaded_records @preloaded.each do |r| @resource.decorate_record(r) end @data = @preloaded end |
#initialize(resource, scope, query, payload: nil, single: false, raise_on_missing: false, preloaded: false) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/graphiti/active_graph/resource_proxy.rb', line 6 def initialize(resource, scope, query, payload: nil, single: false, raise_on_missing: false, preloaded: false) @resource = resource @scope = scope @query = query @payload = payload @single = single @raise_on_missing = raise_on_missing @preloaded = preloaded end |
#save(action: :create) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/graphiti/active_graph/resource_proxy.rb', line 54 def save(action: :create) # TODO: remove this. Only used for persisting many-to-many with AR # (see activerecord adapter) original = Graphiti.context[:namespace] begin Graphiti.context[:namespace] = action ::Graphiti::RequestValidator.new(@resource, @payload.params, action).validate! validator = persist { @resource.persist_with_relationships \ @payload.(action: action), @payload.attributes, @payload.relationships } ensure Graphiti.context[:namespace] = original end @data, success = validator.to_a if success && !resource.relation_resource? # If the context namespace is `update` or `create`, certain # adapters will cause N+1 validation calls, so lets explicitly # switch to a lookup context. Graphiti.with_context(Graphiti.context[:object], :show) do @scope.resolve_sideloads([@data]) end end success end |
#stats ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/graphiti/active_graph/resource_proxy.rb', line 42 def stats @stats ||= if @query.stats && !resource.relation_resource? payload = ::Graphiti::Stats::Payload.new @resource, @query, @scope.unpaginated_object, data payload.generate else {} end end |