jsonapi-resources 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +109 -3
- data/lib/jsonapi/acts_as_resource_controller.rb +7 -7
- data/lib/jsonapi/error.rb +2 -1
- data/lib/jsonapi/exceptions.rb +8 -1
- data/lib/jsonapi/link_builder.rb +2 -2
- data/lib/jsonapi/relationship.rb +4 -3
- data/lib/jsonapi/request.rb +1 -6
- data/lib/jsonapi/resource.rb +142 -98
- data/lib/jsonapi/resource_serializer.rb +7 -7
- data/lib/jsonapi/resources/version.rb +1 -1
- data/lib/jsonapi/response_document.rb +2 -2
- data/test/controllers/controller_test.rb +77 -33
- data/test/fixtures/active_record.rb +169 -93
- data/test/fixtures/book_authors.yml +3 -0
- data/test/integration/requests/request_test.rb +63 -57
- data/test/test_helper.rb +7 -0
- data/test/unit/resource/resource_test.rb +69 -3
- data/test/unit/serializer/serializer_test.rb +1 -1
- metadata +4 -2
data/test/test_helper.rb
CHANGED
@@ -116,6 +116,7 @@ end
|
|
116
116
|
JSONAPI.configuration.route_format = :underscored_route
|
117
117
|
TestApp.routes.draw do
|
118
118
|
jsonapi_resources :people
|
119
|
+
jsonapi_resources :special_people
|
119
120
|
jsonapi_resources :comments
|
120
121
|
jsonapi_resources :firms
|
121
122
|
jsonapi_resources :tags
|
@@ -142,6 +143,9 @@ TestApp.routes.draw do
|
|
142
143
|
jsonapi_resources :boats
|
143
144
|
jsonapi_resources :flat_posts
|
144
145
|
|
146
|
+
jsonapi_resources :books
|
147
|
+
jsonapi_resources :authors
|
148
|
+
|
145
149
|
namespace :api do
|
146
150
|
namespace :v1 do
|
147
151
|
jsonapi_resources :people
|
@@ -225,6 +229,9 @@ TestApp.routes.draw do
|
|
225
229
|
jsonapi_resources :customers
|
226
230
|
jsonapi_resources :purchase_orders
|
227
231
|
jsonapi_resources :line_items
|
232
|
+
jsonapi_resources :categories
|
233
|
+
|
234
|
+
jsonapi_resources :clients
|
228
235
|
end
|
229
236
|
|
230
237
|
namespace :v8 do
|
@@ -63,6 +63,20 @@ end
|
|
63
63
|
|
64
64
|
module MyModule
|
65
65
|
class MyNamespacedResource < JSONAPI::Resource
|
66
|
+
model_name "Person"
|
67
|
+
has_many :related
|
68
|
+
end
|
69
|
+
|
70
|
+
class RelatedResource < JSONAPI::Resource
|
71
|
+
model_name "Comment"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
module MyAPI
|
76
|
+
class MyNamespacedResource < MyModule::MyNamespacedResource
|
77
|
+
end
|
78
|
+
|
79
|
+
class RelatedResource < MyModule::RelatedResource
|
66
80
|
end
|
67
81
|
end
|
68
82
|
|
@@ -72,7 +86,11 @@ class ResourceTest < ActiveSupport::TestCase
|
|
72
86
|
end
|
73
87
|
|
74
88
|
def test_model_name
|
75
|
-
assert_equal(PostResource._model_name
|
89
|
+
assert_equal("Post", PostResource._model_name)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_model_name_of_subclassed_non_abstract_resource
|
93
|
+
assert_equal("Firm", FirmResource._model_name)
|
76
94
|
end
|
77
95
|
|
78
96
|
def test_model
|
@@ -83,6 +101,47 @@ class ResourceTest < ActiveSupport::TestCase
|
|
83
101
|
assert_equal(MyModule::MyNamespacedResource.module_path, 'my_module/')
|
84
102
|
end
|
85
103
|
|
104
|
+
def test_resource_for_root_resource
|
105
|
+
assert_raises NameError do
|
106
|
+
JSONAPI::Resource.resource_for('related')
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_resource_for_with_namespaced_paths
|
111
|
+
assert_equal(JSONAPI::Resource.resource_for('my_module/related'), MyModule::RelatedResource)
|
112
|
+
assert_equal(PostResource.resource_for('my_module/related'), MyModule::RelatedResource)
|
113
|
+
assert_equal(MyModule::MyNamespacedResource.resource_for('my_module/related'), MyModule::RelatedResource)
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_resource_for_resource_does_not_exist_at_root
|
117
|
+
assert_raises NameError do
|
118
|
+
ArticleResource.resource_for('related')
|
119
|
+
end
|
120
|
+
assert_raises NameError do
|
121
|
+
JSONAPI::Resource.resource_for('related')
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_resource_for_namespaced_resource
|
126
|
+
assert_equal(MyModule::MyNamespacedResource.resource_for('related'), MyModule::RelatedResource)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_relationship_parent_point_to_correct_resource
|
130
|
+
assert_equal MyModule::MyNamespacedResource, MyModule::MyNamespacedResource._relationships[:related].parent_resource
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_relationship_parent_option_point_to_correct_resource
|
134
|
+
assert_equal MyModule::MyNamespacedResource, MyModule::MyNamespacedResource._relationships[:related].options[:parent_resource]
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_derived_resources_relationships_parent_point_to_correct_resource
|
138
|
+
assert_equal MyAPI::MyNamespacedResource, MyAPI::MyNamespacedResource._relationships[:related].parent_resource
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_derived_resources_relationships_parent_options_point_to_correct_resource
|
142
|
+
assert_equal MyAPI::MyNamespacedResource, MyAPI::MyNamespacedResource._relationships[:related].options[:parent_resource]
|
143
|
+
end
|
144
|
+
|
86
145
|
def test_base_resource_abstract
|
87
146
|
assert BaseResource._abstract
|
88
147
|
end
|
@@ -240,7 +299,7 @@ class ResourceTest < ActiveSupport::TestCase
|
|
240
299
|
|
241
300
|
# define apply_filters method on post resource to not respect filters
|
242
301
|
PostResource.instance_eval do
|
243
|
-
def apply_sort(records, criteria)
|
302
|
+
def apply_sort(records, criteria, context = {})
|
244
303
|
# :nocov:
|
245
304
|
records
|
246
305
|
# :nocov:
|
@@ -253,7 +312,7 @@ class ResourceTest < ActiveSupport::TestCase
|
|
253
312
|
ensure
|
254
313
|
# reset method to original implementation
|
255
314
|
PostResource.instance_eval do
|
256
|
-
def apply_sort(records, criteria)
|
315
|
+
def apply_sort(records, criteria, context = {})
|
257
316
|
# :nocov:
|
258
317
|
super
|
259
318
|
# :nocov:
|
@@ -471,4 +530,11 @@ class ResourceTest < ActiveSupport::TestCase
|
|
471
530
|
end
|
472
531
|
assert_equal(err.error_messages[:base], ['Boom! Error added in after_save callback.'])
|
473
532
|
end
|
533
|
+
|
534
|
+
def test_resource_for_model_use_hint
|
535
|
+
special_person = Person.create!(name: 'Special', date_joined: Date.today, special: true)
|
536
|
+
special_resource = SpecialPersonResource.new(special_person, nil)
|
537
|
+
resource_model = SpecialPersonResource.records({}).first # simulate a find
|
538
|
+
assert_equal(SpecialPersonResource, SpecialPersonResource.resource_for_model(resource_model))
|
539
|
+
end
|
474
540
|
end
|
@@ -1742,7 +1742,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
|
|
1742
1742
|
def meta(options)
|
1743
1743
|
{
|
1744
1744
|
fixed: 'Hardcoded value',
|
1745
|
-
computed: "#{self.class._type.to_s}: #{options[:serializer].
|
1745
|
+
computed: "#{self.class._type.to_s}: #{options[:serializer].link_builder.self_link(self)}"
|
1746
1746
|
}
|
1747
1747
|
end
|
1748
1748
|
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.
|
4
|
+
version: 0.7.0
|
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: 2015-
|
12
|
+
date: 2015-12-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -157,6 +157,7 @@ files:
|
|
157
157
|
- test/controllers/controller_test.rb
|
158
158
|
- test/fixtures/active_record.rb
|
159
159
|
- test/fixtures/author_details.yml
|
160
|
+
- test/fixtures/book_authors.yml
|
160
161
|
- test/fixtures/book_comments.yml
|
161
162
|
- test/fixtures/books.yml
|
162
163
|
- test/fixtures/categories.yml
|
@@ -238,6 +239,7 @@ test_files:
|
|
238
239
|
- test/controllers/controller_test.rb
|
239
240
|
- test/fixtures/active_record.rb
|
240
241
|
- test/fixtures/author_details.yml
|
242
|
+
- test/fixtures/book_authors.yml
|
241
243
|
- test/fixtures/book_comments.yml
|
242
244
|
- test/fixtures/books.yml
|
243
245
|
- test/fixtures/categories.yml
|