Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge commit 'rails/master'

  • Loading branch information...
commit 0048897a417774f7e5a0c8c9e82fc8684f94ebc1 2 parents 6c7d8cb + 22c5667
@miloops miloops authored
Showing with 1,819 additions and 654 deletions.
  1. +11 −3 actionpack/Rakefile
  2. +1 −1  actionpack/lib/action_controller/abstract.rb
  3. +60 −13 actionpack/lib/action_controller/abstract/base.rb
  4. +5 −5 actionpack/lib/action_controller/abstract/callbacks.rb
  5. +1 −1  actionpack/lib/action_controller/abstract/exceptions.rb
  6. +7 −13 actionpack/lib/action_controller/abstract/helpers.rb
  7. +16 −12 actionpack/lib/action_controller/abstract/layouts.rb
  8. +1 −1  actionpack/lib/action_controller/abstract/logger.rb
  9. +26 −11 actionpack/lib/action_controller/abstract/renderer.rb
  10. +15 −14 actionpack/lib/action_controller/base/base.rb
  11. +1 −1  actionpack/lib/action_controller/dispatch/dispatcher.rb
  12. +1 −1  actionpack/lib/action_controller/dispatch/middlewares.rb
  13. +25 −5 actionpack/lib/action_controller/new_base.rb
  14. +53 −50 actionpack/lib/action_controller/new_base/base.rb
  15. +58 −0 actionpack/lib/action_controller/new_base/compatibility.rb
  16. +131 −0 actionpack/lib/action_controller/new_base/conditional_get.rb
  17. +8 −5 actionpack/lib/action_controller/new_base/hide_actions.rb
  18. +64 −0 actionpack/lib/action_controller/new_base/http.rb
  19. +9 −7 actionpack/lib/action_controller/new_base/layouts.rb
  20. +24 −28 actionpack/lib/action_controller/new_base/renderer.rb
  21. +25 −0 actionpack/lib/action_controller/new_base/testing.rb
  22. +3 −3 actionpack/lib/action_controller/new_base/url_for.rb
  23. +1 −1  actionpack/lib/action_controller/routing/route_set.rb
  24. +6 −9 actionpack/lib/action_controller/testing/integration.rb
  25. +4 −1 actionpack/lib/action_controller/testing/process.rb
  26. +69 −0 actionpack/lib/action_controller/testing/process2.rb
  27. +2 −0  actionpack/lib/action_dispatch/middleware/show_exceptions.rb
  28. +0 −2  actionpack/lib/action_dispatch/middleware/stack.rb
  29. +1 −1  actionpack/lib/action_view/paths.rb
  30. +5 −2 actionpack/lib/action_view/template/text.rb
  31. +22 −26 actionpack/test/abstract_controller/abstract_controller_test.rb
  32. +9 −9 actionpack/test/abstract_controller/callbacks_test.rb
  33. +3 −3 actionpack/test/abstract_controller/helper_test.rb
  34. +12 −12 actionpack/test/abstract_controller/layouts_test.rb
  35. +6 −10 actionpack/test/abstract_controller/test_helper.rb
  36. +133 −0 actionpack/test/abstract_unit2.rb
  37. +10 −0 actionpack/test/controller/content_type_test.rb
  38. +2 −1  actionpack/test/controller/filters_test.rb
  39. +29 −15 actionpack/test/controller/helper_test.rb
  40. +66 −8 actionpack/test/controller/render_test.rb
  41. +92 −23 actionpack/test/lib/fixture_template.rb
  42. +25 −26 actionpack/test/new_base/base_test.rb
  43. +111 −0 actionpack/test/new_base/content_type_test.rb
  44. +47 −0 actionpack/test/new_base/etag_test.rb
  45. +7 −7 actionpack/test/new_base/render_action_test.rb
  46. +21 −9 actionpack/test/new_base/render_implicit_action_test.rb
  47. +18 −4 actionpack/test/new_base/render_layout_test.rb
  48. +55 −37 actionpack/test/new_base/render_template_test.rb
  49. +88 −0 actionpack/test/new_base/render_test.rb
  50. +42 −37 actionpack/test/new_base/render_text_test.rb
  51. +9 −34 actionpack/test/new_base/test_helper.rb
  52. +22 −22 actionpack/test/template/output_buffer_test.rb
  53. +1 −3 activerecord/lib/active_record/aggregations.rb
  54. +4 −4 activerecord/lib/active_record/association_preload.rb
  55. +26 −10 activerecord/lib/active_record/associations.rb
  56. +1 −1  activerecord/lib/active_record/associations/has_one_association.rb
  57. +13 −9 activerecord/lib/active_record/attribute_methods.rb
  58. +6 −7 activerecord/lib/active_record/autosave_association.rb
  59. +1 −3 activerecord/lib/active_record/batches.rb
  60. +2 −3 activerecord/lib/active_record/calculations.rb
  61. +7 −5 activerecord/lib/active_record/callbacks.rb
  62. +11 −10 activerecord/lib/active_record/dirty.rb
  63. +18 −20 activerecord/lib/active_record/fixtures.rb
  64. +8 −8 activerecord/lib/active_record/locking/optimistic.rb
  65. +4 −5 activerecord/lib/active_record/named_scope.rb
  66. +15 −5 activerecord/lib/active_record/nested_attributes.rb
  67. +1 −3 activerecord/lib/active_record/observer.rb
  68. +1 −3 activerecord/lib/active_record/reflection.rb
  69. +4 −3 activerecord/lib/active_record/serializers/json_serializer.rb
  70. +7 −5 activerecord/lib/active_record/timestamp.rb
  71. +5 −7 activerecord/lib/active_record/transactions.rb
  72. +7 −8 activerecord/lib/active_record/validations.rb
  73. +6 −7 activerecord/test/cases/associations/eager_load_nested_include_test.rb
  74. +12 −0 activerecord/test/cases/associations/eager_test.rb
  75. +1 −8 activerecord/test/cases/associations/has_one_associations_test.rb
  76. +61 −0 activerecord/test/cases/associations/inverse_associations_test.rb
  77. +17 −1 activerecord/test/cases/nested_attributes_test.rb
  78. +1 −5 activerecord/test/cases/repair_helper.rb
  79. +1 −3 activerecord/test/fixtures/organizations.yml
  80. +1 −3 activerecord/test/fixtures/sponsors.yml
  81. +0 −4 activerecord/test/models/organization.rb
  82. +2 −0  activerecord/test/models/pirate.rb
  83. +2 −2 activerecord/test/schema/schema.rb
  84. +2 −1  activeresource/Rakefile
  85. +2 −0  activesupport/CHANGELOG
  86. +2 −2 activesupport/lib/active_support/core_ext/date/calculations.rb
  87. +3 −3 activesupport/lib/active_support/core_ext/date_time/conversions.rb
  88. +1 −1  activesupport/lib/active_support/core_ext/time/conversions.rb
  89. +4 −3 activesupport/lib/active_support/dependency_module.rb
  90. +2 −10 activesupport/lib/active_support/new_callbacks.rb
  91. +14 −0 activesupport/lib/active_support/ordered_hash.rb
  92. +2 −2 activesupport/lib/active_support/values/time_zone.rb
  93. +38 −2 activesupport/test/new_callbacks_test.rb
  94. +10 −0 activesupport/test/ordered_hash_test.rb
  95. +5 −0 railties/CHANGELOG
  96. +3 −0  railties/Rakefile
  97. +7 −0 railties/configs/seeds.rb
  98. +5 −0 railties/lib/rails_generator/generators/applications/app/app_generator.rb
  99. +11 −2 railties/lib/tasks/databases.rake
View
14 actionpack/Rakefile
@@ -4,7 +4,6 @@ require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/packagetask'
require 'rake/gempackagetask'
-require 'rake/contrib/sshpublisher'
require File.join(File.dirname(__FILE__), 'lib', 'action_pack', 'version')
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
@@ -23,14 +22,14 @@ task :default => [ :test ]
# Run the unit tests
desc "Run all unit tests"
-task :test => [:test_action_pack, :test_active_record_integration]
+task :test => [:test_action_pack, :test_active_record_integration, :test_new_base]
Rake::TestTask.new(:test_action_pack) do |t|
t.libs << "test"
# make sure we include the tests in alphabetical order as on some systems
# this will not happen automatically and the tests (as a whole) will error
- t.test_files = Dir.glob( "test/[cdft]*/**/*_test.rb" ).sort
+ t.test_files = Dir.glob( "test/{controller,dispatch,template}/**/*_test.rb" ).sort
t.verbose = true
#t.warning = true
@@ -43,6 +42,13 @@ Rake::TestTask.new(:test_active_record_integration) do |t|
t.verbose = true
end
+desc 'New Controller Tests'
+Rake::TestTask.new(:test_new_base) do |t|
+ t.libs << "test"
+ t.test_files = Dir.glob("test/{abstract_controller,new_base}/*_test.rb")
+ t.verbose = true
+end
+
# Genereate the RDoc documentation
@@ -136,12 +142,14 @@ task :update_js => [ :update_scriptaculous ]
desc "Publish the API documentation"
task :pgem => [:package] do
+ require 'rake/contrib/sshpublisher'
Rake::SshFilePublisher.new("gems.rubyonrails.org", "/u/sites/gems/gems", "pkg", "#{PKG_FILE_NAME}.gem").upload
`ssh gems.rubyonrails.org '/u/sites/gems/gemupdate.sh'`
end
desc "Publish the API documentation"
task :pdoc => [:rdoc] do
+ require 'rake/contrib/sshpublisher'
Rake::SshDirPublisher.new("wrath.rubyonrails.org", "public_html/ap", "doc").upload
end
View
2  actionpack/lib/action_controller/abstract.rb
@@ -7,4 +7,4 @@ module AbstractController
autoload :Renderer, "action_controller/abstract/renderer"
# === Exceptions
autoload :ActionNotFound, "action_controller/abstract/exceptions"
-end
+end
View
73 actionpack/lib/action_controller/abstract/base.rb
@@ -1,38 +1,85 @@
module AbstractController
class Base
+
attr_internal :response_body
attr_internal :response_obj
attr_internal :action_name
- def self.process(action)
- new.process(action)
- end
+ class << self
+ attr_reader :abstract
+
+ def abstract!
+ @abstract = true
+ end
+
+ alias_method :abstract?, :abstract
+
+ def internal_methods
+ controller = self
+ controller = controller.superclass until controller.abstract?
+ controller.public_instance_methods(true)
+ end
+
+ def process(action)
+ new.process(action.to_s)
+ end
- def self.inherited(klass)
+ def hidden_actions
+ []
+ end
+
+ def action_methods
+ @action_methods ||=
+ # All public instance methods of this class, including ancestors
+ public_instance_methods(true).map { |m| m.to_s }.to_set -
+ # Except for public instance methods of Base and its ancestors
+ internal_methods.map { |m| m.to_s } +
+ # Be sure to include shadowed public instance methods of this class
+ public_instance_methods(false).map { |m| m.to_s } -
+ # And always exclude explicitly hidden actions
+ hidden_actions
+ end
end
-
+
+ abstract!
+
def initialize
self.response_obj = {}
end
-
+
def process(action_name)
unless respond_to_action?(action_name)
raise ActionNotFound, "The action '#{action_name}' could not be found"
end
-
+
@_action_name = action_name
process_action
- self.response_obj[:body] = self.response_body
self
end
-
+
private
+
+ def action_methods
+ self.class.action_methods
+ end
+
+ # It is possible for respond_to?(action_name) to be false and
+ # respond_to?(:action_missing) to be false if respond_to_action?
+ # is overridden in a subclass. For instance, ActionController::Base
+ # overrides it to include the case where a template matching the
+ # action_name is found.
def process_action
- respond_to?(action_name) ? send(action_name) : send(:action_missing, action_name)
+ if respond_to?(action_name) then send(action_name)
+ elsif respond_to?(:action_missing, true) then action_missing(action_name)
+ end
end
-
+
+ # Override this to change the conditions that will raise an
+ # ActionNotFound error. If you accept a difference case,
+ # you must handle it by also overriding process_action and
+ # handling the case.
def respond_to_action?(action_name)
- respond_to?(action_name) || respond_to?(:action_missing, true)
+ action_methods.include?(action_name) || respond_to?(:action_missing, true)
end
end
-end
+end
View
10 actionpack/lib/action_controller/abstract/callbacks.rb
@@ -13,19 +13,19 @@ def process_action
super
end
end
-
+
module ClassMethods
def _normalize_callback_options(options)
if only = options[:only]
- only = Array(only).map {|o| "action_name == :#{o}"}.join(" || ")
+ only = Array(only).map {|o| "action_name == '#{o}'"}.join(" || ")
options[:per_key] = {:if => only}
end
if except = options[:except]
- except = Array(except).map {|e| "action_name == :#{e}"}.join(" || ")
+ except = Array(except).map {|e| "action_name == '#{e}'"}.join(" || ")
options[:per_key] = {:unless => except}
end
end
-
+
[:before, :after, :around].each do |filter|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{filter}_filter(*names, &blk)
@@ -40,4 +40,4 @@ def #{filter}_filter(*names, &blk)
end
end
end
-end
+end
View
2  actionpack/lib/action_controller/abstract/exceptions.rb
@@ -1,3 +1,3 @@
module AbstractController
class ActionNotFound < StandardError ; end
-end
+end
View
20 actionpack/lib/action_controller/abstract/helpers.rb
@@ -9,13 +9,6 @@ module Helpers
self.master_helper_module = Module.new
end
- # def self.included(klass)
- # klass.class_eval do
- # extlib_inheritable_accessor :master_helper_module
- # self.master_helper_module = Module.new
- # end
- # end
-
def _action_view
@_action_view ||= begin
av = super
@@ -23,19 +16,19 @@ def _action_view
av
end
end
-
+
module ClassMethods
def inherited(klass)
klass.master_helper_module = Module.new
klass.master_helper_module.__send__ :include, master_helper_module
-
+
super
end
-
+
def add_template_helper(mod)
master_helper_module.module_eval { include mod }
end
-
+
def helper_method(*meths)
meths.flatten.each do |meth|
master_helper_module.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
@@ -45,7 +38,7 @@ def #{meth}(*args, &blk)
ruby_eval
end
end
-
+
def helper(*args, &blk)
args.flatten.each do |arg|
case arg
@@ -56,5 +49,6 @@ def helper(*args, &blk)
master_helper_module.module_eval(&blk) if block_given?
end
end
+
end
-end
+end
View
28 actionpack/lib/action_controller/abstract/layouts.rb
@@ -9,18 +9,18 @@ def layout(layout)
unless [String, Symbol, FalseClass, NilClass].include?(layout.class)
raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
end
-
+
@_layout = layout || false # Converts nil to false
_write_layout_method
end
-
+
def _implied_layout_name
name.underscore
end
-
+
# Takes the specified layout and creates a _layout method to be called
# by _default_layout
- #
+ #
# If the specified layout is a:
# String:: return the string
# Symbol:: call the method specified by the symbol
@@ -49,22 +49,26 @@ def _layout
end
end
end
-
+
def _render_template(template, options)
- _action_view._render_template_with_layout(template, options[:_layout])
+ _action_view._render_template_with_layout(template, options[:_layout], options)
end
-
+
private
+
def _layout() end # This will be overwritten
-
+
+ # :api: plugin
+ # ====
+ # Override this to mutate the inbound layout name
def _layout_for_name(name)
unless [String, FalseClass, NilClass].include?(name.class)
raise ArgumentError, "String, false, or nil expected; you passed #{name.inspect}"
end
-
+
name && view_paths.find_by_parts(name, {:formats => formats}, "layouts")
end
-
+
def _default_layout(require_layout = false)
if require_layout && !_layout
raise ArgumentError,
@@ -74,9 +78,9 @@ def _default_layout(require_layout = false)
begin
layout = _layout_for_name(_layout)
rescue NameError => e
- raise NoMethodError,
+ raise NoMethodError,
"You specified #{@_layout.inspect} as the layout, but no such method was found"
end
end
end
-end
+end
View
2  actionpack/lib/action_controller/abstract/logger.rb
@@ -6,4 +6,4 @@ module Logger
cattr_accessor :logger
end
end
-end
+end
View
37 actionpack/lib/action_controller/abstract/renderer.rb
@@ -1,6 +1,15 @@
require "action_controller/abstract/logger"
module AbstractController
+ class AbstractControllerError < StandardError; end
+ class DoubleRenderError < AbstractControllerError
+ DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\"."
+
+ def initialize(message = nil)
+ super(message || DEFAULT_MESSAGE)
+ end
+ end
+
module Renderer
extend ActiveSupport::DependencyModule
@@ -15,31 +24,36 @@ module Renderer
end
def _action_view
- @_action_view ||= ActionView::Base.new(self.class.view_paths, {}, self)
+ @_action_view ||= ActionView::Base.new(self.class.view_paths, {}, self)
end
-
+
def render(options = {})
+ if response_body
+ raise AbstractController::DoubleRenderError, "OMG"
+ end
+
self.response_body = render_to_body(options)
end
-
+
# Raw rendering of a template to a Rack-compatible body.
# ====
# @option _prefix<String> The template's path prefix
# @option _layout<String> The relative path to the layout template to use
- #
+ #
# :api: plugin
def render_to_body(options = {})
name = options[:_template_name] || action_name
-
- template = options[:_template] || view_paths.find_by_parts(name.to_s, {:formats => formats}, options[:_prefix])
- _render_template(template, options)
+
+ options[:_template] ||= view_paths.find_by_parts(name.to_s, {:formats => formats}, options[:_prefix])
+
+ _render_template(options[:_template], options)
end
# Raw rendering of a template to a string.
# ====
# @option _prefix<String> The template's path prefix
# @option _layout<String> The relative path to the layout template to use
- #
+ #
# :api: plugin
def render_to_string(options = {})
AbstractController::Renderer.body_to_s(render_to_body(options))
@@ -48,7 +62,7 @@ def render_to_string(options = {})
def _render_template(template, options)
_action_view._render_template_with_layout(template)
end
-
+
def view_paths() _view_paths end
# Return a string representation of a Rack-compatible response body.
@@ -64,14 +78,15 @@ def self.body_to_s(body)
end
module ClassMethods
+
def append_view_path(path)
self.view_paths << path
end
-
+
def view_paths
self._view_paths
end
-
+
def view_paths=(paths)
self._view_paths = paths.is_a?(ActionView::PathSet) ?
paths : ActionView::Base.process_view_paths(paths)
View
29 actionpack/lib/action_controller/base/base.rb
@@ -365,17 +365,23 @@ def session
attr_reader :template
- class << self
- def call(env)
- new.call(env)
- end
+ def action(name, env)
+ # HACK: For global rescue to have access to the original request and response
+ request = env["action_controller.rescue.request"] ||= ActionDispatch::Request.new(env)
+ response = env["action_controller.rescue.response"] ||= ActionDispatch::Response.new
+ self.action_name = name && name.to_s
+ process(request, response).to_a
+ end
- # Factory for the standard create, process loop where the controller is discarded after processing.
- def process(request, response) #:nodoc:
- ActiveSupport::Deprecation.warn("Controller.process has been deprecated. Use Controller.call instead", caller)
- new.process(request, response)
- end
+ class << self
+ def action(name = nil)
+ @actions ||= {}
+ @actions[name] ||= proc do |env|
+ new.action(name, env)
+ end
+ end
+
# Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
def controller_class_name
@controller_class_name ||= name.demodulize
@@ -518,7 +524,6 @@ def process(request, response, method = :perform_action, *arguments) #:nodoc:
assign_shortcuts(request, response)
initialize_template_class(response)
initialize_current_url
- assign_names
log_processing
send(method, *arguments)
@@ -882,10 +887,6 @@ def performed?
@performed_render || @performed_redirect
end
- def assign_names
- @action_name = (params['action'] || 'index')
- end
-
def reset_variables_added_to_assigns
@template.instance_variable_set("@assigns_added", nil)
end
View
2  actionpack/lib/action_controller/dispatch/dispatcher.rb
@@ -48,7 +48,7 @@ def run_prepare_callbacks
cattr_accessor :middleware
self.middleware = ActionDispatch::MiddlewareStack.new do |middleware|
middlewares = File.join(File.dirname(__FILE__), "middlewares.rb")
- middleware.instance_eval(File.read(middlewares))
+ middleware.instance_eval(File.read(middlewares), middlewares, 1)
end
include ActiveSupport::Callbacks
View
2  actionpack/lib/action_controller/dispatch/middlewares.rb
@@ -14,4 +14,4 @@
use "ActionDispatch::ParamsParser"
use "Rack::MethodOverride"
-use "Rack::Head"
+use "Rack::Head"
View
30 actionpack/lib/action_controller/new_base.rb
@@ -1,7 +1,27 @@
module ActionController
- autoload :AbstractBase, "action_controller/new_base/base"
- autoload :HideActions, "action_controller/new_base/hide_actions"
- autoload :Layouts, "action_controller/new_base/layouts"
- autoload :Renderer, "action_controller/new_base/renderer"
- autoload :UrlFor, "action_controller/new_base/url_for"
+ autoload :Base, "action_controller/new_base/base"
+ autoload :ConditionalGet, "action_controller/new_base/conditional_get"
+ autoload :HideActions, "action_controller/new_base/hide_actions"
+ autoload :Http, "action_controller/new_base/http"
+ autoload :Layouts, "action_controller/new_base/layouts"
+ autoload :Rails2Compatibility, "action_controller/new_base/compatibility"
+ autoload :Renderer, "action_controller/new_base/renderer"
+ autoload :Testing, "action_controller/new_base/testing"
+ autoload :UrlFor, "action_controller/new_base/url_for"
+
+ # Ported modules
+ # require 'action_controller/routing'
+ autoload :Dispatcher, 'action_controller/dispatch/dispatcher'
+ autoload :PolymorphicRoutes, 'action_controller/routing/generation/polymorphic_routes'
+ autoload :RecordIdentifier, 'action_controller/record_identifier'
+ autoload :Resources, 'action_controller/routing/resources'
+ autoload :SessionManagement, 'action_controller/base/session_management'
+ autoload :TestCase, 'action_controller/testing/test_case'
+ autoload :UrlRewriter, 'action_controller/routing/generation/url_rewriter'
+ autoload :UrlWriter, 'action_controller/routing/generation/url_rewriter'
+
+ require 'action_controller/routing'
end
+
+require 'action_dispatch'
+require 'action_view'
View
103 actionpack/lib/action_controller/new_base/base.rb
@@ -1,59 +1,62 @@
module ActionController
- class AbstractBase < AbstractController::Base
- # :api: public
- attr_internal :request, :response, :params
-
- # :api: public
- def self.controller_name
- @controller_name ||= controller_path.split("/").last
+ class Base < Http
+ abstract!
+
+ include AbstractController::Callbacks
+ include AbstractController::Helpers
+ include AbstractController::Logger
+
+ include ActionController::HideActions
+ include ActionController::UrlFor
+ include ActionController::Renderer
+ include ActionController::Layouts
+ include ActionController::ConditionalGet
+
+ # Legacy modules
+ include SessionManagement
+ include ActionDispatch::StatusCodes
+
+ # Rails 2.x compatibility
+ include ActionController::Rails2Compatibility
+
+ def self.inherited(klass)
+ ::ActionController::Base.subclasses << klass.to_s
+ super
end
-
- # :api: public
- def controller_name() self.class.controller_name end
-
- # :api: public
- def self.controller_path
- @controller_path ||= self.name.sub(/Controller$/, '').underscore
+
+ def self.subclasses
+ @subclasses ||= []
end
-
- # :api: public
- def controller_path() self.class.controller_path end
-
- # :api: private
- def self.action_methods
- @action_names ||= Set.new(self.public_instance_methods - self::CORE_METHODS)
+
+ def self.app_loaded!
+ @subclasses.each do |subclass|
+ subclass.constantize._write_layout_method
+ end
end
-
- # :api: private
- def self.action_names() action_methods end
-
- # :api: private
- def action_methods() self.class.action_names end
-
- # :api: private
- def action_names() action_methods end
-
- # :api: plugin
- def self.call(env)
- controller = new
- controller.call(env).to_rack
+
+ def render(action = action_name, options = {})
+ if action.is_a?(Hash)
+ options, action = action, nil
+ else
+ options.merge! :action => action
+ end
+
+ super(options)
end
-
- # :api: plugin
- def response_body=(body)
- @_response.body = body
+
+ def render_to_body(options = {})
+ options = {:template => options} if options.is_a?(String)
+ super
end
-
- # :api: private
- def call(env)
- @_request = ActionDispatch::Request.new(env)
- @_response = ActionDispatch::Response.new
- process(@_request.parameters[:action])
+
+ def process_action
+ ret = super
+ render if response_body.nil?
+ ret
end
-
- # :api: private
- def to_rack
- response.to_a
+
+ def respond_to_action?(action_name)
+ super || view_paths.find_by_parts?(action_name.to_s, {:formats => formats, :locales => [I18n.locale]}, controller_path)
end
end
-end
+end
View
58 actionpack/lib/action_controller/new_base/compatibility.rb
@@ -0,0 +1,58 @@
+module ActionController
+ module Rails2Compatibility
+ extend ActiveSupport::DependencyModule
+
+ # Temporary hax
+ included do
+ ::ActionController::UnknownAction = ::AbstractController::ActionNotFound
+ ::ActionController::DoubleRenderError = ::AbstractController::DoubleRenderError
+
+ cattr_accessor :session_options
+ self.send(:class_variable_set, "@@session_options", {})
+
+ cattr_accessor :allow_concurrency
+ self.send(:class_variable_set, "@@allow_concurrency", false)
+
+ cattr_accessor :param_parsers
+ self.send(:class_variable_set, "@@param_parsers", { Mime::MULTIPART_FORM => :multipart_form,
+ Mime::URL_ENCODED_FORM => :url_encoded_form,
+ Mime::XML => :xml_simple,
+ Mime::JSON => :json })
+
+ cattr_accessor :relative_url_root
+ self.send(:class_variable_set, "@@relative_url_root", ENV['RAILS_RELATIVE_URL_ROOT'])
+
+ cattr_accessor :default_charset
+ self.send(:class_variable_set, "@@default_charset", "utf-8")
+
+ cattr_reader :protected_instance_variables
+ self.send(:class_variable_set, "@@protected_instance_variables", %w(@assigns @performed_redirect @performed_render @variables_added @request_origin @url @parent_controller
+ @action_name @before_filter_chain_aborted @action_cache_path @_headers @_params
+ @_flash @_response))
+ end
+
+ module ClassMethods
+ def protect_from_forgery() end
+ def consider_all_requests_local() end
+ def rescue_action(env)
+ raise env["action_dispatch.rescue.exception"]
+ end
+ end
+
+ def render_to_body(options)
+ if options.is_a?(Hash) && options.key?(:template)
+ options[:template].sub!(/^\//, '')
+ end
+
+ options[:text] = nil if options[:nothing] == true
+
+ super
+ end
+
+ def _layout_for_name(name)
+ name &&= name.sub(%r{^/?layouts/}, '')
+ super
+ end
+
+ end
+end
View
131 actionpack/lib/action_controller/new_base/conditional_get.rb
@@ -0,0 +1,131 @@
+module ActionController
+ module ConditionalGet
+
+ # Sets the etag, last_modified, or both on the response and renders a
+ # "304 Not Modified" response if the request is already fresh.
+ #
+ # Parameters:
+ # * <tt>:etag</tt>
+ # * <tt>:last_modified</tt>
+ # * <tt>:public</tt> By default the Cache-Control header is private, set this to true if you want your application to be cachable by other devices (proxy caches).
+ #
+ # Example:
+ #
+ # def show
+ # @article = Article.find(params[:id])
+ # fresh_when(:etag => @article, :last_modified => @article.created_at.utc, :public => true)
+ # end
+ #
+ # This will render the show template if the request isn't sending a matching etag or
+ # If-Modified-Since header and just a "304 Not Modified" response if there's a match.
+ #
+ def fresh_when(options)
+ options.assert_valid_keys(:etag, :last_modified, :public)
+
+ response.etag = options[:etag] if options[:etag]
+ response.last_modified = options[:last_modified] if options[:last_modified]
+
+ if options[:public]
+ cache_control = response.headers["Cache-Control"].split(",").map {|k| k.strip }
+ cache_control.delete("private")
+ cache_control.delete("no-cache")
+ cache_control << "public"
+ response.headers["Cache-Control"] = cache_control.join(', ')
+ end
+
+ if request.fresh?(response)
+ head :not_modified
+ end
+ end
+
+ # Return a response that has no content (merely headers). The options
+ # argument is interpreted to be a hash of header names and values.
+ # This allows you to easily return a response that consists only of
+ # significant headers:
+ #
+ # head :created, :location => person_path(@person)
+ #
+ # It can also be used to return exceptional conditions:
+ #
+ # return head(:method_not_allowed) unless request.post?
+ # return head(:bad_request) unless valid_request?
+ # render
+ def head(*args)
+ if args.length > 2
+ raise ArgumentError, "too many arguments to head"
+ elsif args.empty?
+ raise ArgumentError, "too few arguments to head"
+ end
+ options = args.extract_options!
+ status = interpret_status(args.shift || options.delete(:status) || :ok)
+
+ options.each do |key, value|
+ headers[key.to_s.dasherize.split(/-/).map { |v| v.capitalize }.join("-")] = value.to_s
+ end
+
+ render :nothing => true, :status => status
+ end
+
+ # Sets the etag and/or last_modified on the response and checks it against
+ # the client request. If the request doesn't match the options provided, the
+ # request is considered stale and should be generated from scratch. Otherwise,
+ # it's fresh and we don't need to generate anything and a reply of "304 Not Modified" is sent.
+ #
+ # Parameters:
+ # * <tt>:etag</tt>
+ # * <tt>:last_modified</tt>
+ # * <tt>:public</tt> By default the Cache-Control header is private, set this to true if you want your application to be cachable by other devices (proxy caches).
+ #
+ # Example:
+ #
+ # def show
+ # @article = Article.find(params[:id])
+ #
+ # if stale?(:etag => @article, :last_modified => @article.created_at.utc)
+ # @statistics = @article.really_expensive_call
+ # respond_to do |format|
+ # # all the supported formats
+ # end
+ # end
+ # end
+ def stale?(options)
+ fresh_when(options)
+ !request.fresh?(response)
+ end
+
+ # Sets a HTTP 1.1 Cache-Control header. Defaults to issuing a "private" instruction, so that
+ # intermediate caches shouldn't cache the response.
+ #
+ # Examples:
+ # expires_in 20.minutes
+ # expires_in 3.hours, :public => true
+ # expires in 3.hours, 'max-stale' => 5.hours, :public => true
+ #
+ # This method will overwrite an existing Cache-Control header.
+ # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more possibilities.
+ def expires_in(seconds, options = {}) #:doc:
+ cache_control = response.headers["Cache-Control"].split(",").map {|k| k.strip }
+
+ cache_control << "max-age=#{seconds}"
+ cache_control.delete("no-cache")
+ if options[:public]
+ cache_control.delete("private")
+ cache_control << "public"
+ else
+ cache_control << "private"
+ end
+
+ # This allows for additional headers to be passed through like 'max-stale' => 5.hours
+ cache_control += options.symbolize_keys.reject{|k,v| k == :public || k == :private }.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"}
+
+ response.headers["Cache-Control"] = cache_control.join(', ')
+ end
+
+ # Sets a HTTP 1.1 Cache-Control header of "no-cache" so no caching should occur by the browser or
+ # intermediate caches (like caching proxy servers).
+ def expires_now #:doc:
+ response.headers["Cache-Control"] = "no-cache"
+ end
+
+ end
+end
View
13 actionpack/lib/action_controller/new_base/hide_actions.rb
@@ -1,25 +1,28 @@
module ActionController
module HideActions
+ extend ActiveSupport::DependencyModule
+
included do
extlib_inheritable_accessor :hidden_actions
self.hidden_actions ||= Set.new
end
def action_methods() self.class.action_names end
- def action_names() action_methods end
-
+ def action_names() action_methods end
+
private
+
def respond_to_action?(action_name)
!hidden_actions.include?(action_name) && (super || respond_to?(:method_missing))
end
-
+
module ClassMethods
def hide_action(*args)
args.each do |arg|
self.hidden_actions << arg.to_s
end
end
-
+
def action_methods
@action_names ||= Set.new(super.reject {|name| self.hidden_actions.include?(name.to_s)})
end
@@ -27,4 +30,4 @@ def action_methods
def self.action_names() action_methods end
end
end
-end
+end
View
64 actionpack/lib/action_controller/new_base/http.rb
@@ -0,0 +1,64 @@
+module ActionController
+ class Http < AbstractController::Base
+ abstract!
+
+ # :api: public
+ attr_internal :request, :response, :params
+
+ # :api: public
+ def self.controller_name
+ @controller_name ||= controller_path.split("/").last
+ end
+
+ # :api: public
+ def controller_name() self.class.controller_name end
+
+ # :api: public
+ def self.controller_path
+ @controller_path ||= self.name.sub(/Controller$/, '').underscore
+ end
+
+ # :api: public
+ def controller_path() self.class.controller_path end
+
+ # :api: private
+ def self.internal_methods
+ ActionController::Http.public_instance_methods(true)
+ end
+
+ # :api: private
+ def self.action_names() action_methods end
+
+ # :api: private
+ def action_names() action_methods end
+
+ # :api: plugin
+ def self.call(env)
+ controller = new
+ controller.call(env).to_rack
+ end
+
+ # :api: private
+ def call(name, env)
+ @_request = ActionDispatch::Request.new(env)
+ @_response = ActionDispatch::Response.new
+ @_response.request = request
+ process(name)
+ @_response.body = response_body
+ @_response.prepare!
+ to_rack
+ end
+
+ def self.action(name)
+ @actions ||= {}
+ @actions[name] ||= proc do |env|
+ new.call(name, env)
+ end
+ end
+
+ # :api: private
+ def to_rack
+ @_response.to_a
+ end
+ end
+end
View
16 actionpack/lib/action_controller/new_base/layouts.rb
@@ -4,34 +4,36 @@ module Layouts
depends_on ActionController::Renderer
depends_on AbstractController::Layouts
-
+
module ClassMethods
def _implied_layout_name
controller_path
end
end
-
+
def render_to_body(options)
# render :text => ..., :layout => ...
# or
# render :anything_else
- if !options.key?(:text) || options.key?(:layout)
+ if (!options.key?(:text) && !options.key?(:inline)) || options.key?(:layout)
options[:_layout] = options.key?(:layout) ? _layout_for_option(options[:layout]) : _default_layout
end
-
+
super
end
-
+
private
+
def _layout_for_option(name)
case name
when String then _layout_for_name(name)
when true then _default_layout(true)
when false, nil then nil
else
- raise ArgumentError,
- "String, true, or false, expected for `layout'; you passed #{name.inspect}"
+ raise ArgumentError,
+ "String, true, or false, expected for `layout'; you passed #{name.inspect}"
end
end
+
end
end
View
52 actionpack/lib/action_controller/new_base/renderer.rb
@@ -3,48 +3,44 @@ module Renderer
extend ActiveSupport::DependencyModule
depends_on AbstractController::Renderer
-
+
def initialize(*)
self.formats = [:html]
super
end
-
- def render(action, options = {})
- # TODO: Move this into #render_to_body
- if action.is_a?(Hash)
- options, action = action, nil
- else
- options.merge! :action => action
- end
-
+
+ def render(options = {})
_process_options(options)
-
- self.response_body = render_to_body(options)
+
+ super(options)
end
def render_to_body(options)
- unless options.is_a?(Hash)
- options = {:action => options}
- end
-
if options.key?(:text)
options[:_template] = ActionView::TextTemplate.new(_text(options))
template = nil
+ elsif options.key?(:inline)
+ handler = ActionView::Template.handler_class_for_extension(options[:type] || "erb")
+ template = ActionView::Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {})
+ options[:_template] = template
elsif options.key?(:template)
options[:_template_name] = options[:template]
- elsif options.key?(:action)
- options[:_template_name] = options[:action].to_s
- options[:_prefix] = _prefix
+ else
+ options[:_template_name] = (options[:action] || action_name).to_s
+ options[:_prefix] = _prefix
end
-
- super(options)
+
+ ret = super(options)
+ response.content_type ||= options[:_template].mime_type
+ ret
end
-
+
private
+
def _prefix
controller_path
- end
-
+ end
+
def _text(options)
text = options[:text]
@@ -53,11 +49,11 @@ def _text(options)
else text.to_s
end
end
-
+
def _process_options(options)
- if status = options[:status]
- response.status = status.to_i
- end
+ status, content_type = options.values_at(:status, :content_type)
+ response.status = status.to_i if status
+ response.content_type = content_type if content_type
end
end
end
View
25 actionpack/lib/action_controller/new_base/testing.rb
@@ -0,0 +1,25 @@
+module ActionController
+ module Testing
+
+ # OMG MEGA HAX
+ def process_with_test(request, response)
+ @_request = request
+ @_response = response
+ @_response.request = request
+ ret = process(request.parameters[:action])
+ @_response.body = self.response_body
+ @_response.prepare!
+ set_test_assigns
+ ret
+ end
+
+ def set_test_assigns
+ @assigns = {}
+ (instance_variable_names - self.class.protected_instance_variables).each do |var|
+ name, value = var[1..-1], instance_variable_get(var)
+ @assigns[name] = value
+ end
+ end
+
+ end
+end
View
6 actionpack/lib/action_controller/new_base/url_for.rb
@@ -16,7 +16,7 @@ def initialize_current_url
# by this method.
def default_url_options(options = nil)
end
-
+
def rewrite_options(options) #:nodoc:
if defaults = default_url_options(options)
defaults.merge(options)
@@ -24,7 +24,7 @@ def rewrite_options(options) #:nodoc:
options
end
end
-
+
def url_for(options = {})
options ||= {}
case options
@@ -37,4 +37,4 @@ def url_for(options = {})
end
end
end
-end
+end
View
2  actionpack/lib/action_controller/routing/route_set.rb
@@ -430,7 +430,7 @@ def raise_named_route_error(options, named_route, named_route_name)
def call(env)
request = ActionDispatch::Request.new(env)
app = Routing::Routes.recognize(request)
- app.call(env)
+ app.action(request.parameters[:action] || 'index').call(env)
end
def recognize(request)
View
15 actionpack/lib/action_controller/testing/integration.rb
@@ -309,24 +309,21 @@ module ControllerCapture #:nodoc:
def self.included(base)
base.extend(ClassMethods)
base.class_eval do
- class << self
- alias_method_chain :new, :capture
- end
+ alias_method_chain :initialize, :capture
end
end
+ def initialize_with_capture(*args)
+ initialize_without_capture
+ self.class.last_instantiation ||= self
+ end
+
module ClassMethods #:nodoc:
mattr_accessor :last_instantiation
def clear_last_instantiation!
self.last_instantiation = nil
end
-
- def new_with_capture(*args)
- controller = new_without_capture(*args)
- self.last_instantiation ||= controller
- controller
- end
end
end
View
5 actionpack/lib/action_controller/testing/process.rb
@@ -131,6 +131,9 @@ def process(action, parameters = nil, session = nil, flash = nil, http_method =
@request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
build_request_uri(action, parameters)
+ @request.env["action_controller.rescue.request"] = @request
+ @request.env["action_controller.rescue.response"] = @response
+
Base.class_eval { include ProcessWithTest } unless Base < ProcessWithTest
env = @request.env
@@ -139,7 +142,7 @@ def process(action, parameters = nil, session = nil, flash = nil, http_method =
# TODO: Enable Lint
# app = Rack::Lint.new(app)
- status, headers, body = app.call(env)
+ status, headers, body = app.action(action, env)
response = Rack::MockResponse.new(status, headers, body)
@response.request, @response.template = @request, @controller.template
View
69 actionpack/lib/action_controller/testing/process2.rb
@@ -0,0 +1,69 @@
+require "action_controller/testing/process"
+
+module ActionController
+ module TestProcess
+
+ # Executes a request simulating GET HTTP method and set/volley the response
+ def get(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "GET")
+ end
+
+ # Executes a request simulating POST HTTP method and set/volley the response
+ def post(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "POST")
+ end
+
+ # Executes a request simulating PUT HTTP method and set/volley the response
+ def put(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "PUT")
+ end
+
+ # Executes a request simulating DELETE HTTP method and set/volley the response
+ def delete(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "DELETE")
+ end
+
+ # Executes a request simulating HEAD HTTP method and set/volley the response
+ def head(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "HEAD")
+ end
+
+ def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
+ # Sanity check for required instance variables so we can give an
+ # understandable error message.
+ %w(@controller @request @response).each do |iv_name|
+ if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil?
+ raise "#{iv_name} is nil: make sure you set it in your test's setup method."
+ end
+ end
+
+ @request.recycle!
+ @response.recycle!
+ @controller.response_body = nil
+
+ @html_document = nil
+ @request.env['REQUEST_METHOD'] = http_method
+
+ parameters ||= {}
+ @request.assign_parameters(@controller.class.controller_path, action.to_s, parameters)
+
+ @request.session = ActionController::TestSession.new(session) unless session.nil?
+ @request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
+ build_request_uri(action, parameters)
+ @controller.params.merge!(parameters)
+ # Base.class_eval { include ProcessWithTest } unless Base < ProcessWithTest
+ @controller.process_with_test(@request, @response)
+ end
+
+ def build_request_uri(action, parameters)
+ unless @request.env['REQUEST_URI']
+ options = @controller.__send__(:rewrite_options, parameters)
+ options.update(:only_path => true, :action => action)
+
+ url = ActionController::UrlRewriter.new(@request, parameters)
+ @request.request_uri = url.rewrite(options)
+ end
+ end
+
+ end
+end
View
2  actionpack/lib/action_dispatch/middleware/show_exceptions.rb
@@ -43,6 +43,8 @@ def initialize(app, consider_all_requests_local = false)
def call(env)
@app.call(env)
rescue Exception => exception
+ raise exception if env['rack.test']
+
log_error(exception) if logger
request = Request.new(env)
View
2  actionpack/lib/action_dispatch/middleware/stack.rb
@@ -34,8 +34,6 @@ def klass
else
@klass.to_s.constantize
end
- rescue NameError
- @klass
end
def active?
View
2  actionpack/lib/action_view/paths.rb
@@ -2,7 +2,7 @@ module ActionView #:nodoc:
class PathSet < Array #:nodoc:
def self.type_cast(obj)
if obj.is_a?(String)
- cache = !Object.const_defined?(:Rails) || Rails.configuration.cache_classes
+ cache = !defined?(Rails) || !Rails.respond_to?(:configuration) || Rails.configuration.cache_classes
Template::FileSystemPathWithFallback.new(obj, :cache => cache)
else
obj
View
7 actionpack/lib/action_view/template/text.rb
@@ -1,9 +1,12 @@
module ActionView #:nodoc:
class TextTemplate < String #:nodoc:
+
+ def identifier() self end
def render(*) self end
- def exempt_from_layout?() false end
-
+ def mime_type() Mime::HTML end
+
+ def partial?() false end
end
end
View
48 actionpack/test/abstract_controller/abstract_controller_test.rb
@@ -20,14 +20,14 @@ def index
class TestBasic < ActiveSupport::TestCase
test "dispatching works" do
result = Me.process(:index)
- assert_equal "Hello world", result.response_obj[:body]
+ assert_equal "Hello world", result.response_body
end
end
# Test Render mixin
# ====
class RenderingController < AbstractController::Base
- use Renderer
+ include Renderer
def _prefix() end
@@ -58,38 +58,38 @@ def naked_render
end
def rendering_to_body
- render_to_body "naked_render.erb"
+ self.response_body = render_to_body :_template_name => "naked_render.erb"
end
def rendering_to_string
- render_to_string "naked_render.erb"
+ self.response_body = render_to_string :_template_name => "naked_render.erb"
end
end
class TestRenderer < ActiveSupport::TestCase
test "rendering templates works" do
result = Me2.process(:index)
- assert_equal "Hello from index.erb", result.response_obj[:body]
+ assert_equal "Hello from index.erb", result.response_body
end
test "rendering passes ivars to the view" do
result = Me2.process(:action_with_ivars)
- assert_equal "Hello from index_with_ivars.erb", result.response_obj[:body]
+ assert_equal "Hello from index_with_ivars.erb", result.response_body
end
test "rendering with no template name" do
result = Me2.process(:naked_render)
- assert_equal "Hello from naked_render.erb", result.response_obj[:body]
+ assert_equal "Hello from naked_render.erb", result.response_body
end
test "rendering to a rack body" do
result = Me2.process(:rendering_to_body)
- assert_equal "Hello from naked_render.erb", result.response_obj[:body]
+ assert_equal "Hello from naked_render.erb", result.response_body
end
test "rendering to a string" do
result = Me2.process(:rendering_to_string)
- assert_equal "Hello from naked_render.erb", result.response_obj[:body]
+ assert_equal "Hello from naked_render.erb", result.response_body
end
end
@@ -121,12 +121,12 @@ def formatted
class TestPrefixedViews < ActiveSupport::TestCase
test "templates are located inside their 'prefix' folder" do
result = Me3.process(:index)
- assert_equal "Hello from me3/index.erb", result.response_obj[:body]
+ assert_equal "Hello from me3/index.erb", result.response_body
end
test "templates included their format" do
result = Me3.process(:formatted)
- assert_equal "Hello from me3/formatted.html.erb", result.response_obj[:body]
+ assert_equal "Hello from me3/formatted.html.erb", result.response_body
end
end
@@ -134,7 +134,12 @@ class TestPrefixedViews < ActiveSupport::TestCase
# ====
# self._layout is used when defined
class WithLayouts < PrefixedViews
- use Layouts
+ include Layouts
+
+ def self.inherited(klass)
+ klass._write_layout_method
+ super
+ end
private
def self.layout(formats)
@@ -147,13 +152,9 @@ def self.layout(formats)
end
end
end
-
- def _layout
- self.class.layout(formats)
- end
-
+
def render_to_body(options = {})
- options[:_layout] = options[:layout] || _layout
+ options[:_layout] = options[:layout] || _default_layout
super
end
end
@@ -173,12 +174,7 @@ def index
class TestLayouts < ActiveSupport::TestCase
test "layouts are included" do
result = Me4.process(:index)
- assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", result.response_obj[:body]
- end
-
- test "it can fall back to the application layout" do
- result = Me5.process(:index)
- assert_equal "Application Enter : Hello from me5/index.erb : Exit", result.response_obj[:body]
+ assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", result.response_body
end
end
@@ -207,7 +203,7 @@ def fail() self.response_body = "fail" end
private
def respond_to_action?(action_name)
- action_name != :fail
+ action_name.to_s != "fail"
end
end
@@ -215,7 +211,7 @@ def respond_to_action?(action_name)
class TestRespondToAction < ActiveSupport::TestCase
def assert_dispatch(klass, body = "success", action = :index)
- response = klass.process(action).response_obj[:body]
+ response = klass.process(action).response_body
assert_equal body, response
end
View
18 actionpack/test/abstract_controller/callbacks_test.rb
@@ -22,7 +22,7 @@ def index
class TestCallbacks < ActiveSupport::TestCase
test "basic callbacks work" do
result = Callback1.process(:index)
- assert_equal "Hello world", result.response_obj[:body]
+ assert_equal "Hello world", result.response_body
end
end
@@ -53,7 +53,7 @@ def index
class TestCallbacks < ActiveSupport::TestCase
test "before_filter works" do
result = Callback2.process(:index)
- assert_equal "Hello world", result.response_obj[:body]
+ assert_equal "Hello world", result.response_body
end
test "after_filter works" do
@@ -84,7 +84,7 @@ def index
class TestCallbacks < ActiveSupport::TestCase
test "before_filter works with procs" do
result = Callback3.process(:index)
- assert_equal "Hello world", result.response_obj[:body]
+ assert_equal "Hello world", result.response_body
end
test "after_filter works with procs" do
@@ -119,12 +119,12 @@ def authenticate
class TestCallbacks < ActiveSupport::TestCase
test "when :only is specified, a before filter is triggered on that action" do
result = CallbacksWithConditions.process(:index)
- assert_equal "Hello, World", result.response_obj[:body]
+ assert_equal "Hello, World", result.response_body
end
test "when :only is specified, a before filter is not triggered on other actions" do
result = CallbacksWithConditions.process(:sekrit_data)
- assert_equal "true", result.response_obj[:body]
+ assert_equal "true", result.response_body
end
test "when :except is specified, an after filter is not triggered on that action" do
@@ -159,12 +159,12 @@ def authenticate
class TestCallbacks < ActiveSupport::TestCase
test "when :only is specified with an array, a before filter is triggered on that action" do
result = CallbacksWithArrayConditions.process(:index)
- assert_equal "Hello, World", result.response_obj[:body]
+ assert_equal "Hello, World", result.response_body
end
test "when :only is specified with an array, a before filter is not triggered on other actions" do
result = CallbacksWithArrayConditions.process(:sekrit_data)
- assert_equal "true", result.response_obj[:body]
+ assert_equal "true", result.response_body
end
test "when :except is specified with an array, an after filter is not triggered on that action" do
@@ -184,12 +184,12 @@ def not_index
class TestCallbacks < ActiveSupport::TestCase
test "when a callback is modified in a child with :only, it works for the :only action" do
result = ChangedConditions.process(:index)
- assert_equal "Hello world", result.response_obj[:body]
+ assert_equal "Hello world", result.response_body
end
test "when a callback is modified in a child with :only, it does not work for other actions" do
result = ChangedConditions.process(:not_index)
- assert_equal "", result.response_obj[:body]
+ assert_equal "", result.response_body
end
end
View
6 actionpack/test/abstract_controller/helper_test.rb
@@ -4,8 +4,8 @@ module AbstractController
module Testing
class ControllerWithHelpers < AbstractController::Base
- use Renderer
- use Helpers
+ include Renderer
+ include Helpers
def render(string)
super(:_template_name => string)
@@ -35,7 +35,7 @@ def index
class TestHelpers < ActiveSupport::TestCase
def test_helpers
result = MyHelpers1.process(:index)
- assert_equal "Hello World : Included", result.response_obj[:body]
+ assert_equal "Hello World : Included", result.response_body
end
end
View
24 actionpack/test/abstract_controller/layouts_test.rb
@@ -8,7 +8,7 @@ class Base < AbstractController::Base
include AbstractController::Renderer
include AbstractController::Layouts
- self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
+ self.view_paths = [ActionView::Template::FixturePath.new(
"layouts/hello.erb" => "With String <%= yield %>",
"layouts/hello_override.erb" => "With Override <%= yield %>",
"layouts/abstract_controller_tests/layouts/with_string_implied_child.erb" =>
@@ -152,12 +152,12 @@ def index
class TestBase < ActiveSupport::TestCase
test "when no layout is specified, and no default is available, render without a layout" do
result = Blank.process(:index)
- assert_equal "Hello blank!", result.response_obj[:body]
+ assert_equal "Hello blank!", result.response_body
end
test "when layout is specified as a string, render with that layout" do
result = WithString.process(:index)
- assert_equal "With String Hello string!", result.response_obj[:body]
+ assert_equal "With String Hello string!", result.response_body
end
test "when layout is specified as a string, but the layout is missing, raise an exception" do
@@ -166,22 +166,22 @@ class TestBase < ActiveSupport::TestCase
test "when layout is specified as false, do not use a layout" do
result = WithFalseLayout.process(:index)
- assert_equal "Hello false!", result.response_obj[:body]
+ assert_equal "Hello false!", result.response_body
end
test "when layout is specified as nil, do not use a layout" do
result = WithNilLayout.process(:index)
- assert_equal "Hello nil!", result.response_obj[:body]
+ assert_equal "Hello nil!", result.response_body
end
test "when layout is specified as a symbol, call the requested method and use the layout returned" do
result = WithSymbol.process(:index)
- assert_equal "OMGHI2U Hello symbol!", result.response_obj[:body]
+ assert_equal "OMGHI2U Hello symbol!", result.response_body
end
test "when layout is specified as a symbol and the method returns nil, don't use a layout" do
result = WithSymbolReturningNil.process(:index)
- assert_equal "Hello nilz!", result.response_obj[:body]
+ assert_equal "Hello nilz!", result.response_body
end
test "when the layout is specified as a symbol and the method doesn't exist, raise an exception" do
@@ -194,28 +194,28 @@ class TestBase < ActiveSupport::TestCase
test "when a child controller does not have a layout, use the parent controller layout" do
result = WithStringChild.process(:index)
- assert_equal "With String Hello string!", result.response_obj[:body]
+ assert_equal "With String Hello string!", result.response_body
end
test "when a child controller has specified a layout, use that layout and not the parent controller layout" do
result = WithStringOverriddenChild.process(:index)
- assert_equal "With Override Hello string!", result.response_obj[:body]
+ assert_equal "With Override Hello string!", result.response_body
end
test "when a child controller has an implied layout, use that layout and not the parent controller layout" do
result = WithStringImpliedChild.process(:index)
- assert_equal "With Implied Hello string!", result.response_obj[:body]
+ assert_equal "With Implied Hello string!", result.response_body
end
test "when a child controller specifies layout nil, do not use the parent layout" do
result = WithNilChild.process(:index)
- assert_equal "Hello string!", result.response_obj[:body]
+ assert_equal "Hello string!", result.response_body
end
test "when a grandchild has no layout specified, the child has an implied layout, and the " \
"parent has specified a layout, use the child controller layout" do
result = WithChildOfImplied.process(:index)
- assert_equal "With Implied Hello string!", result.response_obj[:body]
+ assert_equal "With Implied Hello string!", result.response_body
end
test "raises an exception when specifying layout true" do
View
16 actionpack/test/abstract_controller/test_helper.rb
@@ -2,11 +2,14 @@
$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib')
$:.unshift(File.dirname(__FILE__) + '/../lib')
+require 'rubygems'
require 'test/unit'
-require 'active_support'
+require 'active_support/core/all'
require 'active_support/test_case'
-require 'action_controller'
+require 'action_controller/abstract'
+require 'action_view'
require 'action_view/base'
+require 'action_dispatch'
require 'fixture_template'
begin
@@ -15,11 +18,4 @@
Debugger.start
rescue LoadError
# Debugging disabled. `gem install ruby-debug` to enable.
-end
-
-require 'action_controller/abstract'
-# require 'action_controller/abstract/base'
-# require 'action_controller/abstract/renderer'
-# require 'action_controller/abstract/layouts'
-# require 'action_controller/abstract/callbacks'
-# require 'action_controller/abstract/helpers'
+end
View
133 actionpack/test/abstract_unit2.rb
@@ -0,0 +1,133 @@
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+$:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib')
+$:.unshift(File.dirname(__FILE__) + '/lib')
+
+
+require 'test/unit'
+require 'active_support'
+require 'active_support/core/all'
+require 'active_support/test_case'
+require 'action_controller/abstract'
+require 'action_controller/new_base'
+require 'fixture_template'
+require 'action_controller/testing/process2'
+require 'action_view/test_case'
+
+FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
+
+module ActionController
+
+ class ActionControllerError < StandardError #:nodoc:
+ end
+
+ class SessionRestoreError < ActionControllerError #:nodoc:
+ end
+
+ class RenderError < ActionControllerError #:nodoc:
+ end
+
+ class RoutingError < ActionControllerError #:nodoc:
+ attr_reader :failures
+ def initialize(message, failures=[])
+ super(message)
+ @failures = failures
+ end
+ end
+
+ class MethodNotAllowed < ActionControllerError #:nodoc:
+ attr_reader :allowed_methods
+
+ def initialize(*allowed_methods)
+ super("Only #{allowed_methods.to_sentence(:locale => :en)} requests are allowed.")
+ @allowed_methods = allowed_methods
+ end
+
+ def allowed_methods_header
+ allowed_methods.map { |method_symbol| method_symbol.to_s.upcase } * ', '
+ end
+
+ def handle_response!(response)
+ response.headers['Allow'] ||= allowed_methods_header
+ end
+ end
+
+ class NotImplemented < MethodNotAllowed #:nodoc:
+ end
+
+ class UnknownController < ActionControllerError #:nodoc:
+ end
+
+ class MissingFile < ActionControllerError #:nodoc:
+ end
+
+ class RenderError < ActionControllerError #:nodoc:
+ end
+
+ class SessionOverflowError < ActionControllerError #:nodoc:
+ DEFAULT_MESSAGE = 'Your session data is larger than the data column in which it is to be stored. You must increase the size of your data column if you intend to store large data.'
+
+ def initialize(message = nil)
+ super(message || DEFAULT_MESSAGE)
+ end
+ end
+
+ class UnknownHttpMethod < ActionControllerError #:nodoc:
+ end
+
+ class Base
+ use ActionController::Testing
+ end
+
+ Base.view_paths = FIXTURE_LOAD_PATH
+
+ class TestCase
+ include TestProcess
+ setup do
+ ActionController::Routing::Routes.draw do |map|
+ map.connect ':controller/:action/:id'
+ end
+ end
+
+ def assert_template(options = {}, message = nil)
+ validate_response!
+
+ clean_backtrace do
+ case options
+ when NilClass, String
+ hax = @controller._action_view.instance_variable_get(:@_rendered)
+ rendered = (hax[:template] || []).map { |t| t.identifier }
+ msg = build_message(message,
+ "expecting <?> but rendering with <?>",
+ options, rendered.join(', '))
+ assert_block(msg) do
+ if options.nil?
+ hax[:template].blank?
+ else
+ rendered.any? { |t| t.match(options) }
+ end
+ end
+ when Hash
+ if expected_partial = options[:partial]
+ partials = hax[:partials]
+ if expected_count = options[:count]
+ found = partials.detect { |p, _| p.identifier.match(expected_partial) }
+ actual_count = found.nil? ? 0 : found.second
+ msg = build_message(message,
+ "expecting ? to be rendered ? time(s) but rendered ? time(s)",
+ expected_partial, expected_count, actual_count)
+ assert(actual_count == expected_count.to_i, msg)
+ else
+ msg = build_message(message,
+ "expecting partial <?> but action rendered <?>",
+ options[:partial], partials.keys)
+ assert(partials.keys.any? { |p| p.identifier.match(expected_partial) }, msg)
+ end
+ else
+ assert hax[:partials].empty?,
+ "Expected no partials to be rendered"
+ end
+ end
+ end
+ end
+ end
+end
View
10 actionpack/test/controller/content_type_test.rb
@@ -1,24 +1,29 @@
require 'abstract_unit'
class ContentTypeController < ActionController::Base
+ # :ported:
def render_content_type_from_body
response.content_type = Mime::RSS
render :text => "hello world!"
end
+ # :ported:
def render_defaults
render :text => "hello world!"
end
+ # :ported:
def render_content_type_from_render
render :text => "hello world!", :content_type => Mime::RSS
end
+ # :ported:
def render_charset_from_body
response.charset = "utf-16"
render :text => "hello world!"
end
+ # :ported:
def render_nil_charset_from_body
response.charset = nil
render :text => "hello world!"
@@ -60,6 +65,7 @@ def setup
@controller.logger = Logger.new(nil)
end
+ # :ported:
def test_render_defaults
get :render_defaults
assert_equal "utf-8", @response.charset
@@ -74,24 +80,28 @@ def test_render_changed_charset_default
ContentTypeController.default_charset = "utf-8"
end
+ # :ported:
def test_content_type_from_body
get :render_content_type_from_body
assert_equal "application/rss+xml", @response.content_type
assert_equal "utf-8", @response.charset
end
+ # :ported:
def test_content_type_from_render
get :render_content_type_from_render
assert_equal "application/rss+xml", @response.content_type
assert_equal "utf-8", @response.charset
end
+ # :ported:
def test_charset_from_body
get :render_charset_from_body
assert_equal Mime::HTML, @response.content_type
assert_equal "utf-16", @response.charset
end
+ # :ported:
def test_nil_charset_from_body
get :render_nil_charset_from_body
assert_equal Mime::HTML, @response.content_type
View
3  actionpack/test/controller/filters_test.rb
@@ -607,8 +607,9 @@ def test_filters_with_mixed_specialization_run_in_order
def test_dynamic_dispatch
%w(foo bar baz).each do |action|
request = ActionController::TestRequest.new
+ request.env["action_controller.rescue.request"] = request
request.query_parameters[:choose] = action
- response = Rack::MockResponse.new(*DynamicDispatchController.call(request.env))
+ response = DynamicDispatchController.action.call(request.env).last
assert_equal action, response.body
end
end
View
44 actionpack/test/controller/helper_test.rb
@@ -101,20 +101,30 @@ def test_helper_attr
assert master_helper_methods.include?('delegate_attr=')
end
- def test_helper_for_nested_controller
- request = ActionController::TestRequest.new
- request.action = 'render_hello_world'
+ def call_controller(klass, action)
+ request = ActionController::TestRequest.new
+ request.env["action_controller.rescue.request"] = request
+ klass.action(action).call(request.env)
+ end
- response = Rack::MockResponse.new(*Fun::GamesController.call(request.env))
- assert_equal 'hello: Iz guuut!', response.body
+ def test_helper_for_nested_controller
+ assert_equal 'hello: Iz guuut!',
+ call_controller(Fun::GamesController, "render_hello_world").last.body
+ # request = ActionController::TestRequest.new
+ # request.env["action_controller.rescue.request"] = request
+ #
+ # resp = Fun::GamesController.action(:render_hello_world).call(request.env)
+ # assert_equal 'hello: Iz guuut!', resp.last.body
end
def test_helper_for_acronym_controller
- request = ActionController::TestRequest.new
- request.action = 'test'
-
- response = Rack::MockResponse.new(*Fun::PdfController.call(request.env))
- assert_equal 'test: baz', response.body
+ assert_equal "test: baz", call_controller(Fun::PdfController, "test").last.body
+ #
+ # request = ActionController::TestRequest.new
+ # response = ActionController::TestResponse.new
+ # request.action = 'test'
+ #
+ # assert_equal 'test: baz', Fun::PdfController.process(request, response).body
end
def test_all_helpers
@@ -204,6 +214,12 @@ def index
end
end
+ def call_controller(klass, action)
+ request = ActionController::TestRequest.new
+ request.env["action_controller.rescue.request"] = request
+ klass.action(action).call(request.env)
+ end
+
def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@@ -211,16 +227,14 @@ def setup
end
def test_helper_in_a
- assert_raise(ActionView::TemplateError) { A.call(@request.env) }
+ assert_raise(ActionView::TemplateError) { call_controller(A, "index") }
end
def test_helper_in_b
- response = Rack::MockResponse.new(*B.call(@request.env))
- assert_equal 'B', response.body
+ assert_equal 'B', call_controller(B, "index").last.body
end
def test_helper_in_c
- response = Rack::MockResponse.new(*C.call(@request.env))
- assert_equal 'C', response.body
+ assert_equal 'C', call_controller(C, "index").last.body
end
end
View
74 actionpack/test/controller/render_test.rb
@@ -1,9 +1,10 @@
-require 'abstract_unit'
+require ENV['new_base'] ? 'abstract_unit2' : 'abstract_unit'
require 'controller/fake_models'
require 'pathname'
module Fun
class GamesController < ActionController::Base
+ # :ported:
def hello_world
end
end
@@ -80,6 +81,7 @@ def handle_last_modified_and_etags
fresh_when(:last_modified => Time.now.utc.beginning_of_day, :etag => [ :foo, 123 ])
end
+ # :ported:
def render_hello_world
render :template => "test/hello_world"
end
@@ -94,23 +96,28 @@ def render_hello_world_with_etag_set
render :template => "test/hello_world"
end
+ # :ported: compatibility
def render_hello_world_with_forward_slash
render :template => "/test/hello_world"
end
+ # :ported:
def render_template_in_top_directory
render :template => 'shared'
end
+ # :deprecated:
def render_template_in_top_directory_with_slash
render :template => '/shared'
end
+ # :ported:
def render_hello_world_from_variable
@person = "david"
render :text => "hello #{@person}"
end
+ # :ported:
def render_action_hello_world
render :action => "hello_world"
end
@@ -123,10 +130,12 @@ def render_action_hello_world_with_symbol
render :action => :hello_world
end
+ # :ported:
def render_text_hello_world
render :text => "hello world"
end
+ # :ported:
def render_text_hello_world_with_layout
@variable_for_layout = ", I'm here!"
render :text => "hello world", :layout => true
@@ -218,6 +227,7 @@ def render_json_with_render_to_string
render :json => {:hello => render_to_string(:partial => 'partial')}
end
+ # :ported:
def render_custom_code
render :text => "hello world", :status => 404
end
@@ -228,14 +238,17 @@ def render_custom_code_rjs
end
end
+ # :ported:
def render_text_with_nil
render :text => nil
end
+ # :ported:
def render_text_with_false
render :text => false
end
+ # :ported:
def render_nothing_with_appendix