Skip to content

Commit 856f139

Browse files
authored
Remove code specifically for pre 5.1 rails versions (#1353)
* Remove code specifically for pre 5.1 rails versions * Update rails version supported
1 parent e894827 commit 856f139

File tree

9 files changed

+85
-175
lines changed

9 files changed

+85
-175
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Like JSON:API itself, JR's design is focused on the resources served by an API. JR needs little more than a definition
99
of your resources, including their attributes and relationships, to make your server compliant with JSON API.
1010

11-
JR is designed to work with Rails 4.2+, and provides custom routes, controllers, and serializers. JR's resources may be
11+
JR is designed to work with Rails 5.1+, and provides custom routes, controllers, and serializers. JR's resources may be
1212
backed by ActiveRecord models or by custom objects.
1313

1414
## Documentation

jsonapi-resources.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
2727
spec.add_development_dependency 'pry'
2828
spec.add_development_dependency 'concurrent-ruby-ext'
2929
spec.add_development_dependency 'database_cleaner'
30-
spec.add_dependency 'activerecord', '>= 4.1'
31-
spec.add_dependency 'railties', '>= 4.1'
30+
spec.add_dependency 'activerecord', '>= 5.1'
31+
spec.add_dependency 'railties', '>= 5.1'
3232
spec.add_dependency 'concurrent-ruby'
3333
end

lib/jsonapi/active_relation/adapters/join_left_active_record_adapter.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module JSONAPI
22
module ActiveRelation
33
module Adapters
44
module JoinLeftActiveRecordAdapter
5-
65
# Extends left_joins functionality to rails 4, and uses the same logic for rails 5.0.x and 5.1.x
76
# The default left_joins logic of rails 5.2.x is used. This results in and extra join in some cases. For
87
# example Post.joins(:comments).joins_left(comments: :author) will join the comments table twice,

lib/jsonapi/basic_resource.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -575,20 +575,12 @@ def attribute_to_model_field(attribute)
575575
attr = @_attributes[attribute]
576576
attr && attr[:delegate] ? attr[:delegate].to_sym : attribute
577577
end
578-
if Rails::VERSION::MAJOR >= 5
579-
attribute_type = _model_class.attribute_types[field_name.to_s]
580-
else
581-
attribute_type = _model_class.column_types[field_name.to_s]
582-
end
583-
{ name: field_name, type: attribute_type}
578+
579+
{ name: field_name, type: _model_class.attribute_types[field_name.to_s]}
584580
end
585581

586582
def cast_to_attribute_type(value, type)
587-
if Rails::VERSION::MAJOR >= 5
588-
return type.cast(value)
589-
else
590-
return type.type_cast_from_database(value)
591-
end
583+
type.cast(value)
592584
end
593585

594586
def default_attribute_options

lib/jsonapi/mime_types.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ module MimeTypes
77
def self.install
88
Mime::Type.register JSONAPI::MEDIA_TYPE, :api_json
99

10-
# :nocov:
11-
if Rails::VERSION::MAJOR >= 5
12-
parsers = ActionDispatch::Request.parameter_parsers.merge(
13-
Mime::Type.lookup(JSONAPI::MEDIA_TYPE).symbol => parser
14-
)
15-
ActionDispatch::Request.parameter_parsers = parsers
16-
else
17-
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MEDIA_TYPE)] = parser
18-
end
19-
# :nocov:
10+
parsers = ActionDispatch::Request.parameter_parsers.merge(
11+
Mime::Type.lookup(JSONAPI::MEDIA_TYPE).symbol => parser
12+
)
13+
ActionDispatch::Request.parameter_parsers = parsers
2014
end
2115

2216
def self.parser

test/fixtures/active_record.rb

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -517,24 +517,10 @@ def destroy_callback
517517
case title
518518
when "can't destroy me", "can't destroy me either"
519519
errors.add(:base, "can't destroy me")
520-
521-
# :nocov:
522-
if Rails::VERSION::MAJOR >= 5
523-
throw(:abort)
524-
else
525-
return false
526-
end
527-
# :nocov:
520+
throw(:abort)
528521
when "locked title"
529522
errors.add(:title, "is locked")
530-
531-
# :nocov:
532-
if Rails::VERSION::MAJOR >= 5
533-
throw(:abort)
534-
else
535-
return false
536-
end
537-
# :nocov:
523+
throw(:abort)
538524
end
539525
end
540526
end
@@ -605,13 +591,7 @@ class Planet < ActiveRecord::Base
605591
def check_not_pluto
606592
# Pluto can't be a planet, so cancel the save
607593
if name.downcase == 'pluto'
608-
# :nocov:
609-
if Rails::VERSION::MAJOR >= 5
610-
throw(:abort)
611-
else
612-
return false
613-
end
614-
# :nocov:
594+
throw(:abort)
615595
end
616596
end
617597
end

test/test_helper.rb

Lines changed: 67 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
# COVERAGE=true bundle exec rake test
66

