Skip to content

Commit f0189a3

Browse files
committedFeb 29, 2016
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

+38-67
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

+1-1
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

+1-1
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

‎lib/railsapi/resource.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require 'railsapi/resource_fields'
55
require 'railsapi/resource_records'
66

7-
module Railsapi
7+
module RailsAPI
88
class Resource
99
include ResourceCallbacks
1010
include ResourceRelationships
@@ -98,16 +98,16 @@ def model_error_messages
9898
# ```
9999
def _save
100100
unless @model.valid?
101-
fail Railsapi::Exceptions::ValidationErrors.new(self)
101+
fail RailsAPI::Exceptions::ValidationErrors.new(self)
102102
end
103103

104104
if defined? @model.save
105105
saved = @model.save(validate: false)
106106
unless saved
107107
if @model.errors.present?
108-
fail Railsapi::Exceptions::ValidationErrors.new(self)
108+
fail RailsAPI::Exceptions::ValidationErrors.new(self)
109109
else
110-
fail Railsapi::Exceptions::SaveFailed.new
110+
fail RailsAPI::Exceptions::SaveFailed.new
111111
end
112112
end
113113
else
@@ -121,7 +121,7 @@ def _save
121121

122122
def _remove
123123
unless @model.destroy
124-
fail Railsapi::Exceptions::ValidationErrors.new(self)
124+
fail RailsAPI::Exceptions::ValidationErrors.new(self)
125125
end
126126
:completed
127127
end
@@ -161,7 +161,7 @@ def resource_for(type)
161161
resource_name = _resource_name_from_type(type_with_module)
162162
resource = resource_name.safe_constantize if resource_name
163163
if resource.nil?
164-
fail NameError, "Railsapi: Could not find resource '#{type}'. (Class #{resource_name} not found)"
164+
fail NameError, "RailsAPI: Could not find resource '#{type}'. (Class #{resource_name} not found)"
165165
end
166166
resource
167167
end
@@ -199,7 +199,7 @@ def model_name(model, options = {})
199199

200200
def model_hint(model: _model_name, resource: _type)
201201
model_name = ((model.is_a?(Class)) && (model < ActiveRecord::Base)) ? model.name : model
202-
resource_type = ((resource.is_a?(Class)) && (resource < Railsapi::Resource)) ? resource._type : resource.to_s
202+
resource_type = ((resource.is_a?(Class)) && (resource < RailsAPI::Resource)) ? resource._type : resource.to_s
203203

204204
_model_hints[model_name.to_s.gsub('::', '/').underscore] = resource_type.to_s
205205
end
@@ -234,7 +234,7 @@ def verify_key(key, context = nil)
234234
when :string
235235
return if key.nil?
236236
if key.to_s.include?(',')
237-
raise Railsapi::Exceptions::InvalidFieldValue.new(:id, key)
237+
raise RailsAPI::Exceptions::InvalidFieldValue.new(:id, key)
238238
else
239239
key
240240
end
@@ -243,13 +243,13 @@ def verify_key(key, context = nil)
243243
if key.to_s.match(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/)
244244
key
245245
else
246-
raise Railsapi::Exceptions::InvalidFieldValue.new(:id, key)
246+
raise RailsAPI::Exceptions::InvalidFieldValue.new(:id, key)
247247
end
248248
else
249249
key_type.call(key, context)
250250
end
251251
rescue
252-
raise Railsapi::Exceptions::InvalidFieldValue.new(:id, key)
252+
raise RailsAPI::Exceptions::InvalidFieldValue.new(:id, key)
253253
end
254254

255255
# override to allow for key processing and checking
@@ -301,7 +301,7 @@ def _model_class
301301
end
302302

