Skip to content

Commit d298a40

Browse files
committed
Merge pull request #676 from cerebris/rails5
Rails5
2 parents 2d7e38b + b1a31f6 commit d298a40

14 files changed

Lines changed: 1414 additions & 1139 deletions

.travis.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ language: ruby
22
sudo: false
33
env:
44
- "RAILS_VERSION=4.1.0"
5-
- "RAILS_VERSION=4.2.0"
5+
- "RAILS_VERSION=4.2.6"
6+
- "RAILS_VERSION=5.0.0.beta3"
67
rvm:
78
- 2.0
89
- 2.1
9-
- 2.2
10+
- 2.2.4
11+
- 2.3.0
12+
matrix:
13+
exclude:
14+
- rvm: 2.0
15+
env: "RAILS_VERSION=5.0.0.beta3"
16+
- rvm: 2.1
17+
env: "RAILS_VERSION=5.0.0.beta3"

Rakefile

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
#!/usr/bin/env rake
22
require 'bundler/gem_tasks'
33
require 'rake/testtask'
4-
require './test/test_helper.rb'
54

6-
TestApp.load_tasks
5+
Rake::TestTask.new do |t|
6+
t.verbose = true
7+
t.warning = false
8+
t.test_files = FileList['test/**/*_test.rb']
9+
end
710

811
task default: :test
9-
10-
desc 'Run tests in isolated processes'
11-
namespace :test do
12-
task :isolated do
13-
Dir[test_task.pattern].each do |file|
14-
cmd = ['ruby']
15-
test_task.libs.each { |l| cmd << '-I' << l }
16-
cmd << file
17-
sh cmd.join(' ')
18-
end
19-
end
20-
end

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ def process_request
6666

6767
rescue => e
6868
handle_exceptions(e)
69-
ensure
70-
if response.body.size > 0
71-
response.headers['Content-Type'] = JSONAPI::MEDIA_TYPE
72-
end
7369
end
7470

7571
# set the operations processor in the configuration or override this to use another operations processor
@@ -155,7 +151,8 @@ def render_results(operation_results)
155151

156152
render_options = {
157153
status: response_doc.status,
158-
json: response_doc.contents
154+
json: response_doc.contents,
155+
content_type: JSONAPI::MEDIA_TYPE
159156
}
160157

161158
render_options[:location] = response_doc.contents[:data]["links"][:self] if (

lib/jsonapi/mime_types.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
module JSONAPI
22
MEDIA_TYPE = 'application/vnd.api+json'
3-
end
43

5-
Mime::Type.register JSONAPI::MEDIA_TYPE, :api_json
4+
module MimeTypes
5+
def self.install
6+
Mime::Type.register JSONAPI::MEDIA_TYPE, :api_json
7+
8+
if Rails::VERSION::MAJOR >= 5
9+
parsers = ActionDispatch::Request.parameter_parsers.merge(
10+
Mime::Type.lookup(JSONAPI::MEDIA_TYPE).symbol => parser
11+
)
12+
ActionDispatch::Request.parameter_parsers = parsers
13+
else
14+
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MEDIA_TYPE)] = parser
15+
end
16+
end
17+
18+
def self.parser
19+
lambda do |body|
20+
data = JSON.parse(body)
21+
data = {:_json => data} unless data.is_a?(Hash)
22+
data.with_indifferent_access
23+
end
24+
end
25+
end
626

7-
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MEDIA_TYPE)] = lambda do |body|
8-
data = JSON.parse(body)
9-
data = {:_json => data} unless data.is_a?(Hash)
10-
data.with_indifferent_access
27+
MimeTypes.install
1128
end

lib/jsonapi/request.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ def parse_to_one_links_object(raw)
382382
}
383383
end
384384

385-
if !raw.is_a?(Hash) || raw.length != 2 || !(raw.key?('type') && raw.key?('id'))
385+
if !(raw.is_a?(Hash) || raw.is_a?(ActionController::Parameters)) ||
386+
raw.keys.length != 2 || !(raw.key?('type') && raw.key?('id'))
386387
fail JSONAPI::Exceptions::InvalidLinksObject.new
387388
end
388389

