Skip to content

Commit 862ca46

Browse files
committed
Merge pull request cerebris#54 from cerebris/ResourceNotFoundErrorMessage
Resource not found error message
2 parents b523f67 + d0df517 commit 862ca46

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: ruby
22
rvm:
3+
- 1.9.3
34
- 2.0.0
4-
- 2.1.1
5+
- 2.1

lib/jsonapi/request.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize(params = nil, options = {})
2020
end
2121

2222
def setup(params)
23-
@resource_klass ||= self.class.resource_for(params[:controller]) if params[:controller]
23+
@resource_klass ||= self.class.resource_for(params[:controller].split('/').last) if params[:controller]
2424

2525
unless params.nil?
2626
case params[:action]
@@ -76,10 +76,14 @@ def parse_fields(params)
7676
fields.each do |type, values|
7777
underscored_type = unformat_key(type)
7878
fields[type] = []
79-
type_resource = self.class.resource_for(underscored_type)
80-
if type_resource.nil? || !(@resource_klass._type == underscored_type ||
81-
@resource_klass._has_association?(underscored_type))
79+
begin
80+
type_resource = self.class.resource_for(underscored_type)
81+
rescue NameError
8282
@errors.concat(JSONAPI::Exceptions::InvalidResource.new(type).errors)
83+
end
84+
if type_resource.nil? || !(@resource_klass._type == underscored_type ||
85+
@resource_klass._has_association?(underscored_type))
86+
@errors.concat(JSONAPI::Exceptions::InvalidResource.new(type).errors)
8387
else
8488
unless values.nil?
8589
valid_fields = type_resource.fields.collect {|key| format_key(key)}

lib/jsonapi/resource_for.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ def resource_for(type)
1111
resource_name = JSONAPI::Resource._resource_name_from_type(type)
1212
Object.const_get resource_name if resource_name
1313
rescue NameError
14-
nil
14+
raise NameError, "JSONAPI: Could not find resource '#{type}'. (Class #{resource_name} not found)"
1515
end
1616
else
1717
def resource_for(type)
1818
resource_name = JSONAPI::Resource._resource_name_from_type(type)
19-
resource_name.safe_constantize if resource_name
19+
resource = resource_name.safe_constantize if resource_name
20+
if resource.nil?
21+
raise NameError, "JSONAPI: Could not find resource '#{type}'. (Class #{resource_name} not found)"
22+
end
23+
resource
2024
end
2125
end
2226
# :nocov:
2327
end
2428
end
25-
end
29+
end

test/fixtures/active_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ class PreferencesResource < JSONAPI::Resource
528528
has_one :author, foreign_key: :person_id
529529
has_many :friends
530530

531-
def self.find_by_key(key, context: nil)
531+
def self.find_by_key(key, options = {})
532532
new(Preferences.first)
533533
end
534534
end

0 commit comments

Comments
 (0)