303303
def module_path
304-
if name == 'Railsapi::Resource'
304+
if name == 'RailsAPI::Resource'
305305
''
306306
else
307307
name =~ /::[^:]+\Z/ ? ($`.freeze.gsub('::', '/') + '/').underscore : ''

‎lib/railsapi/resource_attributes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Railsapi
1+
module RailsAPI
22
module ResourceAttributes
33
def self.included(base)
44
base.class_eval do

‎lib/railsapi/resource_callbacks.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'active_support/callbacks'
22

3-
module Railsapi
3+
module RailsAPI
44
module ResourceCallbacks
55
def self.included(base)
66
base.class_eval do

‎lib/railsapi/resource_fields.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Railsapi
1+
module RailsAPI
22
module ResourceFields
33
def self.included(base)
44
base.class_eval do
@@ -47,7 +47,7 @@ def _replace_fields(field_data)
4747
@save_needed = true
4848
rescue ArgumentError
4949
# :nocov: Will be thrown if an enum value isn't allowed for an enum. Currently not tested as enums are a rails 4.1 and higher feature
50-
raise Railsapi::Exceptions::InvalidFieldValue.new(attribute, value)
50+
raise RailsAPI::Exceptions::InvalidFieldValue.new(attribute, value)
5151
# :nocov:
5252
end
5353
end

‎lib/railsapi/resource_records.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Railsapi
1+
module RailsAPI
22
module ResourceRecords
33
def self.included(base)
44
base.class_eval do

‎lib/railsapi/resource_relationships.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'railsapi/relationship'
22

3-
module Railsapi
3+
module RailsAPI
44
module ResourceRelationships
55
def self.included(base)
66
base.class_eval do
@@ -83,8 +83,8 @@ def _add_relationship(klass, *attrs)
8383
@_relationships[relationship_name] = relationship = klass.new(relationship_name, options)
8484

8585
associated_records_method_name = case relationship
86-
when Railsapi::Relationship::ToOne then "record_for_#{relationship_name}"
87-
when Railsapi::Relationship::ToMany then "records_for_#{relationship_name}"
86+
when RailsAPI::Relationship::ToOne then "record_for_#{relationship_name}"
87+
when RailsAPI::Relationship::ToMany then "records_for_#{relationship_name}"
8888
end
8989

9090
foreign_key = relationship.foreign_key
@@ -99,7 +99,7 @@ def _add_relationship(klass, *attrs)
9999
records_for(relation_name)
100100
end unless method_defined?(associated_records_method_name)
101101

102-
if relationship.is_a?(Railsapi::Relationship::ToOne)
102+
if relationship.is_a?(RailsAPI::Relationship::ToOne)
103103
if relationship.belongs_to?
104104
define_method foreign_key do
105105
@model.method(foreign_key).call
@@ -139,7 +139,7 @@ def _add_relationship(klass, *attrs)
139139
end
140140
end unless method_defined?(relationship_name)
141141
end
142-
elsif relationship.is_a?(Railsapi::Relationship::ToMany)
142+
elsif relationship.is_a?(RailsAPI::Relationship::ToMany)
143143
define_method foreign_key do
144144
records = public_send(associated_records_method_name)
145145
return records.collect do |record|
@@ -232,7 +232,7 @@ def _create_to_many_links(relationship_type, relationship_key_values)
232232
if relation.nil?
233233
@model.public_send(relation_name) << related_resource._model
234234
else
235-
fail Railsapi::Exceptions::HasManyRelationExists.new(relationship_key_value)
235+
fail RailsAPI::Exceptions::HasManyRelationExists.new(relationship_key_value)
236236
end
237237
end
238238

‎lib/railsapi/resources/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Railsapi
1+
module RailsAPI
22
module Resources
33
VERSION = "0.1.0"
44
end

‎railsapi-resources.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'railsapi/resources/version'
55

66
Gem::Specification.new do |spec|
77
spec.name = 'railsapi-resources'
8-
spec.version = Railsapi::Resources::VERSION
8+
spec.version = RailsAPI::Resources::VERSION
99
spec.authors = ['Dan Gebhardt', 'Larry Gebhardt']
1010
spec.email = ['dan@cerebris.com', 'larry@cerebris.com']
1111

‎test/fixtures/resources.rb

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'railsapi-resources'
22

33
### RESOURCES
4-
class BaseResource < Railsapi::Resource
4+
class BaseResource < RailsAPI::Resource
55
abstract
66
end
77

@@ -27,28 +27,28 @@ class SpecialPersonResource < SpecialBaseResource
2727
model_name 'Person'
2828
end
2929

30-
class CommentResource < Railsapi::Resource
30+
class CommentResource < RailsAPI::Resource
3131
attributes :body
3232
has_one :post
3333
has_one :author, class_name: 'Person'
3434
has_many :tags
3535
end
3636

37-
class CompanyResource < Railsapi::Resource
37+
class CompanyResource < RailsAPI::Resource
3838
attributes :name, :address
3939
end
4040

4141
class FirmResource < CompanyResource
4242
end
4343

44-
class TagResource < Railsapi::Resource
44+
class TagResource < RailsAPI::Resource
4545
attributes :name
4646

4747
has_many :posts
4848
end
4949

5050

51-
class PostResource < Railsapi::Resource
51+
class PostResource < RailsAPI::Resource
5252
attribute :title
5353
attribute :body
5454
attribute :subject
@@ -119,12 +119,12 @@ def self.sortable_fields(context)
119119

120120
def self.verify_key(key, context = nil)
121121
super(key)
122-
raise Railsapi::Exceptions::RecordNotFound.new(key) unless find_by_key(key, context: context)
122+
raise RailsAPI::Exceptions::RecordNotFound.new(key) unless find_by_key(key, context: context)
123123
return key
124124
end
125125
end
126126

127-
class PreferencesResource < Railsapi::Resource
127+
class PreferencesResource < RailsAPI::Resource
128128
attribute :advanced_mode
129129

130130
has_one :author, :foreign_key_on => :related
@@ -134,16 +134,16 @@ def self.find_by_key(key, options = {})
134134
end
135135
end
136136

137-
class AuthorResource < Railsapi::Resource
137+
class AuthorResource < RailsAPI::Resource
138138
model_name 'Person'
139139
attributes :name
140140
end
141141

142-
class BookResource < Railsapi::Resource
142+
class BookResource < RailsAPI::Resource
143143
has_many :authors, class_name: 'Author'
144144
end
145145

146-
class AuthorDetailResource < Railsapi::Resource
146+
class AuthorDetailResource < RailsAPI::Resource
147147
attributes :author_stuff
148148
end
149149

@@ -153,7 +153,7 @@ module V2
153153
# class PersonResource < PersonResource; end
154154
# class PostResource < PostResource; end
155155

156-
class BookResource < Railsapi::Resource
156+
class BookResource < RailsAPI::Resource
157157
attribute :title
158158
attributes :isbn, :banned
159159

@@ -181,7 +181,7 @@ def not_banned_books
181181
end
182182
end
183183

184-
class BookCommentResource < Railsapi::Resource
184+
class BookCommentResource < RailsAPI::Resource
185185
attributes :body, :approved
186186

187187
has_one :book
@@ -200,7 +200,7 @@ def approved_comments(approved = true)
200200
end
201201
end
202202

203-
class ArticleResource < Railsapi::Resource
203+
class ArticleResource < RailsAPI::Resource
204204
model_name 'Post'
205205
end
206206

@@ -214,19 +214,19 @@ def do_some_after_save_stuff
214214
end
215215
end
216216

217-
class ArticleWithBadAfterSaveResource < Railsapi::Resource
217+
class ArticleWithBadAfterSaveResource < RailsAPI::Resource
218218
model_name 'PostWithBadAfterSave'
219219
attribute :title
220220
end
221221

222-
class NoMatchResource < Railsapi::Resource
222+
class NoMatchResource < RailsAPI::Resource
223223
end
224224

225-
class NoMatchAbstractResource < Railsapi::Resource
225+
class NoMatchAbstractResource < RailsAPI::Resource
226226
abstract
227227
end
228228

229-
class CatResource < Railsapi::Resource
229+
class CatResource < RailsAPI::Resource
230230
attribute :name
231231
attribute :breed
232232

@@ -235,12 +235,12 @@ class CatResource < Railsapi::Resource
235235
end
236236

237237
module MyModule
238-
class MyNamespacedResource < Railsapi::Resource
238+
class MyNamespacedResource < RailsAPI::Resource
239239
model_name "Person"
240240
has_many :related
241241
end
242242

243-
class RelatedResource < Railsapi::Resource
243+
class RelatedResource < RailsAPI::Resource
244244
model_name "Comment"
245245
end
246246
end
@@ -255,7 +255,7 @@ class RelatedResource < MyModule::RelatedResource
255255

256256
module Api
257257
module V8
258-
class NumeroTelefoneResource < Railsapi::Resource
258+
class NumeroTelefoneResource < RailsAPI::Resource
259259
attribute :numero_telefone
260260
end
261261
end

‎test/railsapi/resource_test.rb

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require File.expand_path('../../test_helper', __FILE__)
22

3-
class Railsapi::ResourceTest < Minitest::Test
3+
class RailsAPI::ResourceTest < Minitest::Test
44
def setup
55
@post = Post.first
66
end
@@ -23,12 +23,12 @@ def test_module_path
2323

2424
def test_resource_for_root_resource
2525
assert_raises NameError do
26-
Railsapi::Resource.resource_for('related')
26+
RailsAPI::Resource.resource_for('related')
2727
end
2828
end
2929

3030
def test_resource_for_with_namespaced_paths
31-
assert_equal(Railsapi::Resource.resource_for('my_module/related'), MyModule::RelatedResource)
31+
assert_equal(RailsAPI::Resource.resource_for('my_module/related'), MyModule::RelatedResource)
3232
assert_equal(PostResource.resource_for('my_module/related'), MyModule::RelatedResource)
3333
assert_equal(MyModule::MyNamespacedResource.resource_for('my_module/related'), MyModule::RelatedResource)
3434
end
@@ -38,7 +38,7 @@ def test_resource_for_resource_does_not_exist_at_root
3838
ArticleResource.resource_for('related')
3939
end
4040
assert_raises NameError do
41-
Railsapi::Resource.resource_for('related')
41+
RailsAPI::Resource.resource_for('related')
4242
end
4343
end
4444

@@ -116,7 +116,7 @@ def test_key_type_integer
116116
assert CatResource.verify_key('45')
117117
assert CatResource.verify_key(45)
118118

119-
assert_raises Railsapi::Exceptions::InvalidFieldValue do
119+
assert_raises RailsAPI::Exceptions::InvalidFieldValue do
120120
CatResource.verify_key('45,345')
121121
end
122122

@@ -134,7 +134,7 @@ def test_key_type_string
134134
assert CatResource.verify_key('45')
135135
assert CatResource.verify_key(45)
136136

137-
assert_raises Railsapi::Exceptions::InvalidFieldValue do
137+
assert_raises RailsAPI::Exceptions::InvalidFieldValue do
138138
CatResource.verify_key('45,345')
139139
end
140140

@@ -151,7 +151,7 @@ def test_key_type_uuid
151151

152152
assert CatResource.verify_key('f1a4d5f2-e77a-4d0a-acbb-ee0b98b3f6b5')
153153

154-
assert_raises Railsapi::Exceptions::InvalidFieldValue do
154+
assert_raises RailsAPI::Exceptions::InvalidFieldValue do
155155
CatResource.verify_key('f1a-e77a-4d0a-acbb-ee0b98b3f6b5')
156156
end
157157

@@ -168,14 +168,14 @@ def test_key_type_proc
168168
if key.to_s.match(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/)
169169
key
170170
else
171-
raise Railsapi::Exceptions::InvalidFieldValue.new(:id, key)
171+
raise RailsAPI::Exceptions::InvalidFieldValue.new(:id, key)
172172
end
173173
}
174174
end
175175

176176
assert CatResource.verify_key('f1a4d5f2-e77a-4d0a-acbb-ee0b98b3f6b5')
177177

178-
assert_raises Railsapi::Exceptions::InvalidFieldValue do
178+
assert_raises RailsAPI::Exceptions::InvalidFieldValue do
179179
CatResource.verify_key('f1a-e77a-4d0a-acbb-ee0b98b3f6b5')
180180
end
181181

@@ -187,15 +187,15 @@ def test_key_type_proc
187187

188188
def test_links_resource_warning
189189
_out, err = capture_io do
190-
eval "class LinksResource < Railsapi::Resource; end"
190+
eval "class LinksResource < RailsAPI::Resource; end"
191191
end
192192
assert_match /LinksResource` is a reserved resource name/, err
193193
end
194194

195195
def test_reserved_key_warnings
196196
_out, err = capture_io do
197197
eval <<-CODE
198-
class BadlyNamedAttributesResource < Railsapi::Resource
198+
class BadlyNamedAttributesResource < RailsAPI::Resource
199199
attributes :type
200200
end
201201
CODE
@@ -207,7 +207,7 @@ def test_reserved_relationship_warnings
207207
%w(id type).each do |key|
208208
_out, err = capture_io do
209209
eval <<-CODE
210-
class BadlyNamedAttributesResource < Railsapi::Resource
210+
class BadlyNamedAttributesResource < RailsAPI::Resource
211211
has_one :#{key}
212212
end
213213
CODE
@@ -217,7 +217,7 @@ class BadlyNamedAttributesResource < Railsapi::Resource
217217
%w(types ids).each do |key|
218218
_out, err = capture_io do
219219
eval <<-CODE
220-
class BadlyNamedAttributesResource < Railsapi::Resource
220+
class BadlyNamedAttributesResource < RailsAPI::Resource
221221
has_many :#{key}
222222
end
223223
CODE
@@ -229,18 +229,18 @@ class BadlyNamedAttributesResource < Railsapi::Resource
229229
def test_abstract_warning
230230
_out, err = capture_io do
231231
eval <<-CODE
232-
class NoModelResource < Railsapi::Resource
232+
class NoModelResource < RailsAPI::Resource
233233
end
234234
NoModelResource._model_class
235235
CODE
236236
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
238238
end
239239

240240
def test_no_warning_when_abstract
241241
_out, err = capture_io do
242242
eval <<-CODE
243-
class NoModelAbstractResource < Railsapi::Resource
243+
class NoModelAbstractResource < RailsAPI::Resource
244244
abstract
245245
end
246246
NoModelAbstractResource._model_class
@@ -252,7 +252,7 @@ class NoModelAbstractResource < Railsapi::Resource
252252
def test_correct_error_surfaced_if_validation_errors_in_after_save_callback
253253
post = PostWithBadAfterSave.find(1)
254254
post_resource = ArticleWithBadAfterSaveResource.new(post, nil)
255-
err = assert_raises Railsapi::Exceptions::ValidationErrors do
255+
err = assert_raises RailsAPI::Exceptions::ValidationErrors do
256256
post_resource.replace_fields({:attributes => {:title => 'Some title'}})
257257
end
258258
assert_equal(err.error_messages[:base], ['Boom! Error added in after_save callback.'])

‎test/railsapi/resources_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require File.expand_path('../../test_helper', __FILE__)
22

3-
class Railsapi::ResourcesTest < Minitest::Test
3+
class RailsAPI::ResourcesTest < Minitest::Test
44
def test_that_it_has_a_version_number
5-
refute_nil ::Railsapi::Resources::VERSION
5+
refute_nil ::RailsAPI::Resources::VERSION
66
end
77
end

0 commit comments

Comments
 (0)
Please sign in to comment.