jsonapi-resources 0.0.10 → 0.0.11
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 +4 -4
- data/lib/jsonapi/request.rb +2 -8
- data/lib/jsonapi/resource.rb +35 -0
- data/lib/jsonapi/resource_controller.rb +1 -5
- data/lib/jsonapi/resources/version.rb +1 -1
- data/test/fixtures/active_record.rb +18 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 693f8ed867afa11d9d23f0631bdf2ccd7e19acbe
|
4
|
+
data.tar.gz: 5a0edbc2ab2a5f7c801c3120307c54c7c6a1142a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5f18920d6adee8dd748507071addacc51f58ba2719bf0961fb4fad01de7fb4af229755f90eb22b7c06ec20772fdff835fb6b7234b54c7a341d1eb6885ea879d
|
7
|
+
data.tar.gz: 83901cfc4f1d2dad3883e782bf90cef7cbf62873faba9b872fb84b756ec71c5020bca86acba72cbc72617a55b2cc58bab3b12152a89cf20e5f9e6a2c40aaa9aa
|
data/lib/jsonapi/request.rb
CHANGED
@@ -207,9 +207,7 @@ module JSONAPI
|
|
207
207
|
elsif association.is_a?(JSONAPI::Association::HasMany)
|
208
208
|
keys = []
|
209
209
|
if value.is_a?(Array)
|
210
|
-
value
|
211
|
-
keys.push(@resource_klass.resource_for(association.type).verify_key(val, @context))
|
212
|
-
end
|
210
|
+
keys = @resource_klass.resource_for(association.type).verify_keys(value, @context)
|
213
211
|
else
|
214
212
|
keys.push(@resource_klass.resource_for(association.type).verify_key(value, @context))
|
215
213
|
end
|
@@ -387,11 +385,7 @@ module JSONAPI
|
|
387
385
|
end
|
388
386
|
|
389
387
|
def parse_key_array(raw)
|
390
|
-
|
391
|
-
raw.split(/,/).collect do |key|
|
392
|
-
keys.push @resource_klass.verify_key(key, @context)
|
393
|
-
end
|
394
|
-
return keys
|
388
|
+
return @resource_klass.verify_keys(raw.split(/,/), context)
|
395
389
|
end
|
396
390
|
|
397
391
|
def format_key(key)
|
data/lib/jsonapi/resource.rb
CHANGED
@@ -127,6 +127,9 @@ module JSONAPI
|
|
127
127
|
|
128
128
|
type = base.name.demodulize.sub(/Resource$/, '').underscore
|
129
129
|
base._type = type.pluralize.to_sym
|
130
|
+
|
131
|
+
check_reserved_resource_name(base._type, base.name)
|
132
|
+
|
130
133
|
# If eager loading is on this is how all the resource types are setup
|
131
134
|
# If eager loading is off some resource types will be initialized in
|
132
135
|
# _resource_name_from_type
|
@@ -159,6 +162,8 @@ module JSONAPI
|
|
159
162
|
end
|
160
163
|
|
161
164
|
def attribute(attr, options = {})
|
165
|
+
check_reserved_attribute_name(attr)
|
166
|
+
|
162
167
|
@_attributes[attr] = options
|
163
168
|
define_method attr do
|
164
169
|
@model.send(attr)
|
@@ -302,6 +307,13 @@ module JSONAPI
|
|
302
307
|
return key
|
303
308
|
end
|
304
309
|
|
310
|
+
# override to allow for key processing and checking
|
311
|
+
def verify_keys(keys, context = nil)
|
312
|
+
return keys.collect do |key|
|
313
|
+
verify_key(key, context)
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
305
317
|
# override to allow for custom filters
|
306
318
|
def verify_custom_filter(filter, value, context = nil)
|
307
319
|
return filter, value
|
@@ -388,10 +400,33 @@ module JSONAPI
|
|
388
400
|
|
389
401
|
private
|
390
402
|
|
403
|
+
def check_reserved_resource_name(type, name)
|
404
|
+
if [:ids, :types, :hrefs, :links].include?(type)
|
405
|
+
warn "[NAME COLLISION] `#{name}` is a reserved resource name."
|
406
|
+
return
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
def check_reserved_attribute_name(name)
|
411
|
+
# Allow :id since it can be used to specify the format. Since it is a method on the base Resource
|
412
|
+
# an attribute method won't be created for it.
|
413
|
+
if [:type, :href, :links].include?(name.to_sym)
|
414
|
+
warn "[NAME COLLISION] `#{name}` is a reserved key in #{@@resource_types[_type]}."
|
415
|
+
end
|
416
|
+
end
|
417
|
+
|
418
|
+
def check_reserved_association_name(name)
|
419
|
+
if [:id, :ids, :type, :types, :href, :hrefs, :link, :links].include?(name.to_sym)
|
420
|
+
warn "[NAME COLLISION] `#{name}` is a reserved association name in #{@@resource_types[_type]}."
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
391
424
|
def _associate(klass, *attrs)
|
392
425
|
options = attrs.extract_options!
|
393
426
|
|
394
427
|
attrs.each do |attr|
|
428
|
+
check_reserved_association_name(attr)
|
429
|
+
|
395
430
|
@_associations[attr] = klass.new(attr, options)
|
396
431
|
|
397
432
|
foreign_key = @_associations[attr].foreign_key
|
@@ -120,11 +120,7 @@ module JSONAPI
|
|
120
120
|
end
|
121
121
|
|
122
122
|
def parse_key_array(raw)
|
123
|
-
|
124
|
-
raw.split(/,/).collect do |key|
|
125
|
-
keys.push resource_klass.verify_key(key, context)
|
126
|
-
end
|
127
|
-
return keys
|
123
|
+
return resource_klass.verify_keys(raw.split(/,/), context)
|
128
124
|
end
|
129
125
|
|
130
126
|
# override to set context
|
@@ -379,9 +379,7 @@ class PostResource < JSONAPI::Resource
|
|
379
379
|
def self.verify_custom_filter(filter, values, context = nil)
|
380
380
|
case filter
|
381
381
|
when :id
|
382
|
-
values
|
383
|
-
verify_key(key, context)
|
384
|
-
end
|
382
|
+
verify_keys(values, context)
|
385
383
|
end
|
386
384
|
return filter, values
|
387
385
|
end
|
@@ -476,6 +474,23 @@ class PreferencesResource < JSONAPI::Resource
|
|
476
474
|
has_many :friends, class_name: 'Person'
|
477
475
|
end
|
478
476
|
|
477
|
+
warn 'start testing Name Collisions'
|
478
|
+
# The name collisions only emmit warnings. Exceptions would change the flow of the tests
|
479
|
+
|
480
|
+
class LinksResource < JSONAPI::Resource
|
481
|
+
|
482
|
+
end
|
483
|
+
|
484
|
+
class BadlyNamedAttributesResource < JSONAPI::Resource
|
485
|
+
attributes :type, :href, :links
|
486
|
+
|
487
|
+
has_many :links
|
488
|
+
has_one :href
|
489
|
+
has_one :id
|
490
|
+
has_many :types
|
491
|
+
end
|
492
|
+
warn 'end testing Name Collisions'
|
493
|
+
|
479
494
|
### DATA
|
480
495
|
javascript = Section.create(name: 'javascript')
|
481
496
|
ruby = Section.create(name: 'ruby')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Gebhardt
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-11-
|
12
|
+
date: 2014-11-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|