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
+38-67
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
-
# Railsapi::Resources
1
+
# RailsAPI::Resources
2
2
3
-
**Note: Railsapi Resources is an experiment at breaking out the Resource class from JSONAPI-Resources. This project
4
-
should be considered a work in progress and may be abandoned at any point. Also this README was quickly extracted from
3
+
**Note: RailsAPI::Resources is an experiment that extracts the `Resource` class from [JSONAPI::Resources](https://github.com/cerebris/jsonapi-resources) ("JR"). This project
4
+
should be considered a work in progress and may be abandoned at any point. This README was quickly extracted from
5
5
JR and certainly contains inaccurate information. In addition features may be added or removed at any point. Please do
6
6
not base production software on this library.**
7
7
@@ -10,7 +10,7 @@ not base production software on this library.**
10
10
*[Installation] (#installation)
11
11
*[Usage] (#usage)
12
12
*[Resources] (#resources)
13
-
*[Railsapi::Resource] (#jsonapiresource)
13
+
*[RailsAPI::Resource] (#railsapiresource)
14
14
*[Attributes] (#attributes)
15
15
*[Primary Key] (#primary-key)
16
16
*[Model Name] (#model-name)
@@ -46,14 +46,14 @@ Resource definitions should by convention be placed in a directory under app nam
46
46
name should be the single underscored name of the model that backs the resource with `_resource.rb` appended. For example,
47
47
a `Contact` model's resource should have a class named `ContactResource` defined in a file named `contact_resource.rb`.
48
48
49
-
#### Railsapi::Resource
49
+
#### RailsAPI::Resource
50
50
51
-
Resources must be derived from `Railsapi::Resource`, or a class that is itself derived from `Railsapi::Resource`.
51
+
Resources must be derived from `RailsAPI::Resource`, or a class that is itself derived from `RailsAPI::Resource`.
52
52
53
53
For example:
54
54
55
55
```ruby
56
-
classContactResource < Railsapi::Resource
56
+
classContactResource < RailsAPI::Resource
57
57
end
58
58
```
59
59
@@ -66,7 +66,7 @@ Because abstract resources do not expect to be backed by a model, they won't att
66
66
or any of its relationships.
67
67
68
68
```ruby
69
-
classBaseResource < Railsapi::Resource
69
+
classBaseResource < RailsAPI::Resource
70
70
abstract
71
71
72
72
has_one :creator
@@ -86,7 +86,7 @@ Immutable resources can be used as the basis for a heterogeneous collection. Res
86
86
still be mutated through their own type-specific endpoints.
87
87
88
88
```ruby
89
-
classVehicleResource < Railsapi::Resource
89
+
classVehicleResource < RailsAPI::Resource
90
90
immutable
91
91
92
92
has_one :owner
@@ -102,18 +102,8 @@ class BoatResource < VehicleResource
102
102
attributes :length_at_water_line
103
103
has_one :captain
104
104
end
105
-
106
-
# routes
107
-
jsonapi_resources :vehicles
108
-
jsonapi_resources :cars
109
-
jsonapi_resources :boats
110
-
111
105
```
112
106
113
-
In the above example vehicles are immutable. A call to `/vehicles` or `/vehicles/1` will return vehicles with types
114
-
of either `car` or `boat`. But calls to PUT or POST a `car` must be made to `/cars`. The rails models backing the above
115
-
code use Single Table Inheritance.
116
-
117
107
#### Attributes
118
108
119
109
Any of a resource's attributes that are accessible must be explicitly declared. Single attributes can be declared using
@@ -122,7 +112,7 @@ the `attribute` method, and multiple attributes can be declared with the `attrib
122
112
For example:
123
113
124
114
```ruby
125
-
classContactResource < Railsapi::Resource
115
+
classContactResource < RailsAPI::Resource
126
116
attribute :name_first
127
117
attributes :name_last, :email, :twitter
128
118
end
@@ -137,7 +127,7 @@ This allows a resource's methods to access the underlying model.
137
127
For example, a computed attribute for `full_name` could be defined as such:
@@ -334,7 +305,7 @@ Note that when `model_name` is set a corresponding `model_hint` is also added. T
334
305
`add_model_hint` option set to false. For example:
335
306
336
307
```ruby
337
-
classAuthorResource < Railsapi::Resource
308
+
classAuthorResource < RailsAPI::Resource
338
309
model_name 'Legacy::Person', add_model_hint:false
339
310
end
340
311
```
@@ -352,7 +323,7 @@ Here's a simple example using the `relationship` method where a post has a singl
352
323
posts:
353
324
354
325
```ruby
355
-
classPostResource < Railsapi::Resource
326
+
classPostResource < RailsAPI::Resource
356
327
attributes :title, :body
357
328
358
329
relationship :author, to::one
@@ -362,7 +333,7 @@ end
362
333
And the corresponding author:
363
334
364
335
```ruby
365
-
classAuthorResource < Railsapi::Resource
336
+
classAuthorResource < RailsAPI::Resource
366
337
attribute :name
367
338
368
339
relationship :posts, to::many
@@ -372,7 +343,7 @@ end
372
343
And here's the equivalent resources using the `has_one` and `has_many` methods:
373
344
374
345
```ruby
375
-
classPostResource < Railsapi::Resource
346
+
classPostResource < RailsAPI::Resource
376
347
attributes :title, :body
377
348
378
349
has_one :author
@@ -382,7 +353,7 @@ end
382
353
And the corresponding author:
383
354
384
355
```ruby
385
-
classAuthorResource < Railsapi::Resource
356
+
classAuthorResource < RailsAPI::Resource
386
357
attribute :name
387
358
388
359
has_many :posts
@@ -398,36 +369,36 @@ The relationship methods (`relationship`, `has_one`, and `has_many`) support the
398
369
*`acts_as_set` - allows the entire set of related records to be replaced in one operation. Defaults to false if not set.
399
370
*`polymorphic` - set to true to identify relationships that are polymorphic.
400
371
*`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.
401
-
*`always_include_linkage_data` - if set to true, the relationship includes linkage data. Defaults to false if not set.
402
372
403
373
`to_one` relationships support the additional option:
374
+
404
375
*`foreign_key_on` - defaults to `:self`. To indicate that the foreign key is on the related resource specify `:related`.
class BadlyNamedAttributesResource < Railsapi::Resource
210
+
class BadlyNamedAttributesResource < RailsAPI::Resource
211
211
has_one :#{key}
212
212
end
213
213
CODE
@@ -217,7 +217,7 @@ class BadlyNamedAttributesResource < Railsapi::Resource
217
217
%w(typesids).eachdo |key|
218
218
_out,err=capture_iodo
219
219
eval<<-CODE
220
-
class BadlyNamedAttributesResource < Railsapi::Resource
220
+
class BadlyNamedAttributesResource < RailsAPI::Resource
221
221
has_many :#{key}
222
222
end
223
223
CODE
@@ -229,18 +229,18 @@ class BadlyNamedAttributesResource < Railsapi::Resource
229
229
deftest_abstract_warning
230
230
_out,err=capture_iodo
231
231
eval<<-CODE
232
-
class NoModelResource < Railsapi::Resource
232
+
class NoModelResource < RailsAPI::Resource
233
233
end
234
234
NoModelResource._model_class
235
235
CODE
236
236
end
237
-
assert_match"[MODEL NOT FOUND] Model could not be found for Railsapi::ResourceTest::NoModelResource. If this a base Resource declare it as abstract.\n",err
237
+
assert_match"[MODEL NOT FOUND] Model could not be found for RailsAPI::ResourceTest::NoModelResource. If this a base Resource declare it as abstract.\n",err
238
238
end
239
239
240
240
deftest_no_warning_when_abstract
241
241
_out,err=capture_iodo
242
242
eval<<-CODE
243
-
class NoModelAbstractResource < Railsapi::Resource
243
+
class NoModelAbstractResource < RailsAPI::Resource
244
244
abstract
245
245
end
246
246
NoModelAbstractResource._model_class
@@ -252,7 +252,7 @@ class NoModelAbstractResource < Railsapi::Resource
0 commit comments