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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a17652c7e2b5ef86f6c496038c6de48b0411f6a4
4
- data.tar.gz: 6135de42df9f8f4144b1bdd4b514c385e462ecd1
3
+ metadata.gz: d46aa4ca6068b172194920f8e4a01cbf1fb2bc4b
4
+ data.tar.gz: 753ed53b934af42504e07688e2c0158c350de890
5
5
  SHA512:
6
- metadata.gz: 6e502e58dbc078b48b01720d59394609078b5bedd0cbd58c42b86292323913f1ee5f2a269299c566380d861bdda508fb80c71a9445326d96c2e13fcffb2bf88f
7
- data.tar.gz: 619c67bb1648568d79d5a8a56449248dfe68a39f08c2a6d892cac150f6657aba38bc5c07f27327608714428c201a2f38dc0fcb0b356a3a339fabfa17c70edcfd
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
@@ -5,6 +5,9 @@ source "https://rubygems.org"
5
5
  gem "rails", "~> 4.1"
6
6
  gem "jsonapi-rails", "~> 0.3.1", require: "jsonapi/rails"
7
7
  gem "rspec-rails"
8
+ gem "sqlite3", "~> 1.3.6"
9
+ gem "database_cleaner"
10
+ gem "kaminari", "~> 0.17"
8
11
 
9
12
  group :test do
10
13
  gem "pry"
@@ -5,6 +5,9 @@ source "https://rubygems.org"
5
5
  gem "rails", "~> 5.2"
6
6
  gem "jsonapi-rails", "~> 0.3.1", require: "jsonapi/rails"
7
7
  gem "rspec-rails"
8
+ gem "sqlite3", "~> 1.3.6"
9
+ gem "database_cleaner"
10
+ gem "kaminari", "~> 0.17"
8
11
 
9
12
  group :test do
10
13
  gem "pry"
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
- return @last_page if @last_page || page_size == 0
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
@@ -114,9 +114,11 @@ module Graphiti
114
114
  adapter.resolve(scope)
115
115
  end
116
116
 
117
- def before_commit(model, method)
118
- hook = self.class.config[:before_commit][method]
119
- hook.call(model) if hook
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
@@ -82,7 +82,8 @@ module Graphiti
82
82
 
83
83
  def before_commit(only: [:create, :update, :destroy], &blk)
84
84
  Array(only).each do |verb|
85
- config[:before_commit][verb] = blk
85
+ config[:before_commit][verb] ||= []
86
+ config[:before_commit][verb] << blk
86
87
  end
87
88
  end
88
89
 
@@ -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(model)
78
- assign_attributes(model_instance, params)
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(model_instance)
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(model_instance, params)
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(model_instance)
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(model_instance)
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 |result|
196
+ proc do |*args2|
188
197
  r = nil
189
- send(callbacks[index][:callback], result) do |r2|
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 = {}, as:, &blk)
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
- model = @resource.destroy(@query.filters[:id])
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, :destroy)
124
+ @resource.before_commit(model, metadata)
124
125
  validator
125
126
  end
126
127
  @data, success = validator.to_a
@@ -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
- @sideload = @sideload.children.values.find do |c|
11
- c.group_name == type.to_sym
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
- true
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, @meta[:method]) }
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
- # In the Resource, we want to allow:
222
- #
223
- # def create(attrs)
224
- #
225
- # and
226
- #
227
- # def create(attrs, parent = nil)
228
- #
229
- # 'parent' is an optional parameter that should not be part of the
230
- # method signature in most use cases.
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
- if [2,-2].include?(method.arity)
234
- method.call(attributes, caller_model)
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
@@ -1,3 +1,3 @@
1
1
  module Graphiti
2
- VERSION = "1.0.rc.15"
2
+ VERSION = "1.0.rc.16"
3
3
  end
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.15
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-04 00:00:00.000000000 Z
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