You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62-34Lines changed: 62 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -212,11 +212,33 @@ class AuthorResource < JSONAPI::Resource
212
212
end
213
213
```
214
214
215
-
#### Associations
215
+
#### Relationships
216
216
217
-
Related resources need to be specified in the resource. These are declared with the `has_one` and the `has_many` methods.
217
+
Related resources need to be specified in the resource. These may be declared with the `relationship` or the `has_one`
218
+
and the `has_many` methods.
218
219
219
-
Here's a simple example where a post has a single author and an author can have many posts:
220
+
Here's a simple example using the `relationship` method where a post has a single author and an author can have many
221
+
posts:
222
+
223
+
```ruby
224
+
classPostResource < JSONAPI::Resource
225
+
attribute :title, :body
226
+
227
+
relationship :author, to::one
228
+
end
229
+
```
230
+
231
+
And the corresponding author:
232
+
233
+
```ruby
234
+
classAuthorResource < JSONAPI::Resource
235
+
attribute :name
236
+
237
+
relationship :posts, to::many
238
+
end
239
+
```
240
+
241
+
And here's the equivalent resources using the `has_one` and `has_many` methods:
220
242
221
243
```ruby
222
244
classPostResource < JSONAPI::Resource
@@ -238,14 +260,17 @@ end
238
260
239
261
##### Options
240
262
241
-
The association methods support the following options:
263
+
The relationship methods (`relationship`, `has_one`, and `has_many`) support the following options:
242
264
243
265
*`class_name` - a string specifying the underlying class for the related resource
244
266
*`foreign_key` - the method on the resource used to fetch the related resource. Defaults to `<resource_name>_id` for has_one and `<resource_name>_ids` for has_many relationships.
245
267
*`acts_as_set` - allows the entire set of related records to be replaced in one operation. Defaults to false if not set.
246
-
*`polymorphic` - set to true to identify associations that are polymorphic.
268
+
*`polymorphic` - set to true to identify relationships that are polymorphic.
247
269
*`relation_name` - the name of the relation to use on the model. A lambda may be provided which allows conditional selection of the relation based on the context.
248
270
271
+
`to_one` relationships support the additional option:
272
+
*`foreign_key_on` - defaults to `:self`. To indicate that the foreign key is on the related resource specify `:related`.
273
+
249
274
Examples:
250
275
251
276
```ruby
@@ -288,7 +313,8 @@ class BookResource < JSONAPI::Resource
288
313
end
289
314
```
290
315
291
-
The polymorphic association will require the resource and controller to exist, although routing to them will cause an error.
316
+
The polymorphic relationship will require the resource and controller to exist, although routing to them will cause an
317
+
error.
292
318
293
319
```ruby
294
320
classTaggableResource < JSONAPI::Resource; end
@@ -298,7 +324,8 @@ class TaggablesController < JSONAPI::ResourceController; end
298
324
#### Filters
299
325
300
326
Filters for locating objects of the resource type are specified in the resource definition. Single filters can be
301
-
declared using the `filter` method, and multiple filters can be declared with the `filters` method on the resource class.
327
+
declared using the `filter` method, and multiple filters can be declared with the `filters` method on the resource
328
+
class.
302
329
303
330
For example:
304
331
@@ -311,7 +338,8 @@ class ContactResource < JSONAPI::Resource
311
338
end
312
339
```
313
340
314
-
Then a request could pass in a filter for example `http://example.com/contacts?filter[name_last]=Smith` and the system will find all people where the last name exactly matches Smith.
341
+
Then a request could pass in a filter for example `http://example.com/contacts?filter[name_last]=Smith` and the system
342
+
will find all people where the last name exactly matches Smith.
315
343
316
344
##### Default Filters
317
345
@@ -357,7 +385,7 @@ end
357
385
```
358
386
359
387
When you create a relationship, a method is created to fetch record(s) for that relationship. This method calls
360
-
`records_for(association_name)` by default.
388
+
`records_for(relationship_name)` by default.
361
389
362
390
```ruby
363
391
classPostResource < JSONAPI::Resource
@@ -375,13 +403,13 @@ end
375
403
376
404
```
377
405
378
-
For example, you may want raise an error if the user is not authorized to view the associated records.
406
+
For example, you may want raise an error if the user is not authorized to view the related records.
379
407
380
408
```ruby
381
409
classBaseResource < JSONAPI::Resource
382
-
defrecords_for(association_name, options={})
410
+
defrecords_for(relationship_name, options={})
383
411
context = options[:context]
384
-
records = model.public_send(association_name)
412
+
records = model.public_send(relationship_name)
385
413
386
414
unless context.current_user.can_view?(records)
387
415
raiseNotAuthorizedError
@@ -564,17 +592,17 @@ Callbacks can also be defined for `JSONAPI::OperationsProcessor` events:
564
592
-`:operation`: Any individual operation.
565
593
-`:find_operation`: A `find_operation`.
566
594
-`:show_operation`: A `show_operation`.
567
-
-`:show_association_operation`: A `show_association_operation`.
595
+
-`:show_relationship_operation`: A `show_relationship_operation`.
568
596
-`:show_related_resource_operation`: A `show_related_resource_operation`.
569
597
-`:show_related_resources_operation`: A `show_related_resources_operation`.
570
598
-`:create_resource_operation`: A `create_resource_operation`.
571
599
-`:remove_resource_operation`: A `remove_resource_operation`.
572
600
-`:replace_fields_operation`: A `replace_fields_operation`.
573
-
-`:replace_has_one_association_operation`: A `replace_has_one_association_operation`.
574
-
-`:create_has_many_association_operation`: A `create_has_many_association_operation`.
575
-
-`:replace_has_many_association_operation`: A `replace_has_many_association_operation`.
576
-
-`:remove_has_many_association_operation`: A `remove_has_many_association_operation`.
577
-
-`:remove_has_one_association_operation`: A `remove_has_one_association_operation`.
601
+
-`:replace_has_one_relationship_operation`: A `replace_has_one_relationship_operation`.
602
+
-`:create_has_many_relationship_operation`: A `create_has_many_relationship_operation`.
603
+
-`:replace_has_many_relationship_operation`: A `replace_has_many_relationship_operation`.
604
+
-`:remove_has_many_relationship_operation`: A `remove_has_many_relationship_operation`.
605
+
-`:remove_has_one_relationship_operation`: A `remove_has_one_relationship_operation`.
578
606
579
607
The operation callbacks have access to two meta data hashes, `@operations_meta` and `@operation_meta`, two links hashes,
580
608
`@operations_links` and `@operation_links`, the full list of `@operations`, each individual `@operation` and the
@@ -875,7 +903,7 @@ An array of resources. Nested resources can be specified with dot notation.
875
903
A hash of resource types and arrays of fields for each resource type.
876
904
877
905
*Purpose*: determines which fields are serialized for a resource type. This encompasses both attributes and
878
-
association ids in the links section for a resource. Fields are global for a resource type.
906
+
relationship ids in the links section for a resource. Fields are global for a resource type.
@@ -904,7 +932,7 @@ JR has a couple of helper methods available to assist you with setting up routes
904
932
##### `jsonapi_resources`
905
933
906
934
Like `resources` in `ActionDispatch`, `jsonapi_resources` provides resourceful routes mapping between HTTP verbs and URLs
907
-
and controller actions. This will also setup mappings for relationship URLs for a resource's associations. For example:
935
+
and controller actions. This will also setup mappings for relationship URLs for a resource's relationships. For example:
908
936
909
937
```ruby
910
938
Rails.application.routes.draw do
@@ -917,20 +945,20 @@ gives the following routes
917
945
918
946
```
919
947
Prefix Verb URI Pattern Controller#Action
920
-
contact_relationships_phone_numbers GET /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#show_association {:association=>"phone_numbers"}
921
-
POST /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#create_association {:association=>"phone_numbers"}
contact_phone_numbers GET /contacts/:contact_id/phone-numbers(.:format) phone_numbers#get_related_resources {:association=>"phone_numbers", :source=>"contacts"}
948
+
contact_relationships_phone_numbers GET /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#show_relationship {:relationship=>"phone_numbers"}
949
+
POST /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#create_relationship {:relationship=>"phone_numbers"}
contact_phone_numbers GET /contacts/:contact_id/phone-numbers(.:format) phone_numbers#get_related_resources {:relationship=>"phone_numbers", :source=>"contacts"}
924
952
contacts GET /contacts(.:format) contacts#index
925
953
POST /contacts(.:format) contacts#create
926
954
contact GET /contacts/:id(.:format) contacts#show
927
955
PATCH /contacts/:id(.:format) contacts#update
928
956
PUT /contacts/:id(.:format) contacts#update
929
957
DELETE /contacts/:id(.:format) contacts#destroy
930
-
phone_number_relationships_contact GET /phone-numbers/:phone_number_id/relationships/contact(.:format) phone_numbers#show_association {:association=>"contact"}
phone_number_contact GET /phone-numbers/:phone_number_id/contact(.:format) contacts#get_related_resource {:association=>"contact", :source=>"phone_numbers"}
958
+
phone_number_relationships_contact GET /phone-numbers/:phone_number_id/relationships/contact(.:format) phone_numbers#show_relationship {:relationship=>"contact"}
phone_number_contact GET /phone-numbers/:phone_number_id/contact(.:format) contacts#get_related_resource {:relationship=>"contact", :source=>"phone_numbers"}
934
962
phone_numbers GET /phone-numbers(.:format) phone_numbers#index
935
963
POST /phone-numbers(.:format) phone_numbers#create
936
964
phone_number GET /phone-numbers/:id(.:format) phone_numbers#show
@@ -995,9 +1023,9 @@ end
995
1023
Gives the following routes:
996
1024
997
1025
```
998
-
contact_relationships_phone_numbers GET /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#show_association {:association=>"phone_numbers"}
999
-
POST /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#create_association {:association=>"phone_numbers"}
contact_relationships_phone_numbers GET /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#show_relationship {:relationship=>"phone_numbers"}
1027
+
POST /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#create_relationship {:relationship=>"phone_numbers"}
0 commit comments