jsonapi_compliable 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/Appraisals +4 -0
  3. data/Gemfile +2 -2
  4. data/gemfiles/rails_4.gemfile +4 -3
  5. data/gemfiles/rails_4.gemfile.lock +27 -36
  6. data/gemfiles/rails_5.gemfile +4 -3
  7. data/gemfiles/rails_5.gemfile.lock +27 -37
  8. data/jsonapi_compliable.gemspec +3 -7
  9. data/lib/jsonapi_compliable/adapters/abstract.rb +25 -0
  10. data/lib/jsonapi_compliable/adapters/active_record.rb +47 -0
  11. data/lib/jsonapi_compliable/adapters/active_record_sideloading.rb +119 -0
  12. data/lib/jsonapi_compliable/adapters/null.rb +21 -0
  13. data/lib/jsonapi_compliable/base.rb +43 -50
  14. data/lib/jsonapi_compliable/deserializable.rb +9 -4
  15. data/lib/jsonapi_compliable/extensions/extra_attribute.rb +1 -5
  16. data/lib/jsonapi_compliable/query.rb +153 -0
  17. data/lib/jsonapi_compliable/rails.rb +14 -0
  18. data/lib/jsonapi_compliable/resource.rb +191 -0
  19. data/lib/jsonapi_compliable/scope.rb +57 -0
  20. data/lib/jsonapi_compliable/{scope → scoping}/base.rb +5 -6
  21. data/lib/jsonapi_compliable/{scope → scoping}/default_filter.rb +3 -3
  22. data/lib/jsonapi_compliable/scoping/extra_fields.rb +25 -0
  23. data/lib/jsonapi_compliable/{scope → scoping}/filter.rb +4 -4
  24. data/lib/jsonapi_compliable/{scope → scoping}/filterable.rb +4 -4
  25. data/lib/jsonapi_compliable/{scope → scoping}/paginate.rb +6 -6
  26. data/lib/jsonapi_compliable/scoping/sort.rb +33 -0
  27. data/lib/jsonapi_compliable/sideload.rb +95 -0
  28. data/lib/jsonapi_compliable/stats/dsl.rb +7 -16
  29. data/lib/jsonapi_compliable/stats/payload.rb +6 -14
  30. data/lib/jsonapi_compliable/util/field_params.rb +6 -8
  31. data/lib/jsonapi_compliable/util/hash.rb +14 -0
  32. data/lib/jsonapi_compliable/util/include_params.rb +7 -18
  33. data/lib/jsonapi_compliable/util/render_options.rb +25 -0
  34. data/lib/jsonapi_compliable/version.rb +1 -1
  35. data/lib/jsonapi_compliable.rb +22 -13
  36. metadata +34 -70
  37. data/gemfiles/rails_3.gemfile +0 -9
  38. data/lib/jsonapi_compliable/dsl.rb +0 -90
  39. data/lib/jsonapi_compliable/scope/extra_fields.rb +0 -35
  40. data/lib/jsonapi_compliable/scope/sideload.rb +0 -25
  41. data/lib/jsonapi_compliable/scope/sort.rb +0 -29
  42. data/lib/jsonapi_compliable/util/pagination.rb +0 -11
  43. data/lib/jsonapi_compliable/util/scoping.rb +0 -20
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06adc2e0a9833b1556d62814a1f88c81a4beac2e
4
- data.tar.gz: 53d1f7f2d515a1c9dd18fe02f0ab7fe72069f5a9
3
+ metadata.gz: 363fc9bc08c13030150de69edfaec4bbbbbdb03b
4
+ data.tar.gz: c7b39b7e43d39bb165c5eda6ae41e5be72f6532d
5
5
  SHA512:
