Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
hacks that help getting a rails 2 app run like a rails 3 app
# test
if Rails::VERSION::MAJOR == 2
# make integration tests use rack response, so we can test our middlewares
# and not only the pure controller response
ActionController::Base.class_eval do
# this is usually done just-in-time by #process but we need to do it earlier
include ActionController::Integration::ControllerCapture
# then we hide last_instantiation from #process
def self.last_instantiation;end
end
ActionController::Integration::Session.class_eval do
def process_with_rackify(*args)
process_without_rackify(*args)
ensure
# needed e.g. inside of assert_redirect_to
capture = ActionController::Integration::ControllerCapture::ClassMethods
# not set by original #process
@response.redirected_to = @response.headers["Location"] if @response
if @controller = capture.send(:class_variable_get, :@@last_instantiation)
@request = @controller.request
@response.template = @controller.response.template if @response && @controller.response
@controller.send(:set_test_assigns)
end
end
alias_method_chain :process, :rackify
end
end
# test
if Rails::VERSION::MAJOR == 2
# set default accept to text/html as rails 3 does
def initialize_default_values_with_accept(*args)
@env["HTTP_ACCEPT"] ||= "text/html"
initialize_default_values_without_accept(*args)
end
alias_method_chain :initialize_default_values, :accept unless method_defined?(:initialize_default_values_without_accept)
# reset format before request as rails 3 does
def recycle_with_format_reset!
@format = nil
recycle_without_format_reset!
end
alias_method_chain :recycle!, :format_reset unless method_defined?(:recycle_without_format_reset!)
end
#test
if Rails::VERSION::MAJOR == 2
# make integration tests use rack response, so we can test our middlewares
# and not only the pure controller response
ActionController::Base.class_eval do
# this is usually done just-in-time by #process but we need to do it earlier
include ActionController::Integration::ControllerCapture
# then we hide last_instantiation from #process
def self.last_instantiation;end
end
ActionController::Integration::Session.class_eval do
def process_with_rackify(*args)
process_without_rackify(*args)
ensure
# needed e.g. inside of assert_redirect_to
capture = ActionController::Integration::ControllerCapture::ClassMethods
# not set by original #process
@response.redirected_to = @response.headers["Location"] if @response
if @controller = capture.send(:class_variable_get, :@@last_instantiation)
@request = @controller.request
@response.template = @controller.response.template if @response && @controller.response
@controller.send(:set_test_assigns)
end
end
alias_method_chain :process, :rackify
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment