Skip to content

Commit 5f1e1ce

Browse files
committed
Ensure callbacks are run, add new :assign callback key for attributes callback
1 parent e06783b commit 5f1e1ce

File tree

6 files changed

+36
-33
lines changed

6 files changed

+36
-33
lines changed

lib/graphiti/resource/interface.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Interface
55

66
class_methods do
77
def all(params = {}, base_scope = nil)
8-
validate!(params)
8+
validate_request!(params)
99
_all(params, {}, base_scope)
1010
end
1111

@@ -17,7 +17,7 @@ def _all(params, opts, base_scope)
1717
end
1818

1919
def find(params = {}, base_scope = nil)
20-
validate!(params)
20+
validate_request!(params)
2121
_find(params, base_scope)
2222
end
2323

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

4040
def build(params, base_scope = nil)
41-
validate!(params)
41+
validate_request!(params)
4242
runner = Runner.new(self, params)
4343
runner.proxy(base_scope, single: true, raise_on_missing: true).tap do |instance|
44-
instance.assign_attributes # assign the params to the underlying model
44+
instance.assign_attributes(params) # assign the params to the underlying model
45+
end
46+
end
47+
48+
def load(models, base_scope = nil)
49+
runner = Runner.new(self, {}, base_scope, :find)
50+
runner.proxy(nil, bypass_required_filters: true).tap do |r|
51+
r.data = models
4552
end
4653
end
4754

4855
private
4956

50-
def validate!(params)
57+
def validate_request!(params)
5158
return if Graphiti.context[:graphql] || !validate_endpoints?
5259

5360
if context&.respond_to?(:request)

lib/graphiti/resource/persistence.rb

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ module Persistence
44
extend ActiveSupport::Concern
55

66
class_methods do
7-
def before_attributes(method = nil, only: [:create, :update], &blk)
7+
def before_attributes(method = nil, only: [:create, :update, :assign], &blk)
88
add_callback(:attributes, :before, method, only, &blk)
99
end
1010

11-
def after_attributes(method = nil, only: [:create, :update], &blk)
11+
def after_attributes(method = nil, only: [:create, :update, :assign], &blk)
1212
add_callback(:attributes, :after, method, only, &blk)
1313
end
1414

@@ -69,13 +69,13 @@ def add_callback(kind, lifecycle, method, only, &blk)
6969
end
7070
end
7171

72-
def assign(assign_params, meta = nil)
72+
def assign(assign_params, meta = nil, action_name = nil)
7373
id = assign_params[:id]
7474
assign_params = assign_params.except(:id)
7575
model_instance = nil
7676

77-
run_callbacks :attributes, :assign, assign_params, meta do |params|
78-
model_instance = if meta[:method] == :update && id
77+
run_callbacks :attributes, action_name, assign_params, meta do |params|
78+
model_instance = if action_name != :create && id
7979
self.class._find(id: id).data
8080
else
8181
call_with_meta(:build, model, meta)
@@ -91,11 +91,7 @@ def create(create_params, meta = nil)
9191
model_instance = nil
9292

9393
run_callbacks :persistence, :create, create_params, meta do
94-
run_callbacks :attributes, :create, create_params, meta do |params|
95-
model_instance = call_with_meta(:build, model, meta)
96-
call_with_meta(:assign_attributes, model_instance, params, meta)
97-
model_instance
98-
end
94+
model_instance = assign(create_params, meta, :create)
9995

10096
run_callbacks :save, :create, model_instance, meta do
10197
model_instance = call_with_meta(:save, model_instance, meta)
@@ -107,15 +103,9 @@ def create(create_params, meta = nil)
107103

108104
def update(update_params, meta = nil)
109105
model_instance = nil
110-
id = update_params[:id]
111-
update_params = update_params.except(:id)
112106

113107
run_callbacks :persistence, :update, update_params, meta do
114-
run_callbacks :attributes, :update, update_params, meta do |params|
115-
model_instance = self.class._find(id: id).data
116-
call_with_meta(:assign_attributes, model_instance, params, meta)
117-
model_instance
118-
end
108+
model_instance = assign(update_params, meta, :update)
119109

120110
run_callbacks :save, :update, model_instance, meta do
121111
model_instance = call_with_meta(:save, model_instance, meta)

lib/graphiti/resource_proxy.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class ResourceProxy
77
def initialize(resource, scope, query,
88
payload: nil,
99
single: false,
10-
raise_on_missing: false)
10+
raise_on_missing: false,
11+
data: nil)
12+
1113
@resource = resource
1214
@scope = scope
1315
@query = query
@@ -63,6 +65,11 @@ def as_graphql(options = {})
6365
Renderer.new(self, options).as_graphql
6466
end
6567

68+
def data=(models)
69+
@data = data
70+
[@data].flatten.compact.each { |r| @resource.decorate_record(r) }
71+
end
72+
6673
def data
6774
@data ||= begin
6875
records = @scope.resolve
@@ -113,7 +120,7 @@ def assign_attributes(params = nil)
113120
@data = @resource.assign_with_relationships(
114121
@payload.meta,
115122
@payload.attributes,
116-
@payload.relationships,
123+
@payload.relationships
117124
)
118125
end
119126

@@ -167,7 +174,6 @@ def destroy
167174

168175
def update
169176
resolve_data
170-
assign_attributes
171177
save(action: :update)
172178
end
173179

lib/graphiti/runner.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def proxy(base = nil, opts = {})
7070
query,
7171
payload: deserialized_payload,
7272
single: opts[:single],
73-
raise_on_missing: opts[:raise_on_missing]
73+
raise_on_missing: opts[:raise_on_missing],
74+
data: opts[:data]
7475
end
7576
end
7677
end

lib/graphiti/serializer.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ def strip_relationships!(hash)
9898
end
9999

100100
def strip_relationships?
101-
return false unless Graphiti.config.links_on_demand
102-
params = Graphiti.context[:object].params || {}
101+
return false unless Graphiti.config.links_on_demand
102+
103+
params = Graphiti.context[:object].try(:params) || {}
103104
[false, nil, "false"].include?(params[:links])
104105
end
105106
end

lib/graphiti/util/persistence.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ def initialize(resource, meta, attributes, relationships, caller_model, foreign_
2626

2727
def assign
2828
attributes = @adapter.persistence_attributes(self, @attributes)
29-
@assigned = call_resource_method(:assign, attributes, @caller_model)
30-
@resource.decorate_record(@assigned)
29+
assigned = @resource.assign(attributes, @meta, :assign)
30+
@resource.decorate_record(assigned)
3131

32-
@assigned
32+
assigned
3333
end
3434

3535
# Perform the actual save logic.
@@ -58,8 +58,6 @@ def run
5858
persisted = persist_object(@meta[:method], attributes)
5959
@resource.decorate_record(persisted)
6060

61-
return persisted if @meta[:method] == :assign
62-
6361
assign_temp_id(persisted, @meta[:temp_id])
6462
associate_parents(persisted, parents)
6563

0 commit comments

Comments
 (0)