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 Feb 27, 2024
1 parent e06783b commit 5f1e1ce
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 33 deletions.
17 changes: 12 additions & 5 deletions lib/graphiti/resource/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Interface

class_methods do
def all(params = {}, base_scope = nil)
validate!(params)
validate_request!(params)
_all(params, {}, base_scope)
end

Expand All @@ -17,7 +17,7 @@ def _all(params, opts, base_scope)
end

def find(params = {}, base_scope = nil)
validate!(params)
validate_request!(params)
_find(params, base_scope)
end

Expand All @@ -38,16 +38,23 @@ def _find(params = {}, base_scope = nil)
end

def build(params, base_scope = nil)
validate!(params)
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

private

def validate!(params)
def validate_request!(params)
return if Graphiti.context[:graphql] || !validate_endpoints?

if context&.respond_to?(:request)
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
12 changes: 9 additions & 3 deletions lib/graphiti/resource_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class ResourceProxy
def initialize(resource, scope, query,
payload: nil,
single: false,
raise_on_missing: false)
raise_on_missing: false,
data: nil)

@resource = resource
@scope = scope
@query = query
Expand Down Expand Up @@ -63,6 +65,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 @@ -113,7 +120,7 @@ def assign_attributes(params = nil)
@data = @resource.assign_with_relationships(
@payload.meta,
@payload.attributes,
@payload.relationships,
@payload.relationships
)
end

Expand Down Expand Up @@ -167,7 +174,6 @@ def destroy

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

Expand Down
3 changes: 2 additions & 1 deletion lib/graphiti/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def proxy(base = nil, opts = {})
query,
payload: deserialized_payload,
single: opts[:single],
raise_on_missing: opts[:raise_on_missing]
raise_on_missing: opts[:raise_on_missing],
data: opts[:data]
end
end
end
5 changes: 3 additions & 2 deletions lib/graphiti/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ def strip_relationships!(hash)
end

def strip_relationships?
return false unless Graphiti.config.links_on_demand
params = Graphiti.context[:object].params || {}
return false unless Graphiti.config.links_on_demand

params = Graphiti.context[:object].try(:params) || {}
[false, nil, "false"].include?(params[:links])
end
end
Expand Down
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 5f1e1ce

Please sign in to comment.