77
# To test on a specific rails version use this:
8-
# export RAILS_VERSION=4.2.6; bundle update rails; bundle exec rake test
9-
# export RAILS_VERSION=5.0.0; bundle update rails; bundle exec rake test
10-
# export RAILS_VERSION=5.1.0; bundle update rails; bundle exec rake test
11-
# export RAILS_VERSION=6.0.0.beta1; bundle update rails; bundle exec rake test
8+
# export RAILS_VERSION=5.2.4.4; bundle update; bundle exec rake test
9+
# export RAILS_VERSION=6.0.3.4; bundle update; bundle exec rake test
10+
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake test
1211

13-
# We are no longer having Travis test Rails 4.1.x., but you can try it with:
14-
# export RAILS_VERSION=4.1.0; bundle update rails; bundle exec rake test
12+
# We are no longer having Travis test Rails 4.2.11., but you can try it with:
13+
# export RAILS_VERSION=4.2.11; bundle update rails; bundle exec rake test
1514

1615
# To Switch rails versions and run a particular test order:
17-
# export RAILS_VERSION=4.2.6; bundle update rails; bundle exec rake TESTOPTS="--seed=39333" test
16+
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake TESTOPTS="--seed=39333" test
1817

1918
if ENV['COVERAGE']
2019
SimpleCov.start do
@@ -60,13 +59,11 @@ class TestApp < Rails::Application
6059
config.active_record.schema_format = :none
6160
config.active_support.test_order = :random
6261

63-
if Rails::VERSION::MAJOR >= 5
64-
config.active_support.halt_callback_chains_on_return_false = false
65-
config.active_record.time_zone_aware_types = [:time, :datetime]
66-
config.active_record.belongs_to_required_by_default = false
67-
if Rails::VERSION::MINOR >= 2
68-
config.active_record.sqlite3.represent_boolean_as_integer = true
69-
end
62+
config.active_support.halt_callback_chains_on_return_false = false
63+
config.active_record.time_zone_aware_types = [:time, :datetime]
64+
config.active_record.belongs_to_required_by_default = false
65+
unless Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR < 2
66+
config.active_record.sqlite3.represent_boolean_as_integer = true
7067
end
7168
end
7269

@@ -86,116 +83,80 @@ class Engine < ::Rails::Engine
8683
end
8784

8885
# Monkeypatch ActionController::TestCase to delete the RAW_POST_DATA on subsequent calls in the same test.
89-
if Rails::VERSION::MAJOR >= 5
90-
module ClearRawPostHeader
91-
def process(action, **args)
92-
@request.delete_header 'RAW_POST_DATA'
93-
super action, **args
94-
end
95-
end
96-
97-
class ActionController::TestCase
98-
prepend ClearRawPostHeader
86+
module ClearRawPostHeader
87+
def process(action, **args)
88+
@request.delete_header 'RAW_POST_DATA'
89+
super action, **args
9990
end
10091
end
10192

102-
# Tests are now using the rails 5 format for the http methods. So for rails 4 we will simply convert them back
103-
# in a standard way.
104-
if Rails::VERSION::MAJOR < 5
105-
module Rails4ActionControllerProcess
106-
def process(*args)
107-
if args[2] && args[2][:params]
108-
args[2] = args[2][:params]
109-
end
110-
super
111-
end
112-
end
113-
class ActionController::TestCase
114-
prepend Rails4ActionControllerProcess
115-
end
116-
117-
module ActionDispatch
118-
module Integration #:nodoc:
119-
module Rails4IntegrationProcess
120-
def process(method, path, parameters = nil, headers_or_env = nil)
121-
params = parameters.nil? ? nil : parameters[:params]
122-
headers = parameters.nil? ? nil : parameters[:headers]
123-
super method, path, params, headers
124-
end
125-
end
126-
127-
class Session
128-
prepend Rails4IntegrationProcess
129-
end
130-
end
131-
end
93+
class ActionController::TestCase
94+
prepend ClearRawPostHeader
13295
end
13396

13497
# Patch to allow :api_json mime type to be treated as JSON
13598
# Otherwise it is run through `to_query` and empty arrays are dropped.
136-
if Rails::VERSION::MAJOR >= 5
137-
module ActionController
138-
class TestRequest < ActionDispatch::TestRequest
139-
def assign_parameters(routes, controller_path, action, parameters, generated_path, query_string_keys)
140-
non_path_parameters = {}
141-
path_parameters = {}
142-
143-
parameters.each do |key, value|
144-
if query_string_keys.include?(key)
145-
non_path_parameters[key] = value
99+
module ActionController
100+
class TestRequest < ActionDispatch::TestRequest
101+
def assign_parameters(routes, controller_path, action, parameters, generated_path, query_string_keys)
102+
non_path_parameters = {}
103+
path_parameters = {}
104+
105+
parameters.each do |key, value|
106+
if query_string_keys.include?(key)
107+
non_path_parameters[key] = value
108+
else
109+
if value.is_a?(Array)
110+
value = value.map(&:to_param)
146111
else
147-
if value.is_a?(Array)
148-
value = value.map(&:to_param)
149-
else
150-
value = value.to_param
151-
end
152-
153-
path_parameters[key] = value
112+
value = value.to_param
154113
end
114+
115+
path_parameters[key] = value
155116
end
117+
end
156118

157-
if get?
158-
if self.query_string.blank?
159-
self.query_string = non_path_parameters.to_query
160-
end
119+
if get?
120+
if self.query_string.blank?
121+
self.query_string = non_path_parameters.to_query
122+
end
123+
else
124+
if ENCODER.should_multipart?(non_path_parameters)
125+
self.content_type = ENCODER.content_type
126+
data = ENCODER.build_multipart non_path_parameters
161127
else
162-
if ENCODER.should_multipart?(non_path_parameters)
163-
self.content_type = ENCODER.content_type
164-
data = ENCODER.build_multipart non_path_parameters
165-
else
166-
fetch_header('CONTENT_TYPE') do |k|
167-
set_header k, 'application/x-www-form-urlencoded'
168-
end
169-
170-
# parser = ActionDispatch::Http::Parameters::DEFAULT_PARSERS[Mime::Type.lookup(fetch_header('CONTENT_TYPE'))]
171-
172-
case content_mime_type.to_sym
173-
when nil
174-
raise "Unknown Content-Type: #{content_type}"
175-
when :json, :api_json
176-
data = ActiveSupport::JSON.encode(non_path_parameters)
177-
when :xml
178-
data = non_path_parameters.to_xml
179-
when :url_encoded_form
180-
data = non_path_parameters.to_query
181-
else
182-
@custom_param_parsers[content_mime_type] = ->(_) { non_path_parameters }
183-
data = non_path_parameters.to_query
184-
end
128+
fetch_header('CONTENT_TYPE') do |k|
129+
set_header k, 'application/x-www-form-urlencoded'
185130
end
186131

187-
set_header 'CONTENT_LENGTH', data.length.to_s
188-
set_header 'rack.input', StringIO.new(data)
132+
# parser = ActionDispatch::Http::Parameters::DEFAULT_PARSERS[Mime::Type.lookup(fetch_header('CONTENT_TYPE'))]
133+
134+
case content_mime_type.to_sym
135+
when nil
136+
raise "Unknown Content-Type: #{content_type}"
137+
when :json, :api_json
138+
data = ActiveSupport::JSON.encode(non_path_parameters)
139+
when :xml
140+
data = non_path_parameters.to_xml
141+
when :url_encoded_form
142+
data = non_path_parameters.to_query
143+
else
144+
@custom_param_parsers[content_mime_type] = ->(_) { non_path_parameters }
145+
data = non_path_parameters.to_query
146+
end
189147
end
190148

191-
fetch_header("PATH_INFO") do |k|
192-
set_header k, generated_path
193-
end
194-
path_parameters[:controller] = controller_path
195-
path_parameters[:action] = action
149+
set_header 'CONTENT_LENGTH', data.length.to_s
150+
set_header 'rack.input', StringIO.new(data)
151+
end
196152

197-
self.path_parameters = path_parameters
153+
fetch_header("PATH_INFO") do |k|
154+
set_header k, generated_path
198155
end
156+
path_parameters[:controller] = controller_path
157+
path_parameters[:action] = action
158+
159+
self.path_parameters = path_parameters
199160
end
200161
end
201162
end

test/unit/active_relation_resource_finder/active_record_adapter_test.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,8 @@ def test_joins_left
1111

1212
def test_joins_left_through_inner
1313
sql = Post.joins(:comments).joins_left(comments: :author).to_sql
14-
15-
# Note this joins_left reverts to left_joins on rails 5.2 and later
16-
# This behaves slightly differently in that the base join table is joined twice using left the second time (in this test).
17-
# This should produce the same result set, but will be slightly less efficient on the database
18-
if Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2
19-
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" '\
20-
'LEFT OUTER JOIN "comments" "comments_posts" ON "comments_posts"."post_id" = "posts"."id" '\
21-
'LEFT OUTER JOIN "people" ON "people"."id" = "comments_posts"."author_id"',
22-
sql
23-
else
24-
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" ' \
25-
'LEFT OUTER JOIN "people" ON "people"."id" = "comments"."author_id"',
26-
sql
27-
end
14+
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" ' \
15+
'LEFT OUTER JOIN "people" ON "people"."id" = "comments"."author_id"',
16+
sql
2817
end
2918
end

test/unit/resource/resource_test.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,9 @@ def test_inherited_calls_superclass
178178
end
179179

180180
def test_nil_model_class
181-
# ToDo:Figure out why this test does not work on Rails 4.0
182-
# :nocov:
183-
if (Rails::VERSION::MAJOR >= 4 && Rails::VERSION::MINOR >= 1) || (Rails::VERSION::MAJOR >= 5)
184-
assert_output nil, "[MODEL NOT FOUND] Model could not be found for NoMatchResource. If this is a base Resource declare it as abstract.\n" do
185-
assert_nil NoMatchResource._model_class
186-
end
181+
assert_output nil, "[MODEL NOT FOUND] Model could not be found for NoMatchResource. If this is a base Resource declare it as abstract.\n" do
182+
assert_nil NoMatchResource._model_class
187183
end
188-
# :nocov:
189184
end
190185

191186
def test_nil_abstract_model_class

0 commit comments

Comments
 (0)