Skip to content

Commit

Permalink
Ensure callbacks are run, add new :assign callback key for attributes…
Browse files Browse the repository at this point in the history
… callback
  • Loading branch information
jkeen committed Mar 27, 2024
1 parent 11358b7 commit 5d66f76
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
9 changes: 8 additions & 1 deletion lib/graphiti/resource/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 7 additions & 17 deletions lib/graphiti/resource/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions lib/graphiti/resource_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -123,7 +129,7 @@ def assign_attributes(params = nil)
@data = @resource.assign_with_relationships(
@payload.meta,
@payload.attributes,
@payload.relationships,
@payload.relationships
)
end

Expand Down Expand Up @@ -177,7 +183,6 @@ def destroy

def update
resolve_data
assign_attributes
save(action: :update)
end

Expand Down
7 changes: 7 additions & 0 deletions lib/graphiti/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 3 additions & 5 deletions lib/graphiti/util/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 5d66f76

Please sign in to comment.