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
Rename HasMany to ToMany and HasOne to ToOne.
Brings the project more in line with the JSON API terminology.
This will break existing code that uses callbacks or overrides some
resource methods.
Copy file name to clipboardExpand all lines: README.md
+60-35Lines changed: 60 additions & 35 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,15 +260,15 @@ 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
249
-
`has_one` associations support the additional option:
271
+
`to_one` relationships support the additional option:
250
272
*`foreign_key_on` - defaults to `:self`. To indicate that the foreign key is on the related resource specify `:related`.
251
273
252
274
Examples:
@@ -291,7 +313,8 @@ class BookResource < JSONAPI::Resource
291
313
end
292
314
```
293
315
294
-
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.
295
318
296
319
```ruby
297
320
classTaggableResource < JSONAPI::Resource; end
@@ -301,7 +324,8 @@ class TaggablesController < JSONAPI::ResourceController; end
301
324
#### Filters
302
325
303
326
Filters for locating objects of the resource type are specified in the resource definition. Single filters can be
304
-
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.
305
329
306
330
For example:
307
331
@@ -314,7 +338,8 @@ class ContactResource < JSONAPI::Resource
314
338
end
315
339
```
316
340
317
-
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.
318
343
319
344
##### Default Filters
320
345
@@ -360,7 +385,7 @@ end
360
385
```
361
386
362
387
When you create a relationship, a method is created to fetch record(s) for that relationship. This method calls
363
-
`records_for(association_name)` by default.
388
+
`records_for(relationship_name)` by default.
364
389
365
390
```ruby
366
391
classPostResource < JSONAPI::Resource
@@ -378,13 +403,13 @@ end
378
403
379
404
```
380
405
381
-
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.
382
407
383
408
```ruby
384
409
classBaseResource < JSONAPI::Resource
385
-
defrecords_for(association_name, options={})
410
+
defrecords_for(relationship_name, options={})
386
411
context = options[:context]
387
-
records = model.public_send(association_name)
412
+
records = model.public_send(relationship_name)
388
413
389
414
unless context.current_user.can_view?(records)
390
415
raiseNotAuthorizedError
@@ -567,17 +592,17 @@ Callbacks can also be defined for `JSONAPI::OperationsProcessor` events:
567
592
-`:operation`: Any individual operation.
568
593
-`:find_operation`: A `find_operation`.
569
594
-`:show_operation`: A `show_operation`.
570
-
-`:show_association_operation`: A `show_association_operation`.
595
+
-`:show_relationship_operation`: A `show_relationship_operation`.
571
596
-`:show_related_resource_operation`: A `show_related_resource_operation`.
572
597
-`:show_related_resources_operation`: A `show_related_resources_operation`.
573
598
-`:create_resource_operation`: A `create_resource_operation`.
574
599
-`:remove_resource_operation`: A `remove_resource_operation`.
575
600
-`:replace_fields_operation`: A `replace_fields_operation`.
576
-
-`:replace_has_one_association_operation`: A `replace_has_one_association_operation`.
577
-
-`:create_has_many_association_operation`: A `create_has_many_association_operation`.
578
-
-`:replace_has_many_association_operation`: A `replace_has_many_association_operation`.
579
-
-`:remove_has_many_association_operation`: A `remove_has_many_association_operation`.
580
-
-`: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`.
581
606
582
607
The operation callbacks have access to two meta data hashes, `@operations_meta` and `@operation_meta`, two links hashes,
583
608
`@operations_links` and `@operation_links`, the full list of `@operations`, each individual `@operation` and the
@@ -878,7 +903,7 @@ An array of resources. Nested resources can be specified with dot notation.
878
903
A hash of resource types and arrays of fields for each resource type.
879
904
880
905
*Purpose*: determines which fields are serialized for a resource type. This encompasses both attributes and
881
-
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.
@@ -907,7 +932,7 @@ JR has a couple of helper methods available to assist you with setting up routes
907
932
##### `jsonapi_resources`
908
933
909
934
Like `resources` in `ActionDispatch`, `jsonapi_resources` provides resourceful routes mapping between HTTP verbs and URLs
910
-
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:
911
936
912
937
```ruby
913
938
Rails.application.routes.draw do
@@ -920,20 +945,20 @@ gives the following routes
920
945
921
946
```
922
947
Prefix Verb URI Pattern Controller#Action
923
-
contact_relationships_phone_numbers GET /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#show_association {:association=>"phone_numbers"}
924
-
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"}
927
952
contacts GET /contacts(.:format) contacts#index
928
953
POST /contacts(.:format) contacts#create
929
954
contact GET /contacts/:id(.:format) contacts#show
930
955
PATCH /contacts/:id(.:format) contacts#update
931
956
PUT /contacts/:id(.:format) contacts#update
932
957
DELETE /contacts/:id(.:format) contacts#destroy
933
-
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"}
937
962
phone_numbers GET /phone-numbers(.:format) phone_numbers#index
938
963
POST /phone-numbers(.:format) phone_numbers#create
939
964
phone_number GET /phone-numbers/:id(.:format) phone_numbers#show
@@ -998,9 +1023,9 @@ end
998
1023
Gives the following routes:
999
1024
1000
1025
```
1001
-
contact_relationships_phone_numbers GET /contacts/:contact_id/relationships/phone-numbers(.:format) contacts#show_association {:association=>"phone_numbers"}
1002
-
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