From f45765e5d35ba82a1ae6976c62bc119932d3552a Mon Sep 17 00:00:00 2001 From: Jordan Rushing Date: Wed, 12 Apr 2017 16:34:05 -0700 Subject: [PATCH] Add rails 5 support Copying changes over from this PR on the main repo for Rails 5 support: https://github.com/Sutto/rocket_pants/pull/143 --- lib/rocket_pants/base.rb | 2 -- .../controller/format_verification.rb | 4 +-- lib/rocket_pants/controller/jsonp.rb | 4 +-- lib/rocket_pants/controller/respondable.rb | 6 ++--- lib/rocket_pants/controller/versioning.rb | 2 +- lib/rocket_pants/railtie.rb | 4 +-- lib/rocket_pants/test_helper.rb | 26 ++++++++++++------- rocket_pants.gemspec | 8 +++--- spec/rocket_pants/controller_spec.rb | 4 +-- 9 files changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/rocket_pants/base.rb b/lib/rocket_pants/base.rb index f64784d..f5becdc 100644 --- a/lib/rocket_pants/base.rb +++ b/lib/rocket_pants/base.rb @@ -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, diff --git a/lib/rocket_pants/controller/format_verification.rb b/lib/rocket_pants/controller/format_verification.rb index 84f50ea..c020d2c 100644 --- a/lib/rocket_pants/controller/format_verification.rb +++ b/lib/rocket_pants/controller/format_verification.rb @@ -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 @@ -13,4 +13,4 @@ def ensure_has_valid_format end end -end \ No newline at end of file +end diff --git a/lib/rocket_pants/controller/jsonp.rb b/lib/rocket_pants/controller/jsonp.rb index 0459ca8..fe74cdc 100644 --- a/lib/rocket_pants/controller/jsonp.rb +++ b/lib/rocket_pants/controller/jsonp.rb @@ -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 \ No newline at end of file +end diff --git a/lib/rocket_pants/controller/respondable.rb b/lib/rocket_pants/controller/respondable.rb index 6b6476e..c203a61 100644 --- a/lib/rocket_pants/controller/respondable.rb +++ b/lib/rocket_pants/controller/respondable.rb @@ -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. @@ -227,4 +227,4 @@ def metadata_for(object, options, type, singular) end end -end \ No newline at end of file +end diff --git a/lib/rocket_pants/controller/versioning.rb b/lib/rocket_pants/controller/versioning.rb index 24eeff6..fa5bfb6 100644 --- a/lib/rocket_pants/controller/versioning.rb +++ b/lib/rocket_pants/controller/versioning.rb @@ -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 diff --git a/lib/rocket_pants/railtie.rb b/lib/rocket_pants/railtie.rb index 9cb91fa..20ffdeb 100644 --- a/lib/rocket_pants/railtie.rb +++ b/lib/rocket_pants/railtie.rb @@ -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 } @@ -54,4 +54,4 @@ class Railtie < Rails::Railtie end end -end \ No newline at end of file +end diff --git a/lib/rocket_pants/test_helper.rb b/lib/rocket_pants/test_helper.rb index 1ecb6eb..06bf963 100644 --- a/lib/rocket_pants/test_helper.rb +++ b/lib/rocket_pants/test_helper.rb @@ -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 @@ -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 diff --git a/rocket_pants.gemspec b/rocket_pants.gemspec index 302529a..e11a9ac 100644 --- a/rocket_pants.gemspec +++ b/rocket_pants.gemspec @@ -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' @@ -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 \ No newline at end of file +end diff --git a/spec/rocket_pants/controller_spec.rb b/spec/rocket_pants/controller_spec.rb index b6cd677..a15a8f2 100644 --- a/spec/rocket_pants/controller_spec.rb +++ b/spec/rocket_pants/controller_spec.rb @@ -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 @@ -436,4 +436,4 @@ def object.serializable_hash(*); {:serialised => true}; end end -end \ No newline at end of file +end