jsonapi-resources 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsonapi/resource.rb +13 -1
- data/lib/jsonapi/resource_controller.rb +5 -8
- data/lib/jsonapi/resources/version.rb +1 -1
- data/test/controllers/controller_test.rb +6 -0
- data/test/fixtures/active_record.rb +6 -2
- data/test/unit/serializer/serializer_test.rb +46 -0
- 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: f0f3c0056192f7ce0100152a1365750eeec04e3b
|
4
|
+
data.tar.gz: 5d2b1057fc7f624e91eee60ea8a5cbaff019516d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25ff4a750913e3a987e2e0c3b1e366b8195db07d4487d849608637cb0c3cdfc62bbae9ea0553284bba60a12b41de19b6d451f448dd68c32c9b92d8319cde5d9b
|
7
|
+
data.tar.gz: 41a3b01c6367dd22b4ce0f285a2a4cbba91cb84868ab5dbb345498387cc2921c5565edc0c6f095de37a8a56b8b8c093460f6f83f2be3d9c55821869671dd8468
|
data/lib/jsonapi/resource.rb
CHANGED
@@ -261,6 +261,18 @@ module JSONAPI
|
|
261
261
|
self.new(model, context)
|
262
262
|
end
|
263
263
|
|
264
|
+
def find_by_keys(keys, options = {})
|
265
|
+
context = options[:context]
|
266
|
+
_models = _model_class.where({_primary_key => keys})
|
267
|
+
|
268
|
+
unless _models.length == keys.length
|
269
|
+
key = (keys - _models.pluck(:id).map(&:to_s)).first
|
270
|
+
raise JSONAPI::Exceptions::RecordNotFound.new(key)
|
271
|
+
end
|
272
|
+
|
273
|
+
_models.map { |model| self.new(model, context) }
|
274
|
+
end
|
275
|
+
|
264
276
|
def verify_filters(filters, context = nil)
|
265
277
|
verified_filters = {}
|
266
278
|
filters.each do |filter, raw_value|
|
@@ -398,7 +410,7 @@ module JSONAPI
|
|
398
410
|
resource_class = self.class.resource_for(type_name)
|
399
411
|
if resource_class
|
400
412
|
associated_model = @model.send attr
|
401
|
-
return resource_class.new(associated_model, @context)
|
413
|
+
return associated_model ? resource_class.new(associated_model, @context) : nil
|
402
414
|
end
|
403
415
|
end unless method_defined?(attr)
|
404
416
|
elsif @_associations[attr].is_a?(JSONAPI::Association::HasMany)
|
@@ -30,14 +30,11 @@ module JSONAPI
|
|
30
30
|
def show
|
31
31
|
keys = parse_key_array(params[resource_klass._primary_key])
|
32
32
|
|
33
|
-
if keys.length > 1
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
else
|
39
|
-
resources = resource_klass.find_by_key(keys[0], context: context)
|
40
|
-
end
|
33
|
+
resources = if keys.length > 1
|
34
|
+
resource_klass.find_by_keys(keys, context: context)
|
35
|
+
else
|
36
|
+
resource_klass.find_by_key(keys[0], context: context)
|
37
|
+
end
|
41
38
|
|
42
39
|
render json: JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
43
40
|
resources,
|
@@ -971,6 +971,12 @@ class TagsControllerTest < ActionController::TestCase
|
|
971
971
|
assert_equal 4, json_response['tags'].size
|
972
972
|
assert_equal 2, json_response['linked']['posts'].size
|
973
973
|
end
|
974
|
+
|
975
|
+
def test_tags_show_multiple_with_nonexistent_ids
|
976
|
+
get :show, {id: '6,99,9,100'}
|
977
|
+
assert_response :not_found
|
978
|
+
assert_match /The record identified by 99 could not be found./, json_response['errors'][0]['detail']
|
979
|
+
end
|
974
980
|
end
|
975
981
|
|
976
982
|
class ExpenseEntriesControllerTest < ActionController::TestCase
|
@@ -135,7 +135,7 @@ end
|
|
135
135
|
|
136
136
|
class Planet < ActiveRecord::Base
|
137
137
|
has_many :moons
|
138
|
-
|
138
|
+
belongs_to :planet_type
|
139
139
|
|
140
140
|
has_and_belongs_to_many :tags, join_table: :planets_tags
|
141
141
|
end
|
@@ -432,6 +432,10 @@ class BreedResource < JSONAPI::Resource
|
|
432
432
|
def self.find_by_key(id, options = {})
|
433
433
|
BreedResource.new($breed_data.breeds[id.to_i], options[:context])
|
434
434
|
end
|
435
|
+
|
436
|
+
def self.find_by_keys(keys, options = {})
|
437
|
+
keys.map { |key| self.find_by_key(key, options) }
|
438
|
+
end
|
435
439
|
end
|
436
440
|
|
437
441
|
class PlanetResource < JSONAPI::Resource
|
@@ -452,7 +456,7 @@ class PropertyResource < JSONAPI::Resource
|
|
452
456
|
end
|
453
457
|
|
454
458
|
class PlanetTypeResource < JSONAPI::Resource
|
455
|
-
|
459
|
+
attributes :id, :name
|
456
460
|
has_many :planets
|
457
461
|
end
|
458
462
|
|
@@ -549,4 +549,50 @@ class SerializerTest < MiniTest::Unit::TestCase
|
|
549
549
|
assert_match /\"planetType\":null/, json
|
550
550
|
assert_match /\"moons\":\[\]/, json
|
551
551
|
end
|
552
|
+
|
553
|
+
def test_serializer_include_with_empty_links_null_and_array
|
554
|
+
planets = []
|
555
|
+
Planet.find(7, 8).each do |planet|
|
556
|
+
planets.push PlanetResource.new(planet)
|
557
|
+
end
|
558
|
+
|
559
|
+
planet_hash = JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
560
|
+
planets,
|
561
|
+
include: ['planet_type'],
|
562
|
+
fields: { planet_types: [:id, :name] }
|
563
|
+
)
|
564
|
+
|
565
|
+
assert_hash_equals(
|
566
|
+
{
|
567
|
+
planets: [{
|
568
|
+
id: 7,
|
569
|
+
name: 'Beta X',
|
570
|
+
description: 'Newly discovered Planet Z',
|
571
|
+
links: {
|
572
|
+
planetType: 1,
|
573
|
+
tags: [],
|
574
|
+
moons: []
|
575
|
+
}
|
576
|
+
},
|
577
|
+
{
|
578
|
+
id: 8,
|
579
|
+
name: 'Beta W',
|
580
|
+
description: 'Newly discovered Planet W',
|
581
|
+
links: {
|
582
|
+
planetType: nil,
|
583
|
+
tags: [],
|
584
|
+
moons: []
|
585
|
+
}
|
586
|
+
}],
|
587
|
+
linked: {
|
588
|
+
planetTypes: [
|
589
|
+
{ id: 1, name: "Gas Giant" }
|
590
|
+
]
|
591
|
+
}
|
592
|
+
}, planet_hash)
|
593
|
+
|
594
|
+
json = planet_hash.to_json
|
595
|
+
assert_match /\"planetType\":null/, json
|
596
|
+
assert_match /\"moons\":\[\]/, json
|
597
|
+
end
|
552
598
|
end
|
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.10
|
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-
|
12
|
+
date: 2014-11-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|