Skip to content

Commit

Permalink
Add rails 5 support
Browse files Browse the repository at this point in the history
Copying changes over from this PR on the main repo for Rails 5 support: Sutto#143
  • Loading branch information
jordanmichaelrushing committed Apr 12, 2017
1 parent bddc27a commit f45765e
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
2 changes: 0 additions & 2 deletions lib/rocket_pants/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ class Base < ActionController::Metal
end

MODULES = [
ActionController::HideActions,
ActionController::UrlFor,
ActionController::Redirecting,
ActionController::ConditionalGet,
ActionController::RackDelegation,
record_identifier_klass,
ActionController::HttpAuthentication::Basic::ControllerMethods,
ActionController::HttpAuthentication::Digest::ControllerMethods,
Expand Down
4 changes: 2 additions & 2 deletions lib/rocket_pants/controller/format_verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module FormatVerification
extend ActiveSupport::Concern

included do
before_filter :ensure_has_valid_format
before_action :ensure_has_valid_format
end

private
Expand All @@ -13,4 +13,4 @@ def ensure_has_valid_format
end

end
end
end
4 changes: 2 additions & 2 deletions lib/rocket_pants/controller/jsonp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def wrap_response_in_jsonp
# Finally, set up the callback using the JSONP parameter.
response.content_type = 'application/javascript'
response.body = "#{jsonp_parameter}(#{response.body});"
headers['Content-Length'] = Rack::Utils.bytesize(response.body).to_s
headers['Content-Length'] = (response.body.bytesize).to_s
end

end
end
end
6 changes: 3 additions & 3 deletions lib/rocket_pants/controller/respondable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ def render_json(json, options = {})
json = encode_to_json(json) unless json.respond_to?(:to_str)
# Encode the object to json.
self.status ||= :ok
self.content_type ||= Mime::JSON
self.content_type ||= Mime[:json]
self.response_body = json
headers['Content-Length'] = Rack::Utils.bytesize(json).to_s
headers['Content-Length'] = (json.bytesize).to_s
end

# Renders a raw object, without any wrapping etc.
Expand Down Expand Up @@ -227,4 +227,4 @@ def metadata_for(object, options, type, singular)
end

end
end
end
2 changes: 1 addition & 1 deletion lib/rocket_pants/controller/versioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module ClassMethods
def version(version)
version = version..version if version.is_a?(Integer)
self._version_range = version
before_filter :verify_api_version
before_action :verify_api_version
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rocket_pants/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Railtie < Rails::Railtie
config.rocket_pants.pass_through_errors = nil
config.rocket_pants.pass_through_errors = nil

config.i18n.railties_load_path << File.expand_path('../locale/en.yml', __FILE__)
config.i18n.load_path << File.expand_path('../locale/en.yml', __FILE__)

initializer "rocket_pants.logger" do
ActiveSupport.on_load(:rocket_pants) { self.logger ||= Rails.logger }
Expand Down Expand Up @@ -54,4 +54,4 @@ class Railtie < Rails::Railtie
end

end
end
end
26 changes: 16 additions & 10 deletions lib/rocket_pants/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ module TestHelper

# Extend the response on first include.
class_attribute :_default_version
unless ActionController::TestResponse < ResponseHelper
ActionController::TestResponse.send :include, ResponseHelper
end

unless ActionDispatch::TestResponse < ResponseHelper
ActionDispatch::TestResponse.send :include, ResponseHelper
Expand Down Expand Up @@ -90,21 +87,30 @@ def insert_action_controller_testing_into_base

# Like process, but automatically adds the api version.
def process(action, *args)

insert_action_controller_testing_into_base

# Rails 4 changes the method signature. In rails 3, parameters is the first argument.
# In Rails 4, it's the second.
if args.first.is_a?(String)
parameters = (args[1] ||= {})
if Rails::VERSION::MAJOR <= 4
# Rails 4 changes the method signature. In rails 3, parameters is the first argument.
# In Rails 4, it's the second.
if args.first.is_a?(String)
parameters = (args[1] ||= {})
else
parameters = (args[0] ||= {})
end
else
parameters = (args[0] ||= {})
end

response.recycle_cached_body!

if _default_version.present? && parameters[:version].blank? && parameters['version'].blank?
parameters[:version] = _default_version
if Rails::VERSION::MAJOR <= 4
if _default_version.present? && parameters[:version].blank? && parameters['version'].blank?
parameters[:version] = _default_version
end
else
if _default_version.present? && parameters[:params][:version].blank?
parameters[:params][:version] = _default_version
end
end

super action, *args
Expand Down
8 changes: 4 additions & 4 deletions rocket_pants.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Gem::Specification.new do |s|
s.description = "Rocket Pants adds JSON API love to Rails and ActionController, making it simpler to build API-oriented controllers."
s.required_rubygems_version = ">= 1.3.6"

s.add_dependency 'actionpack', '>= 3.0', '< 5.0'
s.add_dependency 'railties', '>= 3.0', '< 5.0'
s.add_dependency 'actionpack', '>= 3.0', '< 6.0'
s.add_dependency 'railties', '>= 3.0', '< 6.0'
s.add_dependency 'will_paginate', '~> 3.0'
s.add_dependency 'hashie', '>= 1.0', '< 3'
s.add_dependency 'api_smith'
Expand All @@ -23,11 +23,11 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec-rails', '>= 2.4', '< 4.0'
s.add_development_dependency 'rr', '~> 1.0'
s.add_development_dependency 'webmock'
s.add_development_dependency 'activerecord', '>= 3.0', '< 5.0'
s.add_development_dependency 'activerecord', '>= 3.0', '< 6.0'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'reversible_data', '~> 1.0'
s.add_development_dependency 'kaminari'

s.files = Dir.glob("{lib}/**/*")
s.require_path = 'lib'
end
end
4 changes: 2 additions & 2 deletions spec/rocket_pants/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def object.serializable_hash(*); {:serialised => true}; end
get :echo, :echo => "Hello World", :callback => "test"
response.content_type.should include 'application/javascript'
response.body.should == %|test({"response":{"echo":"Hello World"}});|
response.headers['Content-Length'].to_i.should == Rack::Utils.bytesize(response.body)
response.headers['Content-Length'].to_i.should == response.body.bytesize
end

end
Expand All @@ -436,4 +436,4 @@ def object.serializable_hash(*); {:serialised => true}; end

end

end
end

0 comments on commit f45765e

Please sign in to comment.