graphiti 1.0.rc.15 → 1.0.rc.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Appraisals +6 -0
- data/gemfiles/rails_4.gemfile +3 -0
- data/gemfiles/rails_5.gemfile +3 -0
- data/graphiti.gemspec +0 -3
- data/lib/generators/graphiti/templates/create_request_spec.rb.erb +11 -3
- data/lib/generators/graphiti/templates/resource_writes_spec.rb.erb +6 -1
- data/lib/graphiti/delegates/pagination.rb +6 -1
- data/lib/graphiti/resource.rb +5 -3
- data/lib/graphiti/resource/dsl.rb +2 -1
- data/lib/graphiti/resource/persistence.rb +39 -29
- data/lib/graphiti/resource/sideloading.rb +2 -1
- data/lib/graphiti/resource_proxy.rb +3 -2
- data/lib/graphiti/util/link.rb +8 -4
- data/lib/graphiti/util/persistence.rb +15 -14
- data/lib/graphiti/version.rb +1 -1
- metadata +2 -50
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d46aa4ca6068b172194920f8e4a01cbf1fb2bc4b
|
4
|
+
data.tar.gz: 753ed53b934af42504e07688e2c0158c350de890
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc840114e74811c060a3ebedc9e14f6f5a316020e285c058991e02223cabed0f3cf1b079c04a693b8690e578aa6c4c30e48abd57fad1a6af8090f514547b8be6
|
7
|
+
data.tar.gz: 5d21cda6032b24a26f11e40357f73731038e357903dc464ee77a1aee2cbc50d1ce25073fd63a517f5554143be3b430efdc2f900d0fd3676112243472b70f9519
|
data/Appraisals
CHANGED
@@ -2,10 +2,16 @@ appraise "rails-4" do
|
|
2
2
|
gem "rails", "~> 4.1"
|
3
3
|
gem 'jsonapi-rails', '~> 0.3.1', require: 'jsonapi/rails'
|
4
4
|
gem 'rspec-rails'
|
5
|
+
gem 'sqlite3', '~> 1.3.6'
|
6
|
+
gem 'database_cleaner'
|
7
|
+
gem 'kaminari', '~> 0.17'
|
5
8
|
end
|
6
9
|
|
7
10
|
appraise "rails-5" do
|
8
11
|
gem "rails", "~> 5.2"
|
9
12
|
gem 'jsonapi-rails', '~> 0.3.1', require: 'jsonapi/rails'
|
10
13
|
gem 'rspec-rails'
|
14
|
+
gem 'sqlite3', '~> 1.3.6'
|
15
|
+
gem 'database_cleaner'
|
16
|
+
gem 'kaminari', '~> 0.17'
|
11
17
|
end
|
data/gemfiles/rails_4.gemfile
CHANGED
data/gemfiles/rails_5.gemfile
CHANGED
data/graphiti.gemspec
CHANGED
@@ -26,12 +26,9 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_dependency 'activesupport', ['>= 4.1', '< 6']
|
27
27
|
|
28
28
|
spec.add_development_dependency "faraday", '~> 0.15'
|
29
|
-
spec.add_development_dependency "activerecord", ['>= 4.1', '< 6']
|
30
29
|
spec.add_development_dependency "kaminari", '~> 0.17'
|
31
30
|
spec.add_development_dependency "bundler"
|
32
31
|
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
-
spec.add_development_dependency "sqlite3"
|
34
|
-
spec.add_development_dependency "database_cleaner"
|
35
32
|
spec.add_development_dependency "activemodel", ['>= 4.1', '< 6']
|
36
33
|
spec.add_development_dependency "graphiti_spec_helpers", '1.0.beta.4'
|
37
34
|
end
|
@@ -6,13 +6,21 @@ RSpec.describe "<%= type %>#create", type: :request do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
describe 'basic create' do
|
9
|
+
let(:params) do
|
10
|
+
<%- if defined?(FactoryBot) -%>
|
11
|
+
attributes_for(:<%= type.to_s.singularize %>).
|
12
|
+
except("created_at", "updated_at")
|
13
|
+
<%- else -%>
|
14
|
+
{
|
15
|
+
# ... your attrs here
|
16
|
+
}
|
17
|
+
<%- end -%>
|
18
|
+
end
|
9
19
|
let(:payload) do
|
10
20
|
{
|
11
21
|
data: {
|
12
22
|
type: '<%= type %>',
|
13
|
-
attributes:
|
14
|
-
# ... your attrs here
|
15
|
-
}
|
23
|
+
attributes: params
|
16
24
|
}
|
17
25
|
}
|
18
26
|
end
|
@@ -6,7 +6,12 @@ RSpec.describe <%= resource_class %>, type: :resource do
|
|
6
6
|
{
|
7
7
|
data: {
|
8
8
|
type: '<%= type %>',
|
9
|
+
<%- if defined?(FactoryBot) -%>
|
10
|
+
attributes: attributes_for(:<%= type.to_s.singularize %>).
|
11
|
+
except("created_at", "updated_at")
|
12
|
+
<%- else -%>
|
9
13
|
attributes: { }
|
14
|
+
<%- end -%>
|
10
15
|
}
|
11
16
|
}
|
12
17
|
end
|
@@ -17,7 +22,7 @@ RSpec.describe <%= resource_class %>, type: :resource do
|
|
17
22
|
|
18
23
|
it 'works' do
|
19
24
|
expect {
|
20
|
-
expect(instance.save).to eq(true)
|
25
|
+
expect(instance.save).to eq(true), instance.errors.full_messages.to_sentence
|
21
26
|
}.to change { <%= model_class %>.count }.by(1)
|
22
27
|
end
|
23
28
|
end
|
@@ -36,7 +36,11 @@ module Graphiti
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def last_page
|
39
|
-
|
39
|
+
if @last_page
|
40
|
+
return @last_page
|
41
|
+
elsif page_size == 0 || item_count == 0
|
42
|
+
return nil
|
43
|
+
end
|
40
44
|
@last_page = (item_count / page_size)
|
41
45
|
@last_page += 1 if item_count % page_size > 0
|
42
46
|
@last_page
|
@@ -57,6 +61,7 @@ module Graphiti
|
|
57
61
|
# Graphiti.logger.warn(e.message)
|
58
62
|
@item_count = 0
|
59
63
|
end
|
64
|
+
@item_count
|
60
65
|
end
|
61
66
|
|
62
67
|
def current_page
|
data/lib/graphiti/resource.rb
CHANGED
@@ -114,9 +114,11 @@ module Graphiti
|
|
114
114
|
adapter.resolve(scope)
|
115
115
|
end
|
116
116
|
|
117
|
-
def before_commit(model,
|
118
|
-
|
119
|
-
|
117
|
+
def before_commit(model, metadata)
|
118
|
+
hooks = self.class.config[:before_commit][metadata[:method]] || []
|
119
|
+
hooks.each do |hook|
|
120
|
+
instance_exec(model, metadata, &hook)
|
121
|
+
end
|
120
122
|
end
|
121
123
|
|
122
124
|
def transaction
|
@@ -69,64 +69,64 @@ module Graphiti
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
def create(create_params)
|
72
|
+
def create(create_params, meta = nil)
|
73
73
|
model_instance = nil
|
74
74
|
|
75
|
-
run_callbacks :persistence, :create, create_params do
|
76
|
-
run_callbacks :attributes, :create, create_params do |params|
|
77
|
-
model_instance = build
|
78
|
-
assign_attributes
|
75
|
+
run_callbacks :persistence, :create, create_params, meta do
|
76
|
+
run_callbacks :attributes, :create, create_params, meta do |params|
|
77
|
+
model_instance = call_with_meta(:build, model, meta)
|
78
|
+
call_with_meta(:assign_attributes, model_instance, params, meta)
|
79
79
|
model_instance
|
80
80
|
end
|
81
81
|
|
82
|
-
run_callbacks :save, :create, model_instance do
|
83
|
-
model_instance = save
|
82
|
+
run_callbacks :save, :create, model_instance, meta do
|
83
|
+
model_instance = call_with_meta(:save, model_instance, meta)
|
84
84
|
end
|
85
85
|
|
86
86
|
model_instance
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
def update(update_params)
|
90
|
+
def update(update_params, meta = nil)
|
91
91
|
model_instance = nil
|
92
92
|
id = update_params.delete(:id)
|
93
93
|
|
94
|
-
run_callbacks :persistence, :update, update_params do
|
95
|
-
run_callbacks :attributes, :update, update_params do |params|
|
94
|
+
run_callbacks :persistence, :update, update_params, meta do
|
95
|
+
run_callbacks :attributes, :update, update_params, meta do |params|
|
96
96
|
model_instance = self.class._find(params.merge(id: id)).data
|
97
|
-
assign_attributes
|
97
|
+
call_with_meta(:assign_attributes, model_instance, params, meta)
|
98
98
|
model_instance
|
99
99
|
end
|
100
100
|
|
101
|
-
run_callbacks :save, :update, model_instance do
|
102
|
-
model_instance = save
|
101
|
+
run_callbacks :save, :update, model_instance, meta do
|
102
|
+
model_instance = call_with_meta(:save, model_instance, meta)
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
106
|
model_instance
|
107
107
|
end
|
108
108
|
|
109
|
-
def destroy(id)
|
109
|
+
def destroy(id, meta = nil)
|
110
110
|
model_instance = self.class._find(id: id).data
|
111
|
-
run_callbacks :destroy, :destroy, model_instance do
|
112
|
-
delete
|
111
|
+
run_callbacks :destroy, :destroy, model_instance, meta do
|
112
|
+
call_with_meta(:delete, model_instance, meta)
|
113
113
|
end
|
114
114
|
model_instance
|
115
115
|
end
|
116
116
|
|
117
|
-
def build(model)
|
117
|
+
def build(model, meta = nil)
|
118
118
|
adapter.build(model)
|
119
119
|
end
|
120
120
|
|
121
|
-
def assign_attributes(model_instance, update_params)
|
121
|
+
def assign_attributes(model_instance, update_params, meta = nil)
|
122
122
|
adapter.assign_attributes(model_instance, update_params)
|
123
123
|
end
|
124
124
|
|
125
|
-
def save(model_instance)
|
125
|
+
def save(model_instance, meta = nil)
|
126
126
|
adapter.save(model_instance)
|
127
127
|
end
|
128
128
|
|
129
|
-
def delete(model_instance)
|
129
|
+
def delete(model_instance, meta = nil)
|
130
130
|
adapter.destroy(model_instance)
|
131
131
|
end
|
132
132
|
|
@@ -136,7 +136,7 @@ module Graphiti
|
|
136
136
|
fire_around_callbacks(kind, action, *args) do |*yieldargs|
|
137
137
|
fire_callbacks(kind, :before, action, *yieldargs)
|
138
138
|
result = yield(*yieldargs)
|
139
|
-
fire_callbacks(kind, :after, action, result)
|
139
|
+
fire_callbacks(kind, :after, action, *[result, args.last])
|
140
140
|
result
|
141
141
|
end
|
142
142
|
end
|
@@ -147,16 +147,24 @@ module Graphiti
|
|
147
147
|
callbacks.each do |config|
|
148
148
|
callback = config[:callback]
|
149
149
|
next unless config[:only].include?(action)
|
150
|
-
|
151
|
-
if callback.respond_to?(:call)
|
152
|
-
instance_exec(*args, &callback)
|
153
|
-
else
|
154
|
-
send(callback, *args)
|
155
|
-
end
|
150
|
+
call_with_meta(callback, *args)
|
156
151
|
end
|
157
152
|
end
|
158
153
|
end
|
159
154
|
|
155
|
+
# Convenience for calling a method or proc
|
156
|
+
# while taking into account 'meta' is an optional argument
|
157
|
+
def call_with_meta(callback, *args)
|
158
|
+
if callback.respond_to?(:call)
|
159
|
+
instance_exec(*args, &callback)
|
160
|
+
else
|
161
|
+
arity = method(callback).arity
|
162
|
+
args = args[0..0] if arity == 1
|
163
|
+
args = args[0..1] if arity == 2
|
164
|
+
send(callback, *args)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
160
168
|
def fire_around_callbacks(kind, action, *args, &blk)
|
161
169
|
callbacks = self.class.config[:callbacks][kind].try(:[], :around) || []
|
162
170
|
callbacks = callbacks.select { |cb| cb[:only].include?(action) }
|
@@ -177,6 +185,7 @@ module Graphiti
|
|
177
185
|
if callbacks[index + 1]
|
178
186
|
proc do
|
179
187
|
r = nil
|
188
|
+
args = args[0..0] if method(method_name).arity == 1
|
180
189
|
send(method_name, *args) do |r2|
|
181
190
|
wrapped = around_callback_proc(callbacks, index+1, r2, &blk)
|
182
191
|
r = instance_exec(r2, &wrapped)
|
@@ -184,9 +193,10 @@ module Graphiti
|
|
184
193
|
r
|
185
194
|
end
|
186
195
|
else
|
187
|
-
proc do |
|
196
|
+
proc do |*args2|
|
188
197
|
r = nil
|
189
|
-
|
198
|
+
args2 = args2[0..0] if method(callbacks[index][:callback]).arity == 1
|
199
|
+
send(callbacks[index][:callback], *args2) do |r2|
|
190
200
|
r = instance_exec(r2, &blk)
|
191
201
|
end
|
192
202
|
r
|
@@ -61,7 +61,8 @@ module Graphiti
|
|
61
61
|
allow_sideload(name, opts, &blk)
|
62
62
|
end
|
63
63
|
|
64
|
-
def polymorphic_has_many(name, opts = {},
|
64
|
+
def polymorphic_has_many(name, opts = {}, &blk)
|
65
|
+
as = opts.delete(:as)
|
65
66
|
opts[:foreign_key] ||= :"#{as}_id"
|
66
67
|
opts[:polymorphic_as] ||= as
|
67
68
|
_model = model
|
@@ -115,12 +115,13 @@ module Graphiti
|
|
115
115
|
|
116
116
|
def destroy
|
117
117
|
validator = @resource.transaction do
|
118
|
-
|
118
|
+
metadata = { method: :destroy }
|
119
|
+
model = @resource.destroy(@query.filters[:id], metadata)
|
119
120
|
model.instance_variable_set(:@__serializer_klass, @resource.serializer)
|
120
121
|
validator = ::Graphiti::Util::ValidationResponse.new \
|
121
122
|
model, @payload
|
122
123
|
validator.validate!
|
123
|
-
@resource.before_commit(model,
|
124
|
+
@resource.before_commit(model, metadata)
|
124
125
|
validator
|
125
126
|
end
|
126
127
|
@data, success = validator.to_a
|
data/lib/graphiti/util/link.rb
CHANGED
@@ -4,11 +4,15 @@ module Graphiti
|
|
4
4
|
def initialize(sideload, model)
|
5
5
|
@sideload = sideload
|
6
6
|
@model = model
|
7
|
+
@linkable = true
|
7
8
|
|
8
9
|
if @sideload.type == :polymorphic_belongs_to
|
9
|
-
type = @model.send(@sideload.grouper.field_name)
|
10
|
-
|
11
|
-
|
10
|
+
if type = @model.send(@sideload.grouper.field_name)
|
11
|
+
@sideload = @sideload.children.values.find do |c|
|
12
|
+
c.group_name == type.to_sym
|
13
|
+
end
|
14
|
+
else
|
15
|
+
@linkable = false
|
12
16
|
end
|
13
17
|
end
|
14
18
|
end
|
@@ -25,7 +29,7 @@ module Graphiti
|
|
25
29
|
if @sideload.type == :belongs_to
|
26
30
|
not @model.send(@sideload.foreign_key).nil?
|
27
31
|
else
|
28
|
-
|
32
|
+
@linkable
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
@@ -61,7 +61,7 @@ class Graphiti::Util::Persistence
|
|
61
61
|
|
62
62
|
post_process(persisted, parents)
|
63
63
|
post_process(persisted, children)
|
64
|
-
before_commit = -> { @resource.before_commit(persisted,
|
64
|
+
before_commit = -> { @resource.before_commit(persisted, metadata) }
|
65
65
|
add_hook(before_commit)
|
66
66
|
persisted
|
67
67
|
end
|
@@ -218,22 +218,23 @@ class Graphiti::Util::Persistence
|
|
218
218
|
end
|
219
219
|
end
|
220
220
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
221
|
+
def metadata
|
222
|
+
{
|
223
|
+
method: @meta[:method],
|
224
|
+
temp_id: @meta[:temp_id],
|
225
|
+
caller_model: @caller_model,
|
226
|
+
attributes: @attributes,
|
227
|
+
relationships: @relationships
|
228
|
+
}
|
229
|
+
end
|
230
|
+
|
231
231
|
def call_resource_method(method_name, attributes, caller_model)
|
232
232
|
method = @resource.method(method_name)
|
233
|
-
|
234
|
-
|
235
|
-
else
|
233
|
+
|
234
|
+
if method.arity == 1
|
236
235
|
method.call(attributes)
|
236
|
+
else
|
237
|
+
method.call(attributes, metadata)
|
237
238
|
end
|
238
239
|
end
|
239
240
|
end
|
data/lib/graphiti/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphiti
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.rc.
|
4
|
+
version: 1.0.rc.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lee Richmond
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jsonapi-serializable
|
@@ -100,26 +100,6 @@ dependencies:
|
|
100
100
|
- - "~>"
|
101
101
|
- !ruby/object:Gem::Version
|
102
102
|
version: '0.15'
|
103
|
-
- !ruby/object:Gem::Dependency
|
104
|
-
name: activerecord
|
105
|
-
requirement: !ruby/object:Gem::Requirement
|
106
|
-
requirements:
|
107
|
-
- - ">="
|
108
|
-
- !ruby/object:Gem::Version
|
109
|
-
version: '4.1'
|
110
|
-
- - "<"
|
111
|
-
- !ruby/object:Gem::Version
|
112
|
-
version: '6'
|
113
|
-
type: :development
|
114
|
-
prerelease: false
|
115
|
-
version_requirements: !ruby/object:Gem::Requirement
|
116
|
-
requirements:
|
117
|
-
- - ">="
|
118
|
-
- !ruby/object:Gem::Version
|
119
|
-
version: '4.1'
|
120
|
-
- - "<"
|
121
|
-
- !ruby/object:Gem::Version
|
122
|
-
version: '6'
|
123
103
|
- !ruby/object:Gem::Dependency
|
124
104
|
name: kaminari
|
125
105
|
requirement: !ruby/object:Gem::Requirement
|
@@ -162,34 +142,6 @@ dependencies:
|
|
162
142
|
- - "~>"
|
163
143
|
- !ruby/object:Gem::Version
|
164
144
|
version: '10.0'
|
165
|
-
- !ruby/object:Gem::Dependency
|
166
|
-
name: sqlite3
|
167
|
-
requirement: !ruby/object:Gem::Requirement
|
168
|
-
requirements:
|
169
|
-
- - ">="
|
170
|
-
- !ruby/object:Gem::Version
|
171
|
-
version: '0'
|
172
|
-
type: :development
|
173
|
-
prerelease: false
|
174
|
-
version_requirements: !ruby/object:Gem::Requirement
|
175
|
-
requirements:
|
176
|
-
- - ">="
|
177
|
-
- !ruby/object:Gem::Version
|
178
|
-
version: '0'
|
179
|
-
- !ruby/object:Gem::Dependency
|
180
|
-
name: database_cleaner
|
181
|
-
requirement: !ruby/object:Gem::Requirement
|
182
|
-
requirements:
|
183
|
-
- - ">="
|
184
|
-
- !ruby/object:Gem::Version
|
185
|
-
version: '0'
|
186
|
-
type: :development
|
187
|
-
prerelease: false
|
188
|
-
version_requirements: !ruby/object:Gem::Requirement
|
189
|
-
requirements:
|
190
|
-
- - ">="
|
191
|
-
- !ruby/object:Gem::Version
|
192
|
-
version: '0'
|
193
145
|
- !ruby/object:Gem::Dependency
|
194
146
|
name: activemodel
|
195
147
|
requirement: !ruby/object:Gem::Requirement
|