6
- metadata.gz: 4b5e0517b385fe8fe14234570db9a07c27dc552454d3d7197740708828ad1a84faa496e593148f469ca609262f5047905d7a06f5942c30c60c03ac1b4f692455
7
- data.tar.gz: d80f8f709221fd66c313409129bb7aac91c9c53bd8d1c8d7ad8affa646c84e7b3eb5eac511a97eb3067519a2f785f15c9ea2885fa3199507b87dbec8fdf64141
6
+ metadata.gz: b4284c0e7b12b594985925c4c30f74c911c526cfa6531ed0c5990069d9f8e148cde3f4162a89e0e5f5ad177c1ac9bd042daf9228c72e963efd62920d1b7fa4e4
7
+ data.tar.gz: 2b90de831bb4d5678bf996bfc1806f55ac9b6f99f4167d9e3b1de9c3ee9df96de068c2a53f5109cd88b7c5de1f23d5a723d0f4f7732879929532fed7e073ac25
data/Appraisals CHANGED
@@ -1,7 +1,11 @@
1
1
  appraise "rails-4" do
2
2
  gem "rails", "~> 4.1"
3
+ gem 'jsonapi-rails', '~> 0.1', require: 'jsonapi/rails'
4
+ gem 'rspec-rails'
3
5
  end
4
6
 
5
7
  appraise "rails-5" do
6
8
  gem "rails", "~> 5.0"
9
+ gem 'jsonapi-rails', '~> 0.1', require: 'jsonapi/rails'
10
+ gem 'rspec-rails'
7
11
  end
data/Gemfile CHANGED
@@ -4,9 +4,9 @@ source "https://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  group :test do
7
+ gem 'pry'
8
+ gem 'pry-byebug', platform: [:mri]
7
9
  gem 'appraisal'
8
10
  gem 'guard'
9
11
  gem 'guard-rspec'
10
- gem 'pry'
11
- gem 'pry-byebug'
12
12
  end
@@ -2,15 +2,16 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "active_model_serializers", :git => "https://github.com/richmolj/active_model_serializers.git"
6
5
  gem "rails", "~> 4.1"
6
+ gem "jsonapi-rails", "~> 0.1", :require => "jsonapi/rails"
7
+ gem "rspec-rails"
7
8
 
8
9
  group :test do
10
+ gem "pry"
11
+ gem "pry-byebug", :platform => [:mri]
9
12
  gem "appraisal"
10
13
  gem "guard"
11
14
  gem "guard-rspec"
12
- gem "pry"
13
- gem "pry-byebug"
14
15
  end
15
16
 
16
17
  gemspec :path => "../"
@@ -1,21 +1,8 @@
1
- GIT
2
- remote: https://github.com/richmolj/active_model_serializers.git
3
- revision: 1dc2b74059731339f4df3e7882fd949e20433013
4
- specs:
5
- active_model_serializers (0.10.2)
6
- actionpack (>= 4.1, < 6)
7
- activemodel (>= 4.1, < 6)
8
- jsonapi (~> 0.1.1.beta2)
9
- railties (>= 4.1, < 6)
10
-
11
1
  PATH
12
2
  remote: ../
13
3
  specs:
14
- jsonapi_compliable (0.3.4)
15
- active_model_serializers (~> 0.10)
16
- jsonapi (~> 0.1.1.beta2)
17
- jsonapi_ams_extensions (~> 0.1)
18
- rails (>= 4.1, < 6)
4
+ jsonapi_compliable (0.4.0)
5
+ jsonapi-serializable (~> 0.1)
19
6
 
20
7
  GEM
21
8
  remote: https://rubygems.org/
@@ -61,9 +48,9 @@ GEM
61
48
  thor (>= 0.14.0)
62
49
  arel (6.0.3)
63
50
  builder (3.2.2)
64
- byebug (9.0.5)
51
+ byebug (9.0.6)
65
52
  coderay (1.1.1)
66
- concurrent-ruby (1.0.2)
53
+ concurrent-ruby (1.0.4)
67
54
  database_cleaner (1.5.3)
68
55
  diff-lcs (1.2.5)
69
56
  erubis (2.7.0)
@@ -87,11 +74,17 @@ GEM
87
74
  rspec (>= 2.99.0, < 4.0)
88
75
  i18n (0.7.0)
89
76
  json (1.8.3)
90
- jsonapi (0.1.1.beta2)
91
- json (~> 1.8)
92
- jsonapi_ams_extensions (0.1.1)
93
- active_model_serializers (~> 0.10.x)
94
- jsonapi_spec_helpers (0.2.0)
77
+ jsonapi-deserializable (0.1.1)
78
+ jsonapi-parser (= 0.1.1)
79
+ jsonapi-parser (0.1.1)
80
+ jsonapi-rails (0.1.1)
81
+ jsonapi-rb (= 0.1.1)
82
+ jsonapi-rb (0.1.1)
83
+ jsonapi-deserializable (= 0.1.1)
84
+ jsonapi-serializable (= 0.1.1)
85
+ jsonapi-renderer (0.1.1)
86
+ jsonapi-serializable (0.1.1)
87
+ jsonapi-renderer (= 0.1.1)
95
88
  kaminari (0.17.0)
96
89
  actionpack (>= 3.0.0)
97
90
  activesupport (>= 3.0.0)
@@ -109,23 +102,21 @@ GEM
109
102
  mime-types-data (~> 3.2015)
110
103
  mime-types-data (3.2016.0521)
111
104
  mini_portile2 (2.1.0)
112
- minitest (5.9.0)
105
+ minitest (5.10.1)
113
106
  nenv (0.3.0)
114
- nokogiri (1.6.8)
107
+ nokogiri (1.6.8.1)
115
108
  mini_portile2 (~> 2.1.0)
116
- pkg-config (~> 1.1.7)
117
109
  notiffany (0.1.1)
118
110
  nenv (~> 0.1)
119
111
  shellany (~> 0.0)
120
- pkg-config (1.1.7)
121
112
  pry (0.10.4)
122
113
  coderay (~> 1.1.0)
123
114
  method_source (~> 0.8.1)
124
115
  slop (~> 3.4)
125
- pry-byebug (3.4.0)
116
+ pry-byebug (3.4.2)
126
117
  byebug (~> 9.0)
127
118
  pry (~> 0.10)
128
- rack (1.6.4)
119
+ rack (1.6.5)
129
120
  rack-test (0.6.3)
130
121
  rack (>= 1.0)
131
122
  rails (4.2.7.1)
@@ -153,14 +144,14 @@ GEM
153
144
  rake (>= 0.8.7)
154
145
  thor (>= 0.18.1, < 2.0)
155
146
  rake (10.5.0)
156
- rb-fsevent (0.9.7)
147
+ rb-fsevent (0.9.8)
157
148
  rb-inotify (0.9.7)
158
149
  ffi (>= 0.5.0)
159
150
  rspec (3.5.0)
160
151
  rspec-core (~> 3.5.0)
161
152
  rspec-expectations (~> 3.5.0)
162
153
  rspec-mocks (~> 3.5.0)
163
- rspec-core (3.5.3)
154
+ rspec-core (3.5.4)
164
155
  rspec-support (~> 3.5.0)
165
156
  rspec-expectations (3.5.0)
166
157
  diff-lcs (>= 1.2.0, < 2.0)
@@ -177,7 +168,7 @@ GEM
177
168
  rspec-mocks (~> 3.5.0)
178
169
  rspec-support (~> 3.5.0)
179
170
  rspec-support (3.5.0)
180
- ruby_dep (1.4.0)
171
+ ruby_dep (1.5.0)
181
172
  shellany (0.0.1)
182
173
  slop (3.6.0)
183
174
  sprockets (3.7.0)
@@ -187,8 +178,8 @@ GEM
187
178
  actionpack (>= 4.0)
188
179
  activesupport (>= 4.0)
189
180
  sprockets (>= 3.0.0)
190
- sqlite3 (1.3.11)
191
- thor (0.19.1)
181
+ sqlite3 (1.3.12)
182
+ thor (0.19.4)
192
183
  thread_safe (0.3.5)
193
184
  tzinfo (1.2.2)
194
185
  thread_safe (~> 0.1)
@@ -197,14 +188,14 @@ PLATFORMS
197
188
  ruby
198
189
 
199
190
  DEPENDENCIES
200
- active_model_serializers!
191
+ activerecord (>= 4.1, < 6)
201
192
  appraisal
202
193
  bundler (~> 1.12)
203
194
  database_cleaner
204
195
  guard
205
196
  guard-rspec
197
+ jsonapi-rails (~> 0.1)
206
198
  jsonapi_compliable!
207
- jsonapi_spec_helpers
208
199
  kaminari
209
200
  pry
210
201
  pry-byebug
@@ -214,4 +205,4 @@ DEPENDENCIES
214
205
  sqlite3
215
206
 
216
207
  BUNDLED WITH
217
- 1.12.5
208
+ 1.13.6
@@ -2,15 +2,16 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "active_model_serializers", :git => "https://github.com/richmolj/active_model_serializers.git"
6
5
  gem "rails", "~> 5.0"
6
+ gem "jsonapi-rails", "~> 0.1", :require => "jsonapi/rails"
7
+ gem "rspec-rails"
7
8
 
8
9
  group :test do
10
+ gem "pry"
11
+ gem "pry-byebug", :platform => [:mri]
9
12
  gem "appraisal"
10
13
  gem "guard"
11
14
  gem "guard-rspec"
12
- gem "pry"
13
- gem "pry-byebug"
14
15
  end
15
16
 
16
17
  gemspec :path => "../"
@@ -1,21 +1,8 @@
1
- GIT
2
- remote: https://github.com/richmolj/active_model_serializers.git
3
- revision: 1dc2b74059731339f4df3e7882fd949e20433013
4
- specs:
5
- active_model_serializers (0.10.2)
6
- actionpack (>= 4.1, < 6)
7
- activemodel (>= 4.1, < 6)
8
- jsonapi (~> 0.1.1.beta2)
9
- railties (>= 4.1, < 6)
10
-
11
1
  PATH
12
2
  remote: ../
13
3
  specs:
14
- jsonapi_compliable (0.3.4)
15
- active_model_serializers (~> 0.10)
16
- jsonapi (~> 0.1.1.beta2)
17
- jsonapi_ams_extensions (~> 0.1)
18
- rails (>= 4.1, < 6)
4
+ jsonapi_compliable (0.4.0)
5
+ jsonapi-serializable (~> 0.1)
19
6
 
20
7
  GEM
21
8
  remote: https://rubygems.org/
@@ -61,11 +48,11 @@ GEM
61
48
  bundler
62
49
  rake
63
50
  thor (>= 0.14.0)
64
- arel (7.1.1)
51
+ arel (7.1.4)
65
52
  builder (3.2.2)
66
- byebug (9.0.5)
53
+ byebug (9.0.6)
67
54
  coderay (1.1.1)
68
- concurrent-ruby (1.0.2)
55
+ concurrent-ruby (1.0.4)
69
56
  database_cleaner (1.5.3)
70
57
  diff-lcs (1.2.5)
71
58
  erubis (2.7.0)
@@ -88,12 +75,17 @@ GEM
88
75
  guard-compat (~> 1.1)
89
76
  rspec (>= 2.99.0, < 4.0)
90
77
  i18n (0.7.0)
91
- json (1.8.3)
92
- jsonapi (0.1.1.beta2)
93
- json (~> 1.8)
94
- jsonapi_ams_extensions (0.1.1)
95
- active_model_serializers (~> 0.10.x)
96
- jsonapi_spec_helpers (0.2.0)
78
+ jsonapi-deserializable (0.1.1)
79
+ jsonapi-parser (= 0.1.1)
80
+ jsonapi-parser (0.1.1)
81
+ jsonapi-rails (0.1.1)
82
+ jsonapi-rb (= 0.1.1)
83
+ jsonapi-rb (0.1.1)
84
+ jsonapi-deserializable (= 0.1.1)
85
+ jsonapi-serializable (= 0.1.1)
86
+ jsonapi-renderer (0.1.1)
87
+ jsonapi-serializable (0.1.1)
88
+ jsonapi-renderer (= 0.1.1)
97
89
  kaminari (0.17.0)
98
90
  actionpack (>= 3.0.0)
99
91
  activesupport (>= 3.0.0)
@@ -111,21 +103,19 @@ GEM
111
103
  mime-types-data (~> 3.2015)
112
104
  mime-types-data (3.2016.0521)
113
105
  mini_portile2 (2.1.0)
114
- minitest (5.9.0)
106
+ minitest (5.10.1)
115
107
  nenv (0.3.0)
116
108
  nio4r (1.2.1)
117
- nokogiri (1.6.8)
109
+ nokogiri (1.6.8.1)
118
110
  mini_portile2 (~> 2.1.0)
119
- pkg-config (~> 1.1.7)
120
111
  notiffany (0.1.1)
121
112
  nenv (~> 0.1)
122
113
  shellany (~> 0.0)
123
- pkg-config (1.1.7)
124
114
  pry (0.10.4)
125
115
  coderay (~> 1.1.0)
126
116
  method_source (~> 0.8.1)
127
117
  slop (~> 3.4)
128
- pry-byebug (3.4.0)
118
+ pry-byebug (3.4.2)
129
119
  byebug (~> 9.0)
130
120
  pry (~> 0.10)
131
121
  rack (2.0.1)
@@ -155,14 +145,14 @@ GEM
155
145
  rake (>= 0.8.7)
156
146
  thor (>= 0.18.1, < 2.0)
157
147
  rake (10.5.0)
158
- rb-fsevent (0.9.7)
148
+ rb-fsevent (0.9.8)
159
149
  rb-inotify (0.9.7)
160
150
  ffi (>= 0.5.0)
161
151
  rspec (3.5.0)
162
152
  rspec-core (~> 3.5.0)
163
153
  rspec-expectations (~> 3.5.0)
164
154
  rspec-mocks (~> 3.5.0)
165
- rspec-core (3.5.3)
155
+ rspec-core (3.5.4)
166
156
  rspec-support (~> 3.5.0)
167
157
  rspec-expectations (3.5.0)
168
158
  diff-lcs (>= 1.2.0, < 2.0)
@@ -179,7 +169,7 @@ GEM
179
169
  rspec-mocks (~> 3.5.0)
180
170
  rspec-support (~> 3.5.0)
181
171
  rspec-support (3.5.0)
182
- ruby_dep (1.4.0)
172
+ ruby_dep (1.5.0)
183
173
  shellany (0.0.1)
184
174
  slop (3.6.0)
185
175
  sprockets (3.7.0)
@@ -189,8 +179,8 @@ GEM
189
179
  actionpack (>= 4.0)
190
180
  activesupport (>= 4.0)
191
181
  sprockets (>= 3.0.0)
192
- sqlite3 (1.3.11)
193
- thor (0.19.1)
182
+ sqlite3 (1.3.12)
183
+ thor (0.19.4)
194
184
  thread_safe (0.3.5)
195
185
  tzinfo (1.2.2)
196
186
  thread_safe (~> 0.1)
@@ -202,14 +192,14 @@ PLATFORMS
202
192
  ruby
203
193
 
204
194
  DEPENDENCIES
205
- active_model_serializers!
195
+ activerecord (>= 4.1, < 6)
206
196
  appraisal
207
197
  bundler (~> 1.12)
208
198
  database_cleaner
209
199
  guard
210
200
  guard-rspec
201
+ jsonapi-rails (~> 0.1)
211
202
  jsonapi_compliable!
212
- jsonapi_spec_helpers
213
203
  kaminari
214
204
  pry
215
205
  pry-byebug
@@ -219,4 +209,4 @@ DEPENDENCIES
219
209
  sqlite3
220
210
 
221
211
  BUNDLED WITH
222
- 1.12.5
212
+ 1.13.6
@@ -17,16 +17,12 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency "rails", ['>= 4.1', '< 6']
20
+ spec.add_dependency 'jsonapi-serializable', '~> 0.1'
21
21
 
22
- spec.add_dependency 'jsonapi-rails', '~> 0.1'
23
-
24
- spec.add_development_dependency "kaminari"
25
- spec.add_development_dependency "active_model_serializers"
26
- spec.add_development_dependency "jsonapi_spec_helpers"
22
+ spec.add_development_dependency "activerecord", ['>= 4.1', '< 6']
23
+ spec.add_development_dependency "kaminari", '~> 0.17'
27
24
  spec.add_development_dependency "bundler", "~> 1.12"
28
25
  spec.add_development_dependency "rake", "~> 10.0"
29
- spec.add_development_dependency "rspec-rails"
30
26
  spec.add_development_dependency "sqlite3"
31
27
  spec.add_development_dependency "database_cleaner"
32
28
  end
@@ -0,0 +1,25 @@
1
+ module JsonapiCompliable
2
+ module Adapters
3
+ class Abstract
4
+ def filter(scope, attribute, value)
5
+ raise 'you must override #filter in an adapter subclass'
6
+ end
7
+
8
+ def order(scope, attribute, direction)
9
+ raise 'you must override #order in an adapter subclass'
10
+ end
11
+
12
+ def paginate(scope, number, size)
13
+ raise 'you must override #paginate in an adapter subclass'
14
+ end
15
+
16
+ def sideload(scope, includes)
17
+ raise 'you must override #sideload in an adapter subclass'
18
+ end
19
+
20
+ def sideloading_module
21
+ Module.new
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,47 @@
1
+ require 'jsonapi_compliable/adapters/active_record_sideloading'
2
+
3
+ module JsonapiCompliable
4
+ module Adapters
5
+ class ActiveRecord < Abstract
6
+ def filter(scope, attribute, value)
7
+ scope.where(attribute => value)
8
+ end
9
+
10
+ def order(scope, attribute, direction)
11
+ scope.order(attribute => direction)
12
+ end
13
+
14
+ def paginate(scope, current_page, per_page)
15
+ scope.page(current_page).per(per_page)
16
+ end
17
+
18
+ def sideload(scope, includes)
19
+ scope.includes(includes)
20
+ end
21
+
22
+ def count(scope, attr)
23
+ scope.count
24
+ end
25
+
26
+ def average(scope, attr)
27
+ scope.average(attr).to_f
28
+ end
29
+
30
+ def sum(scope, attr)
31
+ scope.sum(attr)
32
+ end
33
+
34
+ def maximum(scope, attr)
35
+ scope.maximum(attr)
36
+ end
37
+
38
+ def minimum(scope, attr)
39
+ scope.minimum(attr)
40
+ end
41
+
42
+ def sideloading_module
43
+ JsonapiCompliable::Adapters::ActiveRecordSideloading
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,119 @@
1
+ module JsonapiCompliable
2
+ module Adapters
3
+ module ActiveRecordSideloading
4
+ def has_many(association_name, scope:, resource:, foreign_key:, primary_key: :id, &blk)
5
+ _scope = scope
6
+
7
+ allow_sideload association_name, resource: resource do
8
+ scope do |parents|
9
+ parent_ids = parents.map { |p| p.send(primary_key) }
10
+ _scope.call.where(foreign_key => parent_ids)
11
+ end
12
+
13
+ assign do |parents, children|
14
+ parents.each do |parent|
15
+ parent.association(association_name).loaded!
16
+ relevant_children = children.select { |c| c.send(foreign_key) == parent.send(primary_key) }
17
+ relevant_children.each do |c|
18
+ parent.association(association_name).add_to_target(c, :skip_callbacks)
19
+ end
20
+ end
21
+ end
22
+
23
+ instance_eval(&blk) if blk
24
+ end
25
+ end
26
+
27
+ def belongs_to(association_name, scope:, resource:, foreign_key:, primary_key: :id, &blk)
28
+ _scope = scope
29
+
30
+ allow_sideload association_name, resource: resource do
31
+ scope do |parents|
32
+ parent_ids = parents.map { |p| p.send(foreign_key) }
33
+ _scope.call.where(primary_key => parent_ids)
34
+ end
35
+
36
+ assign do |parents, children|
37
+ parents.each do |parent|
38
+ relevant_child = children.find { |c| parent.send(foreign_key) == c.send(primary_key) }
39
+ parent.send(:"#{association_name}=", relevant_child)
40
+ end
41
+ end
42
+ end
43
+
44
+ instance_eval(&blk) if blk
45
+ end
46
+
47
+ def has_one(association_name, scope:, resource:, foreign_key:, primary_key: :id, &blk)
48
+ _scope = scope
49
+
50
+ allow_sideload association_name, resource: resource do
51
+ scope do |parents|
52
+ parent_ids = parents.map { |p| p.send(primary_key) }
53
+ _scope.call.where(foreign_key => parent_ids)
54
+ end
55
+
56
+ assign do |parents, children|
57
+ parents.each do |parent|
58
+ parent.association(association_name).loaded!
59
+ relevant_child = children.find { |c| c.send(foreign_key) == parent.send(primary_key) }
60
+ next unless relevant_child
61
+ parent.association(association_name).replace(relevant_child, false)
62
+ end
63
+ end
64
+
65
+ instance_eval(&blk) if blk
66
+ end
67
+ end
68
+
69
+ def has_and_belongs_to_many(association_name, scope:, resource:, foreign_key:, primary_key: :id, as: nil, &blk)
70
+ through = foreign_key.keys.first
71
+ fk = foreign_key.values.first
72
+ _scope = scope
73
+
74
+ allow_sideload association_name, resource: resource do
75
+ scope do |parents|
76
+ _scope.call.joins(through).where(through => { fk => parents.map { |p| p.send(primary_key) } })
77
+ end
78
+
79
+ assign do |parents, children|
80
+ parents.each do |parent|
81
+ parent.association(association_name).loaded!
82
+ relevant_children = children.select { |c| c.send(through).any? { |ct| ct.send(fk) == parent.send(primary_key) } }
83
+ relevant_children.each do |c|
84
+ parent.association(association_name).add_to_target(c, :skip_callbacks)
85
+ end
86
+ end
87
+ end
88
+
89
+ instance_eval(&blk) if blk
90
+ end
91
+ end
92
+
93
+ def polymorphic_belongs_to(association_name, group_by:, groups:, &blk)
94
+ allow_sideload association_name, polymorphic: true do
95
+ group_by(&group_by)
96
+
97
+ groups.each_pair do |type, config|
98
+ primary_key = config[:primary_key] || :id
99
+ foreign_key = config[:foreign_key]
100
+
101
+ allow_sideload type, resource: config[:resource] do
102
+ scope do |parents|
103
+ config[:scope].call.where(primary_key => parents.map { |p| p.send(foreign_key) })
104
+ end
105
+
106
+ assign do |parents, children|
107
+ parents.each do |parent|
108
+ parent.send(:"#{association_name}=", children.find { |c| c.send(primary_key) == parent.send(foreign_key) })
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ instance_eval(&blk) if blk
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,21 @@
1
+ module JsonapiCompliable
2
+ module Adapters
3
+ class Null < Abstract
4
+ def filter(scope, attribute, value)
5
+ scope
6
+ end
7
+
8
+ def order(scope, attribute, direction)
9
+ scope
10
+ end
11
+
12
+ def paginate(scope, number, size)
13
+ scope
14
+ end
15
+
16
+ def sideload(scope, includes)
17
+ scope
18
+ end
19
+ end
20
+ end
21
+ end