diff --git a/lib/graphiti/resource/interface.rb b/lib/graphiti/resource/interface.rb index 839c708f..ffa8f365 100644 --- a/lib/graphiti/resource/interface.rb +++ b/lib/graphiti/resource/interface.rb @@ -50,7 +50,14 @@ def build(params, base_scope = nil) validate_request!(params) runner = Runner.new(self, params) runner.proxy(base_scope, single: true, raise_on_missing: true).tap do |instance| - instance.assign_attributes # assign the params to the underlying model + instance.assign_attributes(params) # assign the params to the underlying model + end + end + + def load(models, base_scope = nil) + runner = Runner.new(self, {}, base_scope, :find) + runner.proxy(nil, bypass_required_filters: true).tap do |r| + r.data = models end end diff --git a/lib/graphiti/resource/persistence.rb b/lib/graphiti/resource/persistence.rb index c89b946a..1f7b4b0c 100644 --- a/lib/graphiti/resource/persistence.rb +++ b/lib/graphiti/resource/persistence.rb @@ -4,11 +4,11 @@ module Persistence extend ActiveSupport::Concern class_methods do - def before_attributes(method = nil, only: [:create, :update], &blk) + def before_attributes(method = nil, only: [:create, :update, :assign], &blk) add_callback(:attributes, :before, method, only, &blk) end - def after_attributes(method = nil, only: [:create, :update], &blk) + def after_attributes(method = nil, only: [:create, :update, :assign], &blk) add_callback(:attributes, :after, method, only, &blk) end @@ -69,13 +69,13 @@ def add_callback(kind, lifecycle, method, only, &blk) end end - def assign(assign_params, meta = nil) + def assign(assign_params, meta = nil, action_name = nil) id = assign_params[:id] assign_params = assign_params.except(:id) model_instance = nil - run_callbacks :attributes, :assign, assign_params, meta do |params| - model_instance = if meta[:method] == :update && id + run_callbacks :attributes, action_name, assign_params, meta do |params| + model_instance = if action_name != :create && id self.class._find(id: id).data else call_with_meta(:build, model, meta) @@ -91,11 +91,7 @@ def create(create_params, meta = nil) model_instance = nil run_callbacks :persistence, :create, create_params, meta do - run_callbacks :attributes, :create, create_params, meta do |params| - model_instance = call_with_meta(:build, model, meta) - call_with_meta(:assign_attributes, model_instance, params, meta) - model_instance - end + model_instance = assign(create_params, meta, :create) run_callbacks :save, :create, model_instance, meta do model_instance = call_with_meta(:save, model_instance, meta) @@ -107,15 +103,9 @@ def create(create_params, meta = nil) def update(update_params, meta = nil) model_instance = nil - id = update_params[:id] - update_params = update_params.except(:id) run_callbacks :persistence, :update, update_params, meta do - run_callbacks :attributes, :update, update_params, meta do |params| - model_instance = self.class._find(id: id).data - call_with_meta(:assign_attributes, model_instance, params, meta) - model_instance - end + model_instance = assign(update_params, meta, :update) run_callbacks :save, :update, model_instance, meta do model_instance = call_with_meta(:save, model_instance, meta) diff --git a/lib/graphiti/resource_proxy.rb b/lib/graphiti/resource_proxy.rb index 87812dc3..a371399d 100644 --- a/lib/graphiti/resource_proxy.rb +++ b/lib/graphiti/resource_proxy.rb @@ -9,7 +9,8 @@ def initialize(resource, scope, query, single: false, raise_on_missing: false, cache: nil, - cache_expires_in: nil) + cache_expires_in: nil, + data: nil) @resource = resource @scope = scope @@ -74,6 +75,11 @@ def as_graphql(options = {}) Renderer.new(self, options).as_graphql end + def data=(models) + @data = data + [@data].flatten.compact.each { |r| @resource.decorate_record(r) } + end + def data @data ||= begin records = @scope.resolve @@ -123,7 +129,7 @@ def assign_attributes(params = nil) @data = @resource.assign_with_relationships( @payload.meta, @payload.attributes, - @payload.relationships, + @payload.relationships ) end @@ -177,7 +183,6 @@ def destroy def update resolve_data - assign_attributes save(action: :update) end diff --git a/lib/graphiti/runner.rb b/lib/graphiti/runner.rb index 66682763..2eb8b7b6 100644 --- a/lib/graphiti/runner.rb +++ b/lib/graphiti/runner.rb @@ -70,9 +70,16 @@ def proxy(base = nil, opts = {}) query, payload: deserialized_payload, single: opts[:single], +<<<<<<< HEAD raise_on_missing: opts[:raise_on_missing], cache: opts[:cache], cache_expires_in: opts[:cache_expires_in] +||||||| parent of c9ac1c6 (Ensure callbacks are run, add new :assign callback key for attributes callback) + raise_on_missing: opts[:raise_on_missing] +======= + raise_on_missing: opts[:raise_on_missing], + data: opts[:data] +>>>>>>> c9ac1c6 (Ensure callbacks are run, add new :assign callback key for attributes callback) end end end diff --git a/lib/graphiti/util/persistence.rb b/lib/graphiti/util/persistence.rb index 17c78d62..202e6b74 100644 --- a/lib/graphiti/util/persistence.rb +++ b/lib/graphiti/util/persistence.rb @@ -26,10 +26,10 @@ def initialize(resource, meta, attributes, relationships, caller_model, foreign_ def assign attributes = @adapter.persistence_attributes(self, @attributes) - @assigned = call_resource_method(:assign, attributes, @caller_model) - @resource.decorate_record(@assigned) + assigned = @resource.assign(attributes, @meta, :assign) + @resource.decorate_record(assigned) - @assigned + assigned end # Perform the actual save logic. @@ -58,8 +58,6 @@ def run persisted = persist_object(@meta[:method], attributes) @resource.decorate_record(persisted) - return persisted if @meta[:method] == :assign - assign_temp_id(persisted, @meta[:temp_id]) associate_parents(persisted, parents)