graphiti 1.0.alpha.25 → 1.0.alpha.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11c4f6ec02d7b26ada772125aa0a6f93adef149c
4
- data.tar.gz: ff61a24878530f72a9347dee1666e3c00daedb9d
3
+ metadata.gz: cc547267adde832aa6910fc38c5ee48af7f38ab5
4
+ data.tar.gz: 9b3b27ba180c2c1d6e1b599ed7302d5ca9b6dd54
5
5
  SHA512:
6
- metadata.gz: 9f39008d0879c9cae9fb68f4e259603783d873983cceb93d1fd1dfc424f9a62fdf2ad803b00229ed27fe8986d4eb6ab80ac34a2c23a5a80b730e1b85bc8e6c3f
7
- data.tar.gz: 95fd32a819cd3f40965c636dc3235c996cce23621d4881aca88f9e083801f52b3473094b637dc241b382a6573dd2ad76e8d5137384a8333c732b7b503c56871b
6
+ metadata.gz: 92f560e227cb8084807d503b85190f631daf696d22e4c649041573ad18c4553350b5525ab52f3d3bcf4270094fc5cd1f2141409de73609140c0c3bb651130b4a
7
+ data.tar.gz: fb1eb584414a97b7a3afb65fb5cf397a6504868c160e0dd4cd5d844bd9249af3047de4d1702888e787a8ff2452e2f66cde2367890e3c944c6311268181c1ffe1
@@ -447,17 +447,7 @@ module Graphiti
447
447
  raise 'you must override #update in an adapter subclass'
448
448
  end
449
449
 
450
- # @param [Class] model_class The configured model class (see Resource.model)
451
- # @param [Integer] id the id for this model
452
- # @return the model instance just destroyed
453
- # @see Resource.model
454
- # @example ActiveRecord default
455
- # def destroy(model_class, id)
456
- # instance = model_class.find(id)
457
- # instance.destroy
458
- # instance
459
- # end
460
- def destroy(model_class, id)
450
+ def destroy(model)
461
451
  raise 'you must override #destroy in an adapter subclass'
462
452
  end
463
453
 
@@ -200,14 +200,6 @@ module Graphiti
200
200
  end
201
201
  end
202
202
 
203
- def belongs_to_many_filter(sideload, scope, value)
204
- scope
205
- .includes(sideload.through_relationship_name)
206
- .where(sideload.through_table_name => {
207
- sideload.true_foreign_key => value
208
- })
209
- end
210
-
211
203
  def associate_all(parent, children, association_name, association_type)
212
204
  if activerecord_associate?(parent, children[0], association_name)
213
205
  association = parent.association(association_name)
@@ -264,11 +256,9 @@ module Graphiti
264
256
  instance
265
257
  end
266
258
 
267
- # (see Adapters::Abstract#destroy)
268
- def destroy(model_class, id)
269
- instance = model_class.find(id)
270
- instance.destroy
271
- instance
259
+ def destroy(model)
260
+ model.destroy
261
+ model
272
262
  end
273
263
  end
274
264
  end
@@ -8,10 +8,49 @@ class Graphiti::Adapters::ActiveRecord::ManyToManySideload < Graphiti::Sideload:
8
8
  foreign_key.keys.first
9
9
  end
10
10
 
11
- def infer_foreign_key
11
+ def belongs_to_many_filter(scope, value)
12
+ scope
13
+ .includes(through_relationship_name)
14
+ .where(belongs_to_many_clause(value))
15
+ end
16
+
17
+ private
18
+
19
+ def belongs_to_many_clause(value)
20
+ where = { true_foreign_key => value }.tap do |c|
21
+ if polymorphic?
22
+ c[foreign_type_column] = foreign_type_value
23
+ end
24
+ end
25
+
26
+ { through_table_name => where }
27
+ end
28
+
29
+ def foreign_type_column
30
+ through_reflection.type
31
+ end
32
+
33
+ def foreign_type_value
34
+ through_reflection.active_record.name
35
+ end
36
+
37
+ def polymorphic?
38
+ !!foreign_type_column
39
+ end
40
+
41
+ def through_reflection
42
+ through = parent_reflection.options[:through]
43
+ parent_resource_class.model.reflections[through.to_s]
44
+ end
45
+
46
+ def parent_reflection
12
47
  parent_model = parent_resource_class.model
13
- key = parent_model.reflections[name.to_s].options[:through]
14
- value = parent_model.reflections[key.to_s].foreign_key.to_sym
48
+ parent_model.reflections[name.to_s]
49
+ end
50
+
51
+ def infer_foreign_key
52
+ key = parent_reflection.options[:through]
53
+ value = through_reflection.foreign_key.to_sym
15
54
  { key => value }
16
55
  end
17
56
  end
@@ -16,6 +16,19 @@ The adapter #{@adapter.class} does not implement method '#{@method}', which was
16
16
  end
17
17
  end
18
18
 
19
+ class UnwritableRelationship < Base
20
+ def initialize(resource, sideload)
21
+ @resource = resource
22
+ @sideload = sideload
23
+ end
24
+
25
+ def message
26
+ <<-MSG
27
+ #{@resource.class}: Tried to persist sideload #{@sideload.name.inspect} but marked writable: false
28
+ MSG
29
+ end
30
+ end
31
+
19
32
  class SingularSideload < Base
20
33
  def initialize(sideload, parent_length)
21
34
  @sideload = sideload
@@ -88,8 +88,8 @@ module Graphiti
88
88
  adapter.update(model, update_params)
89
89
  end
90
90
 
91
- def destroy(id)
92
- adapter.destroy(model, id)
91
+ def destroy(model)
92
+ adapter.destroy(model)
93
93
  end
94
94
 
95
95
  def associate_all(parent, children, association_name, type)
@@ -17,6 +17,10 @@ module Graphiti
17
17
 
18
18
  def find(params = {}, base_scope = nil)
19
19
  validate!(params)
20
+ _find(params, base_scope)
21
+ end
22
+
23
+ def _find(params = {}, base_scope = nil)
20
24
  id = params[:data].try(:[], :id) || params.delete(:id)
21
25
  params[:filter] ||= {}
22
26
  params[:filter].merge!(id: id) if id
@@ -17,11 +17,15 @@ module Graphiti
17
17
  parent.children[name] = sideload
18
18
  else
19
19
  config[:sideloads][name] = sideload
20
- apply_sideloads_to_serializer if rails_autoloading?
20
+ apply_sideload_to_serializer(name) if eagerly_apply_sideload?(sideload)
21
21
  end
22
22
  sideload
23
23
  end
24
24
 
25
+ def apply_sideload_to_serializer(name)
26
+ Util::SerializerRelationships.new(self, config[:sideloads].slice(name)).apply
27
+ end
28
+
25
29
  def apply_sideloads_to_serializer
26
30
  Util::SerializerRelationships.new(self, config[:sideloads]).apply
27
31
  end
@@ -105,8 +109,9 @@ module Graphiti
105
109
  memo
106
110
  end
107
111
 
108
- def rails_autoloading?
109
- defined?(::Rails) && !::Rails.application.config.eager_load
112
+ def eagerly_apply_sideload?(sideload)
113
+ autoloading = defined?(::Rails) && !::Rails.application.config.eager_load
114
+ autoloading || sideload.resource_class_loaded?
110
115
  end
111
116
  end
112
117
  end
@@ -89,8 +89,9 @@ module Graphiti
89
89
  end
90
90
 
91
91
  def destroy
92
+ record = data
92
93
  validator = @resource.transaction do
93
- model = @resource.destroy(@query.filters[:id])
94
+ model = @resource.destroy(record)
94
95
  model.instance_variable_set(:@__serializer_klass, @resource.serializer)
95
96
  validator = ::Graphiti::Util::ValidationResponse.new \
96
97
  model, @payload
@@ -181,8 +181,8 @@ module Graphiti
181
181
  end
182
182
 
183
183
  if relevant_children.is_a?(Array)
184
- associated |= relevant_children if track_associated
185
- associate_all(parent, relevant_children)
184
+ associated |= relevant_children if track_associated
185
+ associate_all(parent, relevant_children)
186
186
  else
187
187
  associated << relevant_children if track_associated && relevant_children
188
188
  associate(parent, relevant_children)
@@ -278,7 +278,7 @@ module Graphiti
278
278
  fk_type = parent_resource_class.attributes[:id][:type]
279
279
  resource_class.filter true_foreign_key, fk_type do
280
280
  eq do |scope, value|
281
- _self.resource.adapter.belongs_to_many_filter(_self, scope, value)
281
+ _self.belongs_to_many_filter(scope, value)
282
282
  end
283
283
  end
284
284
  end
@@ -149,7 +149,8 @@ class Graphiti::Util::Persistence
149
149
  def persist_object(method, attributes)
150
150
  case method
151
151
  when :destroy
152
- call_resource_method(:destroy, attributes[:id], @caller_model)
152
+ model = @resource.class._find(attributes.slice(:id)).data
153
+ call_resource_method(:destroy, model, @caller_model)
153
154
  when :update, nil, :disassociate
154
155
  call_resource_method(:update, attributes, @caller_model)
155
156
  else
@@ -172,8 +173,12 @@ class Graphiti::Util::Persistence
172
173
  def process_belongs_to(relationships)
173
174
  [].tap do |processed|
174
175
  iterate(only: [:polymorphic_belongs_to, :belongs_to]) do |x|
175
- x[:object] = x[:sideload].resource
176
- .persist_with_relationships(x[:meta], x[:attributes], x[:relationships])
176
+ if x[:sideload].writable?
177
+ x[:object] = x[:sideload].resource
178
+ .persist_with_relationships(x[:meta], x[:attributes], x[:relationships])
179
+ else
180
+ raise Graphiti::Errors::UnwritableRelationship.new(@resource, x[:sideload])
181
+ end
177
182
  processed << x
178
183
  end
179
184
  end
@@ -62,7 +62,7 @@ module Graphiti
62
62
  def data_proc
63
63
  _sl = @sideload
64
64
  ->(_) {
65
- if records = @object.public_send(_sl.name)
65
+ if records = @object.public_send(_sl.association_name)
66
66
  if records.respond_to?(:to_ary)
67
67
  records.each { |r| _sl.resource.decorate_record(r) }
68
68
  else
@@ -1,3 +1,3 @@
1
1
  module Graphiti
2
- VERSION = "1.0.alpha.25"
2
+ VERSION = "1.0.alpha.26"
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.alpha.25
4
+ version: 1.0.alpha.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-12 00:00:00.000000000 Z
11
+ date: 2018-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializable