standardapi 6.0.0 → 6.0.0.9

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
  SHA256:
3
- metadata.gz: 81dc01aaad4d3d573a00b241af7c7d4134bc0f9e0c5aae9474cec5b854e000a0
4
- data.tar.gz: fd49e0bfdb1f9c264616dfe64ef84d5f1ae1cd2bab6f7be267d8c1c449085a67
3
+ metadata.gz: c628d369e270f41efcedd26602fee435cd4d7b58b80522e982b842fdac0e2f4c
4
+ data.tar.gz: 34ecda61518319d65c0b0a88eb257212dd29f828db15acfd35e39c9af49eff71
5
5
  SHA512:
6
- metadata.gz: 294677d3823d9adbe74f1f91ee7da3b17590dceab4c1c7ef2279f95d377dfd65e7d73a4be8a6950f205f1f4f3f7b755f948c85938d39220fcdff286fabf4a714
7
- data.tar.gz: bde0392349330cf02767e0233b1e878960bdcc5402e503306035e1e8b966d2d604c3afe1fa3cc863aaf214f4b4bdf1b817881438694b21a423b843a633b7292c
6
+ metadata.gz: 4f1739fa09be7bcc2b339d1714fa2f327861e39aa04228a287a16ad94e7aab0b3625173ec012b2f0653c6c0cfcf9c92f7ceaaeaadc5477afc0c7444570a84d3a
7
+ data.tar.gz: 8214eeb03cff6d51051e40da9438392b92d1a08a0b09f022f35b9e2028c9c384afbeba7c1975a48ac6e6537d6d3e33aac0c19725ded76c96e18aae85a66ffa8a
@@ -72,7 +72,7 @@ module StandardAPI
72
72
  record = resources.find(params[:id])
73
73
  instance_variable_set("@#{model.model_name.singular}", record)
74
74
 
75
- if record.update_attributes(model_params)
75
+ if record.update(model_params)
76
76
  if request.format == :html
77
77
  redirect_to url_for(
78
78
  controller: record.class.base_class.model_name.collection,
@@ -155,7 +155,7 @@ module StandardAPI
155
155
  end
156
156
 
157
157
  def resources
158
- query = model.filter(params[:where]).filter(current_mask[model.table_name])
158
+ query = model.filter(params['where']).filter(current_mask[model.table_name])
159
159
 
160
160
  if params[:distinct_on]
161
161
  query = query.distinct_on(params[:distinct_on])
@@ -187,8 +187,10 @@ module StandardAPI
187
187
  when true
188
188
  preloads[key] = value
189
189
  when Hash, ActiveSupport::HashWithIndifferentAccess
190
- if !value.keys.any? { |x| ['where', 'limit', 'offset', 'order', 'distinct'].include?(x) }
191
- preloads[key] = preloadables_hash(reflection.klass, value)
190
+ if !value.keys.any? { |x| ['when', 'where', 'limit', 'offset', 'order', 'distinct'].include?(x) }
191
+ if !reflection.polymorphic?
192
+ preloads[key] = preloadables_hash(reflection.klass, value)
193
+ end
192
194
  end
193
195
  end
194
196
  end
@@ -206,8 +208,10 @@ module StandardAPI
206
208
  when true
207
209
  preloads[key] = value
208
210
  when Hash, ActiveSupport::HashWithIndifferentAccess
209
- if !value.keys.any? { |x| ['where', 'limit', 'offset', 'order', 'distinct'].include?(x) }
210
- preloads[key] = preloadables_hash(reflection.klass, value)
211
+ if !value.keys.any? { |x| ['when', 'where', 'limit', 'offset', 'order', 'distinct'].include?(x) }
212
+ if !reflection.polymorphic?
213
+ preloads[key] = preloadables_hash(reflection.klass, value)
214
+ end
211
215
  end
212
216
  end
213
217
  end
@@ -303,7 +307,7 @@ module StandardAPI
303
307
 
304
308
  column = column == '*' ? Arel.star : column.to_sym
305
309
  if functions.include?(func.to_s.downcase)
306
- @selects << ((@model || model).arel_table[column].send(func))
310
+ @selects << ((defined?(@model) ? @model : model).arel_table[column].send(func))
307
311
  end
308
312
  end
309
313
  end
@@ -23,7 +23,7 @@ module StandardAPI
23
23
  if includes.empty?
24
24
  record.cache_key(*timestamp_keys)
25
25
  else
26
- timestamp = record.send(:max_updated_column_timestamp, timestamp_keys)
26
+ timestamp = timestamp_keys.map { |attr| record[attr]&.to_time }.compact.max
27
27
  "#{record.model_name.cache_key}/#{record.id}-#{digest_hash(sort_hash(includes))}-#{timestamp.utc.to_s(record.cache_timestamp_format)}"
28
28
  end
29
29
  end
@@ -58,7 +58,7 @@ module StandardAPI
58
58
  end
59
59
 
60
60
  def cached_at_columns_for_includes(includes)
61
- includes.select { |k,v| ![:where, :limit, :order, :distinct].include?(k.to_sym) }.map do |k, v|
61
+ includes.select { |k,v| !['when', 'where', 'limit', 'order', 'distinct'].include?(k) }.map do |k, v|
62
62
  ["#{k}_cached_at"] + cached_at_columns_for_includes(v).map { |v2| "#{k}_#{v2}" }
63
63
  end.flatten
64
64
  end
@@ -17,7 +17,7 @@ module StandardAPI
17
17
  includes.flatten.compact.each { |v| normalized.merge!(normalize(v)) }
18
18
  when Hash, ActionController::Parameters
19
19
  includes.each_pair do |k, v|
20
- if ['where', 'order'].include?(k.to_s) # Where and order are not normalized (sanitation happens in activerecord-filter)
20
+ if ['limit', 'when', 'where', 'order'].include?(k.to_s) # Where and order are not normalized (sanitation happens in activerecord-filter)
21
21
  normalized[k] = case v
22
22
  when Hash then v.to_h
23
23
  when ActionController::Parameters then v.to_unsafe_h
@@ -58,7 +58,7 @@ module StandardAPI
58
58
 
59
59
  permit = normalize(permit.with_indifferent_access)
60
60
  includes.each do |k, v|
61
- if permit.has_key?(k) || ['where', 'order', 'distinct'].include?(k.to_s)
61
+ if permit.has_key?(k) || ['limit', 'when', 'where', 'order', 'distinct'].include?(k.to_s)
62
62
  permitted[k] = sanitize(v, permit[k] || {}, true)
63
63
  else
64
64
  if [:raise, nil].include?(Rails.configuration.try(:action_on_unpermitted_includes))
@@ -15,7 +15,14 @@ module StandardAPI
15
15
  key2, key3 = *key.to_s.split('.')
16
16
  permitted << sanitize({key2.to_sym => { key3.to_sym => value } }, permit)
17
17
  elsif permit.include?(key.to_s)
18
- value = value.to_unsafe_hash.symbolize_keys if value.is_a?(Hash) || value.is_a?(ActionController::Parameters)
18
+ case value
19
+ when Hash
20
+ value
21
+ when ActionController::Parameters
22
+ value.to_unsafe_hash
23
+ else
24
+ value
25
+ end
19
26
  permitted << { key.to_sym => value }
20
27
  elsif permit.find { |x| (x.is_a?(Hash) || x.is_a?(ActionController::Parameters)) && x.has_key?(key.to_s) }
21
28
  subpermit = permit.find { |x| (x.is_a?(Hash) || x.is_a?(ActionController::Parameters)) && x.has_key?(key.to_s) }[key.to_s]
@@ -90,7 +90,7 @@ module StandardAPI
90
90
  assert_equal json[included.to_s][key.to_s], normalize_to_json(m, key, value), message
91
91
  end
92
92
  else
93
- m = models.first.send(included).first.try(:reload)
93
+ m = models.find { |x| json['id'] == normalize_to_json(x, 'id', x.id) }.send(included).first.try(:reload)
94
94
 
95
95
  m_json = if m && m.has_attribute?(:id)
96
96
  json[included.to_s].find { |x| x['id'] == normalize_to_json(m, :id, m.id) }
@@ -1,3 +1,3 @@
1
1
  module StandardAPI
2
- VERSION = '6.0.0'
2
+ VERSION = '6.0.0.9'
3
3
  end
@@ -5,7 +5,7 @@ record.attributes.each do |name, value|
5
5
  end
6
6
 
7
7
  includes.each do |inc, subinc|
8
- next if [:where, :order, :limit].include?(inc.to_sym)
8
+ next if ["limit", "offset", "order", "when", "where"].include?(inc)
9
9
 
10
10
  case association = record.class.reflect_on_association(inc)
11
11
  when ActiveRecord::Reflection::HasManyReflection, ActiveRecord::Reflection::HasAndBelongsToManyReflection, ActiveRecord::Reflection::ThroughReflection
@@ -14,18 +14,17 @@ includes.each do |inc, subinc|
14
14
  partial = model_partial(association.klass)
15
15
  json.set! inc do
16
16
  # TODO limit causes preloaded assocations to reload
17
- if subinc.keys.any? { |x| ['where', 'limit', 'offset', 'order'].include?(x) }
18
- if subinc[:distinct]
19
- json.array! record.send(inc).filter(subinc[:where]).limit(subinc[:limit]).sort(subinc[:order]).distinct_on(subinc[:distinct]), partial: partial, as: partial.split('/').last, locals: { includes: subinc }
17
+ if subinc.keys.any? { |x| ["limit", "offset", "order", "when", "where"].include?(x) }
18
+ if subinc['distinct']
19
+ json.array! record.send(inc).filter(subinc['where']).limit(subinc['limit']).sort(subinc['order']).distinct_on(subinc['distinct']), partial: partial, as: partial.split('/').last, locals: { includes: subinc }
20
20
  else
21
- json.array! record.send(inc).filter(subinc[:where]).limit(subinc[:limit]).sort(subinc[:order]).distinct, partial: partial, as: partial.split('/').last, locals: { includes: subinc }
21
+ json.array! record.send(inc).filter(subinc['where']).limit(subinc['limit']).sort(subinc['order']).distinct, partial: partial, as: partial.split('/').last, locals: { includes: subinc }
22
22
  end
23
23
  else
24
24
  json.array! record.send(inc), partial: partial, as: partial.split('/').last, locals: { includes: subinc }
25
25
  end
26
26
  end
27
27
  end
28
-
29
28
  when ActiveRecord::Reflection::BelongsToReflection, ActiveRecord::Reflection::HasOneReflection
30
29
  can_cache = can_cache_relation?(record.class, inc, subinc)
31
30
  if association.is_a?(ActiveRecord::Reflection::BelongsToReflection)
@@ -42,7 +41,6 @@ includes.each do |inc, subinc|
42
41
  end
43
42
  end
44
43
  end
45
-
46
44
  else
47
45
  if record.respond_to?(inc)
48
46
  value = record.send(inc)
@@ -65,4 +63,4 @@ if !record.errors.blank?
65
63
  errs = record.errors.to_hash
66
64
  errs.default_proc = nil
67
65
  json.set! 'errors', errs
68
- end
66
+ end
@@ -7,8 +7,8 @@ json.object! do
7
7
  end
8
8
 
9
9
  includes.each do |inc, subinc|
10
- next if [:where, :order, :limit].include?(inc.to_sym)
11
-
10
+ next if ["limit", "offset", "order", "when", "where"].include?(inc)
11
+
12
12
  case association = record.class.reflect_on_association(inc)
13
13
  when ActiveRecord::Reflection::HasManyReflection, ActiveRecord::Reflection::HasAndBelongsToManyReflection, ActiveRecord::Reflection::ThroughReflection
14
14
  can_cache = can_cache_relation?(record.class, inc, subinc)
@@ -16,18 +16,17 @@ json.object! do
16
16
  partial = model_partial(association.klass)
17
17
  json.set! inc do
18
18
  # TODO limit causes preloaded assocations to reload
19
- if subinc.keys.any? { |x| ['where', 'limit', 'offset', 'order'].include?(x) }
20
- if subinc[:distinct]
21
- json.array! record.send(inc).filter(subinc[:where]).limit(subinc[:limit]).sort(subinc[:order]).distinct_on(subinc[:distinct]), partial: partial, as: partial.split('/').last, locals: { includes: subinc }
19
+ if subinc.keys.any? { |x| ["limit", "offset", "order", "when", "where"].include?(x) }
20
+ if subinc['distinct']
21
+ json.array! record.send(inc).filter(subinc['where']).limit(subinc['limit']).sort(subinc['order']).distinct_on(subinc['distinct']), partial: partial, as: partial.split('/').last, locals: { includes: subinc }
22
22
  else
23
- json.array! record.send(inc).filter(subinc[:where]).limit(subinc[:limit]).sort(subinc[:order]).distinct, partial: partial, as: partial.split('/').last, locals: { includes: subinc }
23
+ json.array! record.send(inc).filter(subinc['where']).limit(subinc['limit']).sort(subinc['order']).distinct, partial: partial, as: partial.split('/').last, locals: { includes: subinc }
24
24
  end
25
25
  else
26
26
  json.array! record.send(inc), partial: partial, as: partial.split('/').last, locals: { includes: subinc }
27
27
  end
28
28
  end
29
29
  end
30
-
31
30
  when ActiveRecord::Reflection::BelongsToReflection, ActiveRecord::Reflection::HasOneReflection
32
31
  can_cache = can_cache_relation?(record.class, inc, subinc)
33
32
  if association.is_a?(ActiveRecord::Reflection::BelongsToReflection)
@@ -44,7 +43,6 @@ json.object! do
44
43
  end
45
44
  end
46
45
  end
47
-
48
46
  else
49
47
  if record.respond_to?(inc)
50
48
  value = record.send(inc)
@@ -60,13 +58,13 @@ json.object! do
60
58
  end
61
59
  end
62
60
  end
63
-
61
+
64
62
  end
65
-
63
+
66
64
  if !record.errors.blank?
67
65
  errs = record.errors.to_hash
68
66
  errs.default_proc = nil
69
67
  json.set! 'errors', errs
70
68
  end
71
69
 
72
- end
70
+ end
@@ -5,12 +5,32 @@ end
5
5
  if !includes.empty? && can_cache?(model, includes)
6
6
  partial = model_partial(model)
7
7
  record_name = partial.split('/').last.to_sym
8
- locals = { record_name => nil, :includes => includes }
9
8
 
10
9
  json.cache_collection! instance_variable_get("@#{model.model_name.plural}"), key: proc { |record| cache_key(record, includes) } do |record|
11
- locals[record_name] = record
10
+ locals = { record: record, record_name => record, :includes => includes }
12
11
  json.partial! partial, locals
13
12
  end
14
13
  else
15
- json.array! instance_variable_get("@#{model.model_name.plural}"), partial: model_partial(model), as: model_partial(model).split('/').last, includes: includes
14
+ partial = model_partial(model)
15
+ record_name = partial.split('/').last.to_sym
16
+ json.array!(instance_variable_get("@#{model.model_name.plural}")) do |record|
17
+ sub_includes = includes.select do |key, value|
18
+ case value
19
+ when Hash, ActionController::Parameters
20
+ if value.has_key?('when')
21
+ value['when'].all? { |k, v| record.send(k).as_json == v }
22
+ else
23
+ true
24
+ end
25
+ else
26
+ true
27
+ end
28
+ end
29
+
30
+ json.partial! partial, {
31
+ record: record,
32
+ record_name => record,
33
+ includes: sub_includes
34
+ }
35
+ end
16
36
  end
@@ -5,12 +5,34 @@ end
5
5
  if !includes.empty? && can_cache?(model, includes)
6
6
  partial = model_partial(model)
7
7
  record_name = partial.split('/').last.to_sym
8
- locals = { record_name => nil, :includes => includes }
9
8
 
10
9
  json.cache_collection! instance_variable_get("@#{model.model_name.plural}"), key: proc {|record| cache_key(record, includes) } do |record|
11
- locals[record_name] = record
10
+ locals = { record: record, record_name => record, :includes => includes }
12
11
  json.partial! partial, locals
13
12
  end
14
13
  else
15
- json.array! instance_variable_get("@#{model.model_name.plural}"), partial: model_partial(model), as: model_partial(model).split('/').last, includes: includes
14
+ partial = model_partial(model)
15
+ record_name = partial.split('/').last.to_sym
16
+ json.array! do
17
+ instance_variable_get("@#{model.model_name.plural}").each do |record|
18
+ sub_includes = includes.select do |key, value|
19
+ case value
20
+ when Hash, ActionController::Parameters
21
+ if value.has_key?('when')
22
+ value['when'].all? { |k, v| record.send(k).as_json == v }
23
+ else
24
+ true
25
+ end
26
+ else
27
+ true
28
+ end
29
+ end
30
+
31
+ json.partial! partial, {
32
+ record: record,
33
+ record_name => record,
34
+ includes: sub_includes
35
+ }
36
+ end
37
+ end
16
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standardapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0
4
+ version: 6.0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-30 00:00:00.000000000 Z
11
+ date: 2019-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,42 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 6.0.0.rc1
19
+ version: 6.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 6.0.0.rc1
26
+ version: 6.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 6.0.0.rc1
33
+ version: 6.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 6.0.0.rc1
40
+ version: 6.0.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: actionpack
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 6.0.0.rc1
47
+ version: 6.0.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 6.0.0.rc1
54
+ version: 6.0.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activerecord-sort
57
57
  requirement: !ruby/object:Gem::Requirement