Skip to content

Commit f0189a3

Browse files
committed
Rename Railsapi => RailsAPI.
Also cleanup some vestigial bits of JR’s README.
1 parent d7e30dc commit f0189a3

14 files changed

+103
-132
lines changed

README.md

Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Railsapi::Resources
1+
# RailsAPI::Resources
22

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
55
JR and certainly contains inaccurate information. In addition features may be added or removed at any point. Please do
66
not base production software on this library.**
77

@@ -10,7 +10,7 @@ not base production software on this library.**
1010
* [Installation] (#installation)
1111
* [Usage] (#usage)
1212
* [Resources] (#resources)
13-
* [Railsapi::Resource] (#jsonapiresource)
13+
* [RailsAPI::Resource] (#railsapiresource)
1414
* [Attributes] (#attributes)
1515
* [Primary Key] (#primary-key)
1616
* [Model Name] (#model-name)
@@ -46,14 +46,14 @@ Resource definitions should by convention be placed in a directory under app nam
4646
name should be the single underscored name of the model that backs the resource with `_resource.rb` appended. For example,
4747
a `Contact` model's resource should have a class named `ContactResource` defined in a file named `contact_resource.rb`.
4848

49-
#### Railsapi::Resource
49+
#### RailsAPI::Resource
5050

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`.
5252

5353
For example:
5454

5555
```ruby
56-
class ContactResource < Railsapi::Resource
56+
class ContactResource < RailsAPI::Resource
5757
end
5858
```
5959

@@ -66,7 +66,7 @@ Because abstract resources do not expect to be backed by a model, they won't att
6666
or any of its relationships.
6767

6868
```ruby
69-
class BaseResource < Railsapi::Resource
69+
class BaseResource < RailsAPI::Resource
7070
abstract
7171

7272
has_one :creator
@@ -86,7 +86,7 @@ Immutable resources can be used as the basis for a heterogeneous collection. Res
8686
still be mutated through their own type-specific endpoints.
8787

8888
```ruby
89-
class VehicleResource < Railsapi::Resource
89+
class VehicleResource < RailsAPI::Resource
9090
immutable
9191

9292
has_one :owner
@@ -102,18 +102,8 @@ class BoatResource < VehicleResource
102102
attributes :length_at_water_line
103103
has_one :captain
104104
end
105-
106-
# routes
107-
jsonapi_resources :vehicles
108-
jsonapi_resources :cars
109-
jsonapi_resources :boats
110-
111105
```
112106

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-
117107
#### Attributes
118108

119109
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
122112
For example:
123113

124114
```ruby
125-
class ContactResource < Railsapi::Resource
115+
class ContactResource < RailsAPI::Resource
126116
attribute :name_first
127117
attributes :name_last, :email, :twitter
128118
end
@@ -137,7 +127,7 @@ This allows a resource's methods to access the underlying model.
137127
For example, a computed attribute for `full_name` could be defined as such:
138128

139129
```ruby
140-
class ContactResource < Railsapi::Resource
130+
class ContactResource < RailsAPI::Resource
141131
attributes :name_first, :name_last, :email, :twitter
142132
attribute :full_name
143133

@@ -155,7 +145,7 @@ the `fetchable_fields` method.
155145
Here's an example that prevents guest users from seeing the `email` field:
156146

157147
```ruby
158-
class AuthorResource < Railsapi::Resource
148+
class AuthorResource < RailsAPI::Resource
159149
attributes :name, :email
160150
model_name 'Person'
161151
has_many :posts
@@ -181,7 +171,7 @@ the `update` or `create` methods, override the `self.updatable_fields` and `self
181171
This example prevents `full_name` from being set:
182172

183173
```ruby
184-
class ContactResource < Railsapi::Resource
174+
class ContactResource < RailsAPI::Resource
185175
attributes :name_first, :name_last, :full_name
186176

187177
def full_name
@@ -205,15 +195,13 @@ By using the context you have the option to determine the creatable and updatabl
205195

206196
##### Sortable Attributes
207197

208-
Railsapi Resources supports [sorting primary resources by multiple sort criteria](http://jsonapi.org/format/#fetching-sorting).
209-
210198
By default all attributes are assumed to be sortable. To prevent some attributes from being sortable, override the
211199
`self.sortable_fields` method on a resource.
212200

213201
Here's an example that prevents sorting by post's `body`:
214202

215203
```ruby
216-
class PostResource < Railsapi::Resource
204+
class PostResource < RailsAPI::Resource
217205
attributes :title, :body
218206

219207
def self.sortable_fields(context)
@@ -239,7 +227,7 @@ class SpokenLanguage < ActiveRecord::Base
239227
end
240228

241229
# Resource with getters and setter
242-
class PersonResource < Railsapi::Resource
230+
class PersonResource < RailsAPI::Resource
243231
attributes :name, :email, :spoken_languages
244232

245233
# Getter
@@ -264,49 +252,32 @@ If the underlying model does not use `id` as the primary key _and_ does not supp
264252
must use the `primary_key` method to tell the resource which field on the model to use as the primary key. **Note:**
265253
this _must_ be the actual primary key of the model.
266254

267-
By default only integer values are allowed for primary key. To change this behavior you can set the `resource_key_type`
268-
configuration option:
269-
270-
```ruby
271-
JSONAPI.configure do |config|
272-
# Allowed values are :integer(default), :uuid, :string, or a proc
273-
config.resource_key_type = :uuid
274-
end
275-
```
255+
By default only integer values are used for primary key.
276256

277257
##### Override key type on a resource
278258

279259
You can override the default resource key type on a per-resource basis by calling `key_type` in the resource class,
280260
with the same allowed values as the `resource_key_type` configuration option.
281261

282262
```ruby
283-
class ContactResource < Railsapi::Resource
263+
class ContactResource < RailsAPI::Resource
284264
attribute :id
285265
attributes :name_first, :name_last, :email, :twitter
286266
key_type :uuid
287267
end
288268
```
289269

290-
##### Custom resource key validators
291-
292-
If you need more control over the key, you can override the #verify_key method on your resource, or set a lambda that
293-
accepts key and context arguments in `config/initializers/jsonapi_resources.rb`:
294-
295-
```ruby
296-
JSONAPI.configure do |config|
297-
config.resource_key_type = -> (key, context) { key && String(key) }
298-
end
299-
```
300-
301270
#### Model Name
302271

303272
The name of the underlying model is inferred from the Resource name. It can be overridden by use of the `model_name`
304273
method. For example:
305274

306275
```ruby
307-
class AuthorResource < Railsapi::Resource
308-
attribute :name
276+
class AuthorResource < RailsAPI::Resource
309277
model_name 'Person'
278+
279+
attribute :name
280+
310281
has_many :posts
311282
end
312283
```
@@ -320,7 +291,7 @@ resource names. It can also fail when using namespaced models. In this case a `m
320291
names to resources. For example:
321292

322293
```ruby
323-
class AuthorResource < Railsapi::Resource
294+
class AuthorResource < RailsAPI::Resource
324295
attribute :name
325296
model_name 'Person'
326297
model_hint model: Commenter, resource: :special_person
@@ -334,7 +305,7 @@ Note that when `model_name` is set a corresponding `model_hint` is also added. T
334305
`add_model_hint` option set to false. For example:
335306

336307
```ruby
337-
class AuthorResource < Railsapi::Resource
308+
class AuthorResource < RailsAPI::Resource
338309
model_name 'Legacy::Person', add_model_hint: false
339310
end
340311
```
@@ -352,7 +323,7 @@ Here's a simple example using the `relationship` method where a post has a singl
352323
posts:
353324

354325
```ruby
355-
class PostResource < Railsapi::Resource
326+
class PostResource < RailsAPI::Resource
356327
attributes :title, :body
357328

358329
relationship :author, to: :one
@@ -362,7 +333,7 @@ end
362333
And the corresponding author:
363334

364335
```ruby
365-
class AuthorResource < Railsapi::Resource
336+
class AuthorResource < RailsAPI::Resource
366337
attribute :name
367338

368339
relationship :posts, to: :many
@@ -372,7 +343,7 @@ end
372343
And here's the equivalent resources using the `has_one` and `has_many` methods:
373344

374345
```ruby
375-
class PostResource < Railsapi::Resource
346+
class PostResource < RailsAPI::Resource
376347
attributes :title, :body
377348

378349
has_one :author
@@ -382,7 +353,7 @@ end
382353
And the corresponding author:
383354

384355
```ruby
385-
class AuthorResource < Railsapi::Resource
356+
class AuthorResource < RailsAPI::Resource
386357
attribute :name
387358

388359
has_many :posts
@@ -398,36 +369,36 @@ The relationship methods (`relationship`, `has_one`, and `has_many`) support the
398369
* `acts_as_set` - allows the entire set of related records to be replaced in one operation. Defaults to false if not set.
399370
* `polymorphic` - set to true to identify relationships that are polymorphic.
400371
* `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.
402372

403373
`to_one` relationships support the additional option:
374+
404375
* `foreign_key_on` - defaults to `:self`. To indicate that the foreign key is on the related resource specify `:related`.
405376

406377
Examples:
407378

408379
```ruby
409-
class CommentResource < Railsapi::Resource
380+
class CommentResource < RailsAPI::Resource
410381
attributes :body
411382
has_one :post
412383
has_one :author, class_name: 'Person'
413384
has_many :tags, acts_as_set: true
414385
end
415386

416-
class ExpenseEntryResource < Railsapi::Resource
387+
class ExpenseEntryResource < RailsAPI::Resource
417388
attributes :cost, :transaction_date
418389

419390
has_one :currency, class_name: 'Currency', foreign_key: 'currency_code'
420391
has_one :employee
421392
end
422393

423-
class TagResource < Railsapi::Resource
394+
class TagResource < RailsAPI::Resource
424395
attributes :name
425396
has_one :taggable, polymorphic: true
426397
end
427398
```
428399

429400
```ruby
430-
class BookResource < Railsapi::Resource
401+
class BookResource < RailsAPI::Resource
431402

432403
# Only book_admins may see unapproved comments for a book. Using
433404
# a lambda to select the correct relation on the model
@@ -449,8 +420,8 @@ The polymorphic relationship will require the resource and controller to exist,
449420
error.
450421

451422
```ruby
452-
class TaggableResource < Railsapi::Resource; end
453-
class TaggablesController < Railsapi::ResourceController; end
423+
class TaggableResource < RailsAPI::Resource; end
424+
class TaggablesController < RailsAPI::ResourceController; end
454425
```
455426

456427
#### Callbacks
@@ -461,7 +432,7 @@ used to from `ActiveRecord`.
461432
For example, you might use a callback to perform authorization on your resource before an action.
462433

463434
```ruby
464-
class BaseResource < Railsapi::Resource
435+
class BaseResource < RailsAPI::Resource
465436
before_create :authorize_create
466437

467438
def authorize_create
@@ -475,9 +446,9 @@ The types of supported callbacks are:
475446
- `after`
476447
- `around`
477448

478-
##### `Railsapi::ResourceCallbacks`
449+
##### `RailsAPI::ResourceCallbacks`
479450

480-
Callbacks can be defined for the following `Railsapi::Resource` events:
451+
Callbacks can be defined for the following `RailsAPI::Resource` events:
481452

482453
- `:create`
483454
- `:update`
@@ -493,7 +464,7 @@ Callbacks can be defined for the following `Railsapi::Resource` events:
493464

494465
#### Namespaces
495466

496-
Railsapi::Resources supports namespacing of resources. With namespacing you can version your API.
467+
RailsAPI::Resources supports namespacing of resources. With namespacing you can version your API.
497468

498469
## Contributing
499470

lib/railsapi/exceptions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Railsapi
1+
module RailsAPI
22
module Exceptions
33
class Error < RuntimeError; end
44

lib/railsapi/relationship.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Railsapi
1+
module RailsAPI
22
class Relationship
33
attr_reader :acts_as_set, :foreign_key, :type, :options, :name,
44
:class_name, :polymorphic, :parent_resource

0 commit comments

Comments
 (0)