@@ -476,7 +477,7 @@ def parse_to_one_relationship(link_value, relationship)
476477
def parse_to_many_relationship(link_value, relationship, &add_result)
477478
if link_value.is_a?(Array) && link_value.length == 0
478479
linkage = []
479-
elsif link_value.is_a?(Hash)
480+
elsif (link_value.is_a?(Hash) || link_value.is_a?(ActionController::Parameters))
480481
linkage = link_value[:data]
481482
else
482483
fail JSONAPI::Exceptions::InvalidLinksObject.new
@@ -608,7 +609,7 @@ def parse_update_relationship_operation(verified_params, relationship, parent_ke
608609
def parse_single_replace_operation(data, keys, id_key_presence_check_required: true)
609610
fail JSONAPI::Exceptions::MissingKey.new if data[:id].nil?
610611

611-
key = data[:id]
612+
key = data[:id].to_s
612613
if id_key_presence_check_required && !keys.include?(key)
613614
fail JSONAPI::Exceptions::KeyNotIncludedInURL.new(key)
614615
end

lib/jsonapi/response_document.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,17 @@ def query_params(params)
9090
query_params[:page] = params
9191

9292
request = @options[:request]
93-
query_params[:fields] = request.params[:fields] if request.params[:fields]
93+
if request.params[:fields]
94+
query_params[:fields] = request.params[:fields].respond_to?(:to_unsafe_hash) ? request.params[:fields].to_unsafe_hash : request.params[:fields]
95+
end
96+
9497
query_params[:include] = request.params[:include] if request.params[:include]
9598
query_params[:sort] = request.params[:sort] if request.params[:sort]
96-
query_params[:filter] = request.params[:filter] if request.params[:filter]
99+
100+
if request.params[:filter]
101+
query_params[:filter] = request.params[:filter].respond_to?(:to_unsafe_hash) ? request.params[:filter].to_unsafe_hash : request.params[:filter]
102+
end
103+
97104
query_params
98105
end
99106

lib/jsonapi/routing_ext.rb

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,24 @@ def jsonapi_resource(*resources, &_block)
3333
end
3434

3535
resource @resource_type, options do
36-
@scope[:jsonapi_resource] = @resource_type
37-
38-
if block_given?
39-
yield
36+
if @scope.respond_to? :[]=
37+
# Rails 4
38+
@scope[:jsonapi_resource] = @resource_type
39+
40+
if block_given?
41+
yield
42+
else
43+
jsonapi_relationships
44+
end
4045
else
41-
jsonapi_relationships
46+
# Rails 5
47+
jsonapi_resource_scope(SingletonResource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do
48+
if block_given?
49+
yield
50+
else
51+
jsonapi_relationships
52+
end
53+
end
4254
end
4355
end
4456
end
@@ -87,12 +99,23 @@ def jsonapi_resources(*resources, &_block)
8799
end
88100

89101
resources @resource_type, options do
90-
@scope[:jsonapi_resource] = @resource_type
91-
92-
if block_given?
93-
yield
102+
if @scope.respond_to? :[]=
103+
# Rails 4
104+
@scope[:jsonapi_resource] = @resource_type
105+
if block_given?
106+
yield
107+
else
108+
jsonapi_relationships
109+
end
94110
else
95-
jsonapi_relationships
111+
# Rails 5
112+
jsonapi_resource_scope(Resource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do
113+
if block_given?
114+
yield
115+
else
116+
jsonapi_relationships
117+
end
118+
end
96119
end
97120
end
98121
end
@@ -206,6 +229,16 @@ def jsonapi_related_resources(*relationship)
206229
action: 'get_related_resources', via: [:get]
207230
end
208231

232+
protected
233+
234+
def jsonapi_resource_scope(resource, resource_type) #:nodoc:
235+
@scope = @scope.new(scope_level_resource: resource, jsonapi_resource: resource_type)
236+
237+
controller(resource.resource_scope) { yield }
238+
ensure
239+
@scope = @scope.parent
240+
end
241+
209242
private
210243

211244
def resource_type_with_module_prefix(resource = nil)

0 commit comments

Comments
 (0)