diff options
51 files changed, 1434 insertions, 428 deletions
diff --git a/actionpack/Rakefile b/actionpack/Rakefile index 300c2ebf81..18610ed773 100644 --- a/actionpack/Rakefile +++ b/actionpack/Rakefile @@ -23,14 +23,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 +43,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 diff --git a/actionpack/lib/action_controller/abstract/base.rb b/actionpack/lib/action_controller/abstract/base.rb index ade7719cc0..ab9aed0b26 100644 --- a/actionpack/lib/action_controller/abstract/base.rb +++ b/actionpack/lib/action_controller/abstract/base.rb @@ -4,13 +4,44 @@ module AbstractController attr_internal :response_body attr_internal :response_obj attr_internal :action_name - - def self.process(action) - new.process(action) + + 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 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 - def self.inherited(klass) - end + abstract! def initialize self.response_obj = {} @@ -23,19 +54,32 @@ module AbstractController @_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
\ No newline at end of file diff --git a/actionpack/lib/action_controller/abstract/callbacks.rb b/actionpack/lib/action_controller/abstract/callbacks.rb index d7faaf4236..6e15b3e81b 100644 --- a/actionpack/lib/action_controller/abstract/callbacks.rb +++ b/actionpack/lib/action_controller/abstract/callbacks.rb @@ -17,11 +17,11 @@ module AbstractController 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 diff --git a/actionpack/lib/action_controller/abstract/helpers.rb b/actionpack/lib/action_controller/abstract/helpers.rb index 62caa119e7..968d3080c1 100644 --- a/actionpack/lib/action_controller/abstract/helpers.rb +++ b/actionpack/lib/action_controller/abstract/helpers.rb @@ -8,14 +8,7 @@ module AbstractController extlib_inheritable_accessor :master_helper_module 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 diff --git a/actionpack/lib/action_controller/abstract/layouts.rb b/actionpack/lib/action_controller/abstract/layouts.rb index 76130f8dc0..e48b8b2b4b 100644 --- a/actionpack/lib/action_controller/abstract/layouts.rb +++ b/actionpack/lib/action_controller/abstract/layouts.rb @@ -51,13 +51,16 @@ module AbstractController 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}" @@ -68,10 +71,10 @@ module AbstractController def _default_layout(require_layout = false) if require_layout && !_layout - raise ArgumentError, + raise ArgumentError, "There was no default layout for #{self.class} in #{view_paths.inspect}" end - + begin layout = _layout_for_name(_layout) rescue NameError => e diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb index beb848f90e..b58688c9da 100644 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ b/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 @@ -19,6 +28,10 @@ module AbstractController end def render(options = {}) + if response_body + raise AbstractController::DoubleRenderError, "OMG" + end + self.response_body = render_to_body(options) end @@ -31,8 +44,9 @@ module AbstractController 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. diff --git a/actionpack/lib/action_controller/base/base.rb b/actionpack/lib/action_controller/base/base.rb index 7bbde519cc..d25801b17b 100644 --- a/actionpack/lib/action_controller/base/base.rb +++ b/actionpack/lib/action_controller/base/base.rb @@ -365,17 +365,23 @@ module ActionController #:nodoc: 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 @@ module ActionController #:nodoc: assign_shortcuts(request, response) initialize_template_class(response) initialize_current_url - assign_names log_processing send(method, *arguments) @@ -882,10 +887,6 @@ module ActionController #:nodoc: @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 diff --git a/actionpack/lib/action_controller/dispatch/dispatcher.rb b/actionpack/lib/action_controller/dispatch/dispatcher.rb index cce3b6175d..63866caed9 100644 --- a/actionpack/lib/action_controller/dispatch/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatch/dispatcher.rb @@ -48,7 +48,7 @@ module ActionController 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 diff --git a/actionpack/lib/action_controller/dispatch/middlewares.rb b/actionpack/lib/action_controller/dispatch/middlewares.rb index 31a7b00d28..f99637b109 100644 --- a/actionpack/lib/action_controller/dispatch/middlewares.rb +++ b/actionpack/lib/action_controller/dispatch/middlewares.rb @@ -14,4 +14,4 @@ use lambda { ActionController::Base.session_store }, use "ActionDispatch::ParamsParser" use "Rack::MethodOverride" -use "Rack::Head" +use "Rack::Head"
\ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base.rb b/actionpack/lib/action_controller/new_base.rb index 7c65f1cdc1..8a7de1476c 100644 --- a/actionpack/lib/action_controller/new_base.rb +++ b/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" -end
\ No newline at end of file + 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'
\ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb index 08e7a1a0e7..4892886341 100644 --- a/actionpack/lib/action_controller/new_base/base.rb +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -1,60 +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 - 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 + class Base < Http + abstract! - # :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) + 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: private - def self.action_names() action_methods end + def self.subclasses + @subclasses ||= [] + end - # :api: private - def action_methods() self.class.action_names end - - # :api: private - def action_names() action_methods end + def self.app_loaded! + @subclasses.each do |subclass| + subclass.constantize._write_layout_method + end + 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
\ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/compatibility.rb b/actionpack/lib/action_controller/new_base/compatibility.rb new file mode 100644 index 0000000000..4655a94923 --- /dev/null +++ b/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
\ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/conditional_get.rb b/actionpack/lib/action_controller/new_base/conditional_get.rb new file mode 100644 index 0000000000..e1407e671a --- /dev/null +++ b/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
\ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/hide_actions.rb b/actionpack/lib/action_controller/new_base/hide_actions.rb index aa420442fb..d1857a9169 100644 --- a/actionpack/lib/action_controller/new_base/hide_actions.rb +++ b/actionpack/lib/action_controller/new_base/hide_actions.rb @@ -1,5 +1,7 @@ module ActionController module HideActions + extend ActiveSupport::DependencyModule + included do extlib_inheritable_accessor :hidden_actions self.hidden_actions ||= Set.new diff --git a/actionpack/lib/action_controller/new_base/http.rb b/actionpack/lib/action_controller/new_base/http.rb new file mode 100644 index 0000000000..f269fe70db --- /dev/null +++ b/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 diff --git a/actionpack/lib/action_controller/new_base/layouts.rb b/actionpack/lib/action_controller/new_base/layouts.rb index 89d24fe92d..e851eb5f9a 100644 --- a/actionpack/lib/action_controller/new_base/layouts.rb +++ b/actionpack/lib/action_controller/new_base/layouts.rb @@ -15,7 +15,7 @@ module ActionController # 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 diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index be4ea54c3b..41e3dfbe23 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -9,35 +9,30 @@ module ActionController 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 + 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 @@ -56,9 +51,9 @@ module ActionController 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 diff --git a/actionpack/lib/action_controller/new_base/testing.rb b/actionpack/lib/action_controller/new_base/testing.rb new file mode 100644 index 0000000000..106990b9ba --- /dev/null +++ b/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
\ No newline at end of file diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb index 172b867bf0..45ad8a3a3b 100644 --- a/actionpack/lib/action_controller/routing/route_set.rb +++ b/actionpack/lib/action_controller/routing/route_set.rb @@ -430,7 +430,7 @@ module ActionController 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) diff --git a/actionpack/lib/action_controller/testing/integration.rb b/actionpack/lib/action_controller/testing/integration.rb index 4f39ee6a01..d6991ab4f5 100644 --- a/actionpack/lib/action_controller/testing/integration.rb +++ b/actionpack/lib/action_controller/testing/integration.rb @@ -309,24 +309,21 @@ module ActionController 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 diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb index 49e8322491..21023ac101 100644 --- a/actionpack/lib/action_controller/testing/process.rb +++ b/actionpack/lib/action_controller/testing/process.rb @@ -131,6 +131,9 @@ module ActionController #:nodoc: @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 @@ module ActionController #:nodoc: # 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 diff --git a/actionpack/lib/action_controller/testing/process2.rb b/actionpack/lib/action_controller/testing/process2.rb new file mode 100644 index 0000000000..18b7335450 --- /dev/null +++ b/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
\ No newline at end of file diff --git a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb index 791254cdf2..71c1e1b9a9 100644 --- a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb @@ -43,6 +43,8 @@ module ActionDispatch def call(env) @app.call(env) rescue Exception => exception + raise exception if env['rack.test'] + log_error(exception) if logger request = Request.new(env) diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index ec8a9ca76e..ade2d6f05e 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -34,8 +34,6 @@ module ActionDispatch else @klass.to_s.constantize end - rescue NameError - @klass end def active? diff --git a/actionpack/lib/action_view/paths.rb b/actionpack/lib/action_view/paths.rb index f6d021c92a..95c56faf9c 100644 --- a/actionpack/lib/action_view/paths.rb +++ b/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 diff --git a/actionpack/lib/action_view/template/text.rb b/actionpack/lib/action_view/template/text.rb index f81174d707..a777021a12 100644 --- a/actionpack/lib/action_view/template/text.rb +++ b/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 diff --git a/actionpack/test/abstract_controller/abstract_controller_test.rb b/actionpack/test/abstract_controller/abstract_controller_test.rb index 331797afcf..689aa99fd8 100644 --- a/actionpack/test/abstract_controller/abstract_controller_test.rb +++ b/actionpack/test/abstract_controller/abstract_controller_test.rb @@ -20,14 +20,14 @@ module AbstractController 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 @@ module AbstractController 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 @@ module AbstractController 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 @@ module AbstractController # ==== # 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 @@ module AbstractController 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 @@ module AbstractController 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 @@ module AbstractController private def respond_to_action?(action_name) - action_name != :fail + action_name.to_s != "fail" end end @@ -215,7 +211,7 @@ module AbstractController 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 diff --git a/actionpack/test/abstract_controller/callbacks_test.rb b/actionpack/test/abstract_controller/callbacks_test.rb index 89243b631e..410b1e57a6 100644 --- a/actionpack/test/abstract_controller/callbacks_test.rb +++ b/actionpack/test/abstract_controller/callbacks_test.rb @@ -22,7 +22,7 @@ module AbstractController 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 @@ module AbstractController 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 @@ module AbstractController 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 @@ module AbstractController 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 @@ module AbstractController 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 @@ module AbstractController 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 diff --git a/actionpack/test/abstract_controller/helper_test.rb b/actionpack/test/abstract_controller/helper_test.rb index 6284fa4f70..f91aefe606 100644 --- a/actionpack/test/abstract_controller/helper_test.rb +++ b/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 @@ module AbstractController 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 diff --git a/actionpack/test/abstract_controller/layouts_test.rb b/actionpack/test/abstract_controller/layouts_test.rb index a305a30a54..6e1c2bf9e8 100644 --- a/actionpack/test/abstract_controller/layouts_test.rb +++ b/actionpack/test/abstract_controller/layouts_test.rb @@ -8,7 +8,7 @@ module AbstractControllerTests 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 @@ module AbstractControllerTests 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 @@ module AbstractControllerTests 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 @@ module AbstractControllerTests 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 diff --git a/actionpack/test/abstract_controller/test_helper.rb b/actionpack/test/abstract_controller/test_helper.rb index b9248c6bbd..a08ab5a6d7 100644 --- a/actionpack/test/abstract_controller/test_helper.rb +++ b/actionpack/test/abstract_controller/test_helper.rb @@ -2,11 +2,14 @@ $:.unshift(File.dirname(__FILE__) + '/../../lib') $:.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 @@ begin 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'
\ No newline at end of file +end
\ No newline at end of file diff --git a/actionpack/test/abstract_unit2.rb b/actionpack/test/abstract_unit2.rb new file mode 100644 index 0000000000..95e7b2e273 --- /dev/null +++ b/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
\ No newline at end of file diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb index 7377546631..64b8b10d5b 100644 --- a/actionpack/test/controller/content_type_test.rb +++ b/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 @@ class ContentTypeTest < ActionController::TestCase @controller.logger = Logger.new(nil) end + # :ported: def test_render_defaults get :render_defaults assert_equal "utf-8", @response.charset @@ -74,24 +80,28 @@ class ContentTypeTest < ActionController::TestCase 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 diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index 5fd3eb0446..9ad49e9282 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -607,8 +607,9 @@ class FilterTest < Test::Unit::TestCase 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 diff --git a/actionpack/test/controller/helper_test.rb b/actionpack/test/controller/helper_test.rb index e72bce1791..19cd5f4db2 100644 --- a/actionpack/test/controller/helper_test.rb +++ b/actionpack/test/controller/helper_test.rb @@ -101,20 +101,30 @@ class HelperTest < Test::Unit::TestCase 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 @@ class IsolatedHelpersTest < Test::Unit::TestCase 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 @@ class IsolatedHelpersTest < Test::Unit::TestCase 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 diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index dd2dd7a502..a58d9b08b5 100644 --- a/actionpack/test/controller/render_test.rb +++ b/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 @@ class TestController < ActionController::Base 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 @@ class TestController < ActionController::Base 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 @@ class TestController < ActionController::Base 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 @@ class TestController < ActionController::Base 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 @@ class TestController < ActionController::Base 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 render :text => "appended" end @@ -244,6 +257,10 @@ class TestController < ActionController::Base render :js => "alert('hello')" end + # This test is testing 3 things: + # render :file in AV :ported: + # render :template in AC :ported: + # setting content type def render_xml_hello @name = "David" render :template => "test/hello" @@ -270,10 +287,12 @@ class TestController < ActionController::Base # let's just rely on the template end + # :ported: def blank_response render :text => ' ' end + # :ported: def layout_test render :action => "hello_world" end @@ -281,7 +300,8 @@ class TestController < ActionController::Base def builder_layout_test render :action => "hello", :layout => "layouts/builder" end - + + # :move: test this in ActionView def builder_partial_test render :action => "hello_world_container" end @@ -391,6 +411,7 @@ class TestController < ActionController::Base render :layout => true, :inline => "Hello: <%= params[:name] %>" end + # :ported: def render_with_explicit_template render :template => "test/hello_world" end @@ -399,10 +420,12 @@ class TestController < ActionController::Base render "test/hello_world" end + # :ported: def render_with_explicit_template_with_locals render :template => "test/render_file_with_locals", :locals => { :secret => 'area51' } end + # :ported: def double_render render :text => "hello" render :text => "world" @@ -434,10 +457,15 @@ class TestController < ActionController::Base render :action => "potential_conflicts" end + # :deprecated: + # Tests being able to pick a .builder template over a .erb + # For instance, being able to have hello.xml.builder and hello.xml.erb + # and select one via "hello.builder" or "hello.erb" def hello_world_from_rxml_using_action render :action => "hello_world_from_rxml.builder" end + # :deprecated: def hello_world_from_rxml_using_template render :template => "test/hello_world_from_rxml.builder" end @@ -506,6 +534,7 @@ class TestController < ActionController::Base # Action template sets variable that's picked up by layout end + # :addressed: def render_text_with_assigns @hello = "world" render :text => "foo" @@ -755,6 +784,7 @@ class RenderTest < ActionController::TestCase @request.host = "www.nextangle.com" end + # :ported: def test_simple_show get :hello_world assert_response 200 @@ -763,11 +793,13 @@ class RenderTest < ActionController::TestCase assert_equal "<html>Hello world!</html>", @response.body end + # :ported: def test_renders_default_template_for_missing_action get :'hyphen-ated' assert_template 'test/hyphen-ated' end + # :ported: def test_render get :render_hello_world assert_template "test/hello_world" @@ -785,54 +817,64 @@ class RenderTest < ActionController::TestCase end end + # :ported: compatibility def test_render_with_forward_slash get :render_hello_world_with_forward_slash assert_template "test/hello_world" end + # :ported: def test_render_in_top_directory get :render_template_in_top_directory assert_template "shared" assert_equal "Elastica", @response.body end + # :ported: def test_render_in_top_directory_with_slash get :render_template_in_top_directory_with_slash assert_template "shared" assert_equal "Elastica", @response.body end + # :ported: def test_render_from_variable get :render_hello_world_from_variable assert_equal "hello david", @response.body end + # :ported: def test_render_action get :render_action_hello_world assert_template "test/hello_world" end + # :ported: def test_render_action_hello_world_as_string get :render_action_hello_world_as_string assert_equal "Hello world!", @response.body assert_template "test/hello_world" end + # :ported: def test_render_action_with_symbol get :render_action_hello_world_with_symbol assert_template "test/hello_world" end + # :ported: def test_render_text get :render_text_hello_world assert_equal "hello world", @response.body end + # :ported: def test_do_with_render_text_and_layout get :render_text_hello_world_with_layout assert_equal "<html>hello world, I'm here!</html>", @response.body end + # :ported: def test_do_with_render_action_and_layout_false get :hello_world_with_layout_false assert_equal 'Hello world!', @response.body @@ -914,6 +956,7 @@ class RenderTest < ActionController::TestCase assert_equal 'application/json', @response.content_type end + # :ported: def test_render_custom_code get :render_custom_code assert_response 404 @@ -927,31 +970,37 @@ class RenderTest < ActionController::TestCase assert_equal %(Element.replace("foo", "partial html");), @response.body end + # :ported: def test_render_text_with_nil get :render_text_with_nil assert_response 200 assert_equal ' ', @response.body end + # :ported: def test_render_text_with_false get :render_text_with_false assert_equal 'false', @response.body end + # :ported: def test_render_nothing_with_appendix get :render_nothing_with_appendix assert_response 200 assert_equal 'appended', @response.body end + # :ported: def test_attempt_to_access_object_method assert_raise(ActionController::UnknownAction, "No action responded to [clone]") { get :clone } end + # :ported: def test_private_methods assert_raise(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout } end + # :ported: def test_access_to_request_in_view get :accessing_request_in_template assert_equal "Hello: www.nextangle.com", @response.body @@ -962,11 +1011,13 @@ class RenderTest < ActionController::TestCase assert_equal "Logger", @response.body end + # :ported: def test_access_to_action_name_in_view get :accessing_action_name_in_template assert_equal "accessing_action_name_in_template", @response.body end + # :ported: def test_access_to_controller_name_in_view get :accessing_controller_name_in_template assert_equal "test", @response.body # name is explicitly set to 'test' inside the controller. @@ -978,6 +1029,7 @@ class RenderTest < ActionController::TestCase assert_equal "text/javascript", @response.content_type end + # :ported: def test_render_xml get :render_xml_hello assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body @@ -990,6 +1042,7 @@ class RenderTest < ActionController::TestCase assert_equal "application/xml", @response.content_type end + # :ported: def test_render_xml_with_default get :greeting assert_equal "<p>This is grand!</p>\n", @response.body @@ -1044,6 +1097,7 @@ class RenderTest < ActionController::TestCase assert_template "test/hello_world" end + # :ported: def test_nested_rendering @controller = Fun::GamesController.new get :hello_world @@ -1204,6 +1258,7 @@ class RenderTest < ActionController::TestCase assert_equal "<html>Hello world!</html>", @response.body end + # :ported: def test_double_render assert_raise(ActionController::DoubleRenderError) { get :double_render } end @@ -1232,11 +1287,13 @@ class RenderTest < ActionController::TestCase assert_equal "<title>Talking to the layout</title>\nAction was here!", @response.body end + # :addressed: def test_render_text_with_assigns get :render_text_with_assigns assert_equal "world", assigns["hello"] end + # :ported: def test_template_with_locals get :render_with_explicit_template_with_locals assert_equal "The secret is area51\n", @response.body @@ -1603,13 +1660,13 @@ class EtagRenderTest < ActionController::TestCase def test_render_against_etag_request_should_200_when_no_match @request.if_none_match = etag_for("hello somewhere else") get :render_hello_world_from_variable - assert_equal 200, @response.status + assert_equal 200, @response.status.to_i assert !@response.body.empty? end def test_render_should_not_set_etag_when_last_modified_has_been_specified get :render_hello_world_with_last_modified_set - assert_equal 200, @response.status + assert_equal 200, @response.status.to_i assert_not_nil @response.last_modified assert_nil @response.etag assert @response.body.present? @@ -1623,11 +1680,12 @@ class EtagRenderTest < ActionController::TestCase @request.if_none_match = expected_etag get :render_hello_world_from_variable - assert_equal 304, @response.status + assert_equal 304, @response.status.to_i + @response = ActionController::TestResponse.new @request.if_none_match = "\"diftag\"" get :render_hello_world_from_variable - assert_equal 200, @response.status + assert_equal 200, @response.status.to_i end def render_with_404_shouldnt_have_etag @@ -1695,7 +1753,7 @@ class LastModifiedRenderTest < ActionController::TestCase def test_request_not_modified @request.if_modified_since = @last_modified get :conditional_hello - assert_equal 304, @response.status + assert_equal 304, @response.status.to_i assert @response.body.blank?, @response.body assert_equal @last_modified, @response.headers['Last-Modified'] end @@ -1710,7 +1768,7 @@ class LastModifiedRenderTest < ActionController::TestCase def test_request_modified @request.if_modified_since = 'Thu, 16 Jul 2008 00:00:00 GMT' get :conditional_hello - assert_equal 200, @response.status + assert_equal 200, @response.status.to_i assert !@response.body.blank? assert_equal @last_modified, @response.headers['Last-Modified'] end diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb index 26f6ec2d0c..e43e329a9e 100644 --- a/actionpack/test/lib/fixture_template.rb +++ b/actionpack/test/lib/fixture_template.rb @@ -1,35 +1,104 @@ module ActionView #:nodoc: - class FixtureTemplate < Template - class FixturePath < Template::Path - def initialize(hash = {}) - @hash = {} - - hash.each do |k, v| - @hash[k.sub(/\.\w+$/, '')] = FixtureTemplate.new(v, k.split("/").last, self) +class Template + class FixturePath < Path + def initialize(hash = {}, options = {}) + super(options) + @hash = hash + end + + def find_templates(name, details, prefix, partial) + if regexp = details_to_regexp(name, details, prefix, partial) + cached(regexp) do + templates = [] + @hash.select { |k,v| k =~ regexp }.each do |path, source| + templates << Template.new(source, path, *path_to_details(path)) + end + templates end - - super("fixtures://root") - end - - def find_template(path) - @hash[path] end end - def initialize(body, *args) - @body = body - super(*args) + private + + def formats_regexp + @formats_regexp ||= begin + formats = Mime::SET.map { |m| m.symbol } + '(?:' + formats.map { |l| "\\.#{Regexp.escape(l.to_s)}" }.join('|') + ')?' + end end - def source - @body + def handler_regexp + e = TemplateHandlers.extensions.map{|h| "\\.#{Regexp.escape(h.to_s)}"}.join("|") + "(?:#{e})?" end - private - - def find_full_path(path, load_paths) - return '/', path + def details_to_regexp(name, details, prefix, partial) + path = "" + path << "#{prefix}/" unless prefix.empty? + path << (partial ? "_#{name}" : name) + + extensions = "" + [:locales, :formats].each do |k| + extensions << if exts = details[k] + '(?:' + exts.map {|e| "\\.#{Regexp.escape(e.to_s)}"}.join('|') + ')?' + else + k == :formats ? formats_regexp : '' + end + end + + %r'#{Regexp.escape(path)}#{extensions}#{handler_regexp}' + end + + # TODO: fix me + # :api: plugin + def path_to_details(path) + # [:erb, :format => :html, :locale => :en, :partial => true/false] + if m = path.match(%r'(_)?[\w-]+(\.[\w-]+)*\.(\w+)$') + partial = m[1] == '_' + details = (m[2]||"").split('.').reject { |e| e.empty? } + handler = Template.handler_class_for_extension(m[3]) + + format = Mime[details.last] && details.pop.to_sym + locale = details.last && details.pop.to_sym + + return handler, :format => format, :locale => locale, :partial => partial + end end - end + + + # class FixtureTemplate < Template + # class FixturePath < Template::Path + # def initialize(hash = {}) + # @hash = {} + # + # hash.each do |k, v| + # @hash[k.sub(/\.\w+$/, '')] = FixtureTemplate.new(v, k.split("/").last, self) + # end + # + # super("fixtures://root") + # end + # + # def find_template(path) + # @hash[path] + # end + # end + # + # def initialize(body, *args) + # @body = body + # super(*args) + # end + # + # def source + # @body + # end + # + # private + # + # def find_full_path(path, load_paths) + # return '/', path + # end + # + # end +end end
\ No newline at end of file diff --git a/actionpack/test/new_base/base_test.rb b/actionpack/test/new_base/base_test.rb index 4f46cb6492..a32653f128 100644 --- a/actionpack/test/new_base/base_test.rb +++ b/actionpack/test/new_base/base_test.rb @@ -1,8 +1,8 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") # Tests the controller dispatching happy path -module HappyPath - class SimpleDispatchController < ActionController::Base2 +module Dispatching + class SimpleController < ActionController::Base def index render :text => "success" end @@ -23,7 +23,7 @@ module HappyPath class TestSimpleDispatch < SimpleRouteCase - get "/happy_path/simple_dispatch/index" + get "/dispatching/simple/index" test "sets the body" do assert_body "success" @@ -34,57 +34,56 @@ module HappyPath end test "sets the content type" do - assert_content_type Mime::HTML + assert_content_type "text/html; charset=utf-8" end test "sets the content length" do - assert_header "Content-Length", 7 + assert_header "Content-Length", "7" end end # :api: plugin class TestDirectResponseMod < SimpleRouteCase - get "/happy_path/simple_dispatch/modify_response_body" + get "/dispatching/simple/modify_response_body" test "sets the body" do assert_body "success" end test "setting the body manually sets the content length" do - assert_header "Content-Length", 7 + assert_header "Content-Length", "7" end end # :api: plugin class TestDirectResponseModTwice < SimpleRouteCase - get "/happy_path/simple_dispatch/modify_response_body_twice" + get "/dispatching/simple/modify_response_body_twice" test "self.response_body= returns the body being set" do assert_body "success!" end test "updating the response body updates the content length" do - assert_header "Content-Length", 8 + assert_header "Content-Length", "8" end end -end - - -class EmptyController < ActionController::Base2 ; end -module Submodule - class ContainedEmptyController < ActionController::Base2 ; end -end + + class EmptyController < ActionController::Base ; end + module Submodule + class ContainedEmptyController < ActionController::Base ; end + end -class ControllerClassTests < Test::Unit::TestCase - def test_controller_path - assert_equal 'empty', EmptyController.controller_path - assert_equal EmptyController.controller_path, EmptyController.new.controller_path - assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path - assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path + class ControllerClassTests < Test::Unit::TestCase + def test_controller_path + assert_equal 'dispatching/empty', EmptyController.controller_path + assert_equal EmptyController.controller_path, EmptyController.new.controller_path + assert_equal 'dispatching/submodule/contained_empty', Submodule::ContainedEmptyController.controller_path + assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path + end + def test_controller_name + assert_equal 'empty', EmptyController.controller_name + assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name + end end - def test_controller_name - assert_equal 'empty', EmptyController.controller_name - assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name - end end
\ No newline at end of file diff --git a/actionpack/test/new_base/content_type_test.rb b/actionpack/test/new_base/content_type_test.rb new file mode 100644 index 0000000000..a5c04e9cb6 --- /dev/null +++ b/actionpack/test/new_base/content_type_test.rb @@ -0,0 +1,111 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module ContentType + class BaseController < ActionController::Base + def index + render :text => "Hello world!" + end + + def set_on_response_obj + response.content_type = Mime::RSS + render :text => "Hello world!" + end + + def set_on_render + render :text => "Hello world!", :content_type => Mime::RSS + end + end + + class TestDefault < SimpleRouteCase + describe "a default response is HTML and UTF8" + + get "/content_type/base" + assert_body "Hello world!" + assert_header "Content-Type", "text/html; charset=utf-8" + end + + class TestSetOnResponseObj < SimpleRouteCase + describe "setting the content type of the response directly on the response object" + + get "/content_type/base/set_on_response_obj" + assert_body "Hello world!" + assert_header "Content-Type", "application/rss+xml; charset=utf-8" + end + + class TestSetOnRender < SimpleRouteCase + describe "setting the content type of the response as an option to render" + + get "/content_type/base/set_on_render" + assert_body "Hello world!" + assert_header "Content-Type", "application/rss+xml; charset=utf-8" + end + + class ImpliedController < ActionController::Base + self.view_paths = [ActionView::Template::FixturePath.new( + "content_type/implied/i_am_html_erb.html.erb" => "Hello world!", + "content_type/implied/i_am_xml_erb.xml.erb" => "<xml>Hello world!</xml>", + "content_type/implied/i_am_html_builder.html.builder" => "xml.p 'Hello'", + "content_type/implied/i_am_xml_builder.xml.builder" => "xml.awesome 'Hello'" + )] + + def i_am_html_erb() end + def i_am_xml_erb() end + def i_am_html_builder() end + def i_am_xml_builder() end + end + + class TestImpliedController < SimpleRouteCase + describe "the template's mime type is used if no content_type is specified" + + test "sets Content-Type as text/html when rendering *.html.erb" do + get "/content_type/implied/i_am_html_erb" + assert_header "Content-Type", "text/html; charset=utf-8" + end + + test "sets Content-Type as application/xml when rendering *.xml.erb" do + get "/content_type/implied/i_am_xml_erb" + assert_header "Content-Type", "application/xml; charset=utf-8" + end + + test "sets Content-Type as text/html when rendering *.html.builder" do + get "/content_type/implied/i_am_html_builder" + assert_header "Content-Type", "text/html; charset=utf-8" + end + + test "sets Content-Type as application/xml when rendering *.xml.builder" do + get "/content_type/implied/i_am_xml_builder" + assert_header "Content-Type", "application/xml; charset=utf-8" + end + + end +end + +module Charset + class BaseController < ActionController::Base + def set_on_response_obj + response.charset = "utf-16" + render :text => "Hello world!" + end + + def set_as_nil_on_response_obj + response.charset = nil + render :text => "Hello world!" + end + end + + class TestSetOnResponseObj < SimpleRouteCase + describe "setting the charset of the response directly on the response object" + + get "/charset/base/set_on_response_obj" + assert_body "Hello world!" + assert_header "Content-Type", "text/html; charset=utf-16" + end + + class TestSetAsNilOnResponseObj < SimpleRouteCase + describe "setting the charset of the response as nil directly on the response object" + + get "/charset/base/set_as_nil_on_response_obj" + assert_body "Hello world!" + assert_header "Content-Type", "text/html; charset=utf-8" + end +end
\ No newline at end of file diff --git a/actionpack/test/new_base/etag_test.rb b/actionpack/test/new_base/etag_test.rb new file mode 100644 index 0000000000..7af5febfb3 --- /dev/null +++ b/actionpack/test/new_base/etag_test.rb @@ -0,0 +1,47 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module Etags + + class BasicController < ActionController::Base + + self.view_paths = [ActionView::Template::FixturePath.new( + "etags/basic/base.html.erb" => "Hello from without_layout.html.erb", + "layouts/etags.html.erb" => "teh <%= yield %> tagz" + )] + + def without_layout + render :action => "base" + end + + def with_layout + render :action => "base", :layout => "etag" + end + + end + + class TestBasic < SimpleRouteCase + describe "Rendering without any special etag options returns an etag that is an MD5 hash of its text" + + test "an action without a layout" do + get "/etags/basic/without_layout" + body = "Hello from without_layout.html.erb" + assert_body body + assert_header "Etag", etag_for(body) + assert_status 200 + end + + test "an action with a layout" do + get "/etags/basic/with_layout" + body = "teh Hello from without_layout.html.erb tagz" + assert_body body + assert_header "Etag", etag_for(body) + assert_status 200 + end + + def etag_for(text) + %("#{Digest::MD5.hexdigest(text)}") + end + end + + +end
\ No newline at end of file diff --git a/actionpack/test/new_base/render_action_test.rb b/actionpack/test/new_base/render_action_test.rb index 2bfb374a31..348d70381b 100644 --- a/actionpack/test/new_base/render_action_test.rb +++ b/actionpack/test/new_base/render_action_test.rb @@ -3,9 +3,9 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") module RenderAction # This has no layout and it works - class BasicController < ActionController::Base2 + class BasicController < ActionController::Base - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + self.view_paths = [ActionView::Template::FixturePath.new( "render_action/basic/hello_world.html.erb" => "Hello world!" )] @@ -129,7 +129,7 @@ module RenderActionWithApplicationLayout class BasicController < ::ApplicationController # Set the view path to an application view structure with layouts - self.view_paths = self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + self.view_paths = self.view_paths = [ActionView::Template::FixturePath.new( "render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!", "layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI", "layouts/greetings.html.erb" => "Greetings <%= yield %> Bai" @@ -203,8 +203,8 @@ end module RenderActionWithControllerLayout - class BasicController < ActionController::Base2 - self.view_paths = self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + class BasicController < ActionController::Base + self.view_paths = self.view_paths = [ActionView::Template::FixturePath.new( "render_action_with_controller_layout/basic/hello_world.html.erb" => "Hello World!", "layouts/render_action_with_controller_layout/basic.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI" )] @@ -266,8 +266,8 @@ end module RenderActionWithBothLayouts - class BasicController < ActionController::Base2 - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new({ + class BasicController < ActionController::Base + self.view_paths = [ActionView::Template::FixturePath.new({ "render_action_with_both_layouts/basic/hello_world.html.erb" => "Hello World!", "layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI", "layouts/render_action_with_both_layouts/basic.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI" diff --git a/actionpack/test/new_base/render_implicit_action_test.rb b/actionpack/test/new_base/render_implicit_action_test.rb index 798505b539..58f5cec181 100644 --- a/actionpack/test/new_base/render_implicit_action_test.rb +++ b/actionpack/test/new_base/render_implicit_action_test.rb @@ -1,16 +1,28 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") -module HappyPath - - class RenderImplicitActionController < ActionController::Base2 - # No actions yet, they are implicit +module RenderImplicitAction + class SimpleController < ::ApplicationController + self.view_paths = [ActionView::Template::FixturePath.new( + "render_implicit_action/simple/hello_world.html.erb" => "Hello world!", + "render_implicit_action/simple/hyphen-ated.html.erb" => "Hello hyphen-ated!" + )] + + def hello_world() end end - class TestRendersActionImplicitly < SimpleRouteCase - - test "renders action implicitly" do - assert true - end + class TestImplicitRender < SimpleRouteCase + describe "render a simple action with new explicit call to render" + + get "/render_implicit_action/simple/hello_world" + assert_body "Hello world!" + assert_status 200 + end + class TestImplicitWithSpecialCharactersRender < SimpleRouteCase + describe "render an action with a missing method and has special characters" + + get "/render_implicit_action/simple/hyphen-ated" + assert_body "Hello hyphen-ated!" + assert_status 200 end end
\ No newline at end of file diff --git a/actionpack/test/new_base/render_layout_test.rb b/actionpack/test/new_base/render_layout_test.rb index facf67ea85..5d28926cc6 100644 --- a/actionpack/test/new_base/render_layout_test.rb +++ b/actionpack/test/new_base/render_layout_test.rb @@ -3,14 +3,23 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") module ControllerLayouts class ImplicitController < ::ApplicationController - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + self.view_paths = [ActionView::Template::FixturePath.new( "layouts/application.html.erb" => "OMG <%= yield %> KTHXBAI", - "basic.html.erb" => "Hello world!" + "layouts/override.html.erb" => "Override! <%= yield %>", + "basic.html.erb" => "Hello world!" )] def index render :template => "basic" end + + def override + render :template => "basic", :layout => "override" + end + + def builder_override + + end end class TestImplicitLayout < SimpleRouteCase @@ -23,7 +32,7 @@ module ControllerLayouts class ImplicitNameController < ::ApplicationController - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + self.view_paths = [ActionView::Template::FixturePath.new( "layouts/controller_layouts/implicit_name.html.erb" => "OMGIMPLICIT <%= yield %> KTHXBAI", "basic.html.erb" => "Hello world!" )] @@ -41,5 +50,10 @@ module ControllerLayouts assert_status 200 end - + class TestOverridingImplicitLayout < SimpleRouteCase + describe "overriding an implicit layout with render :layout option" + + get "/controller_layouts/implicit/override" + assert_body "Override! Hello world!" + end end
\ No newline at end of file diff --git a/actionpack/test/new_base/render_template_test.rb b/actionpack/test/new_base/render_template_test.rb index c6c0269b40..c09eeb1926 100644 --- a/actionpack/test/new_base/render_template_test.rb +++ b/actionpack/test/new_base/render_template_test.rb @@ -1,43 +1,35 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") -module HappyPath - - class RenderTemplateWithoutLayoutController < ActionController::Base2 +module RenderTemplate + class WithoutLayoutController < ActionController::Base - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + self.view_paths = [ActionView::Template::FixturePath.new( "test/basic.html.erb" => "Hello from basic.html.erb", - "shared.html.erb" => "Elastica" + "shared.html.erb" => "Elastica", + "locals.html.erb" => "The secret is <%= secret %>" )] - def render_hello_world + def index render :template => "test/basic" end - def render_hello_world_with_forward_slash - render :template => "/test/basic" - end - - def render_template_in_top_directory + def in_top_directory render :template => 'shared' end - def render_template_in_top_directory_with_slash + def in_top_directory_with_slash render :template => '/shared' end - end - - class TestTemplateRenderWithoutLayout < SimpleRouteCase - describe "rendering a normal template with full path without layout" - get "/happy_path/render_template_without_layout/render_hello_world" - assert_body "Hello from basic.html.erb" - assert_status 200 + def with_locals + render :template => "locals", :locals => { :secret => 'area51' } + end end - class TestTemplateRenderWithForwardSlash < SimpleRouteCase - describe "rendering a normal template with full path starting with a leading slash" + class TestWithoutLayout < SimpleRouteCase + describe "rendering a normal template with full path without layout" - get "/happy_path/render_template_without_layout/render_hello_world_with_forward_slash" + get "/render_template/without_layout" assert_body "Hello from basic.html.erb" assert_status 200 end @@ -45,7 +37,7 @@ module HappyPath class TestTemplateRenderInTopDirectory < SimpleRouteCase describe "rendering a template not in a subdirectory" - get "/happy_path/render_template_without_layout/render_template_in_top_directory" + get "/render_template/without_layout/in_top_directory" assert_body "Elastica" assert_status 200 end @@ -53,37 +45,45 @@ module HappyPath class TestTemplateRenderInTopDirectoryWithSlash < SimpleRouteCase describe "rendering a template not in a subdirectory with a leading slash" - get "/happy_path/render_template_without_layout/render_template_in_top_directory_with_slash" + get "/render_template/without_layout/in_top_directory_with_slash" assert_body "Elastica" assert_status 200 end + + class TestTemplateRenderWithLocals < SimpleRouteCase + describe "rendering a template with local variables" - class RenderTemplateWithLayoutController < ::ApplicationController + get "/render_template/without_layout/with_locals" + assert_body "The secret is area51" + assert_status 200 + end - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + class WithLayoutController < ::ApplicationController + + self.view_paths = [ActionView::Template::FixturePath.new( "test/basic.html.erb" => "Hello from basic.html.erb", "shared.html.erb" => "Elastica", "layouts/application.html.erb" => "<%= yield %>, I'm here!", "layouts/greetings.html.erb" => "<%= yield %>, I wish thee well." )] - def render_hello_world + def index render :template => "test/basic" end - def render_hello_world_with_layout + def with_layout render :template => "test/basic", :layout => true end - def render_hello_world_with_layout_false + def with_layout_false render :template => "test/basic", :layout => false end - def render_hello_world_with_layout_nil + def with_layout_nil render :template => "test/basic", :layout => nil end - def render_hello_world_with_custom_layout + def with_custom_layout render :template => "test/basic", :layout => "greetings" end end @@ -91,7 +91,7 @@ module HappyPath class TestTemplateRenderWithLayout < SimpleRouteCase describe "rendering a normal template with full path with layout" - get "/happy_path/render_template_with_layout/render_hello_world" + get "/render_template/with_layout" assert_body "Hello from basic.html.erb, I'm here!" assert_status 200 end @@ -99,7 +99,7 @@ module HappyPath class TestTemplateRenderWithLayoutTrue < SimpleRouteCase describe "rendering a normal template with full path with layout => :true" - get "/happy_path/render_template_with_layout/render_hello_world_with_layout" + get "/render_template/with_layout/with_layout" assert_body "Hello from basic.html.erb, I'm here!" assert_status 200 end @@ -107,7 +107,7 @@ module HappyPath class TestTemplateRenderWithLayoutFalse < SimpleRouteCase describe "rendering a normal template with full path with layout => :false" - get "/happy_path/render_template_with_layout/render_hello_world_with_layout_false" + get "/render_template/with_layout/with_layout_false" assert_body "Hello from basic.html.erb" assert_status 200 end @@ -115,7 +115,7 @@ module HappyPath class TestTemplateRenderWithLayoutNil < SimpleRouteCase describe "rendering a normal template with full path with layout => :nil" - get "/happy_path/render_template_with_layout/render_hello_world_with_layout_nil" + get "/render_template/with_layout/with_layout_nil" assert_body "Hello from basic.html.erb" assert_status 200 end @@ -123,11 +123,29 @@ module HappyPath class TestTemplateRenderWithCustomLayout < SimpleRouteCase describe "rendering a normal template with full path with layout => 'greetings'" - get "/happy_path/render_template_with_layout/render_hello_world_with_custom_layout" + get "/render_template/with_layout/with_custom_layout" assert_body "Hello from basic.html.erb, I wish thee well." assert_status 200 end - + module Compatibility + class WithoutLayoutController < ActionController::Base + self.view_paths = [ActionView::Template::FixturePath.new( + "test/basic.html.erb" => "Hello from basic.html.erb", + "shared.html.erb" => "Elastica" + )] + def with_forward_slash + render :template => "/test/basic" + end + end + + class TestTemplateRenderWithForwardSlash < SimpleRouteCase + describe "rendering a normal template with full path starting with a leading slash" + + get "/render_template/compatibility/without_layout/with_forward_slash" + assert_body "Hello from basic.html.erb" + assert_status 200 + end + end end
\ No newline at end of file diff --git a/actionpack/test/new_base/render_test.rb b/actionpack/test/new_base/render_test.rb new file mode 100644 index 0000000000..b1867fdcc2 --- /dev/null +++ b/actionpack/test/new_base/render_test.rb @@ -0,0 +1,88 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module Render + class BlankRenderController < ActionController::Base + self.view_paths = [ActionView::Template::FixturePath.new( + "render/blank_render/index.html.erb" => "Hello world!", + "render/blank_render/access_request.html.erb" => "The request: <%= request.method.to_s.upcase %>", + "render/blank_render/access_action_name.html.erb" => "Action Name: <%= action_name %>", + "render/blank_render/access_controller_name.html.erb" => "Controller Name: <%= controller_name %>" + )] + + def index + render + end + + def access_request + render :action => "access_request" + end + + def render_action_name + render :action => "access_action_name" + end + + private + + def secretz + render :text => "FAIL WHALE!" + end + end + + class TestBlankRender < SimpleRouteCase + describe "Render with blank" + + get "/render/blank_render" + assert_body "Hello world!" + assert_status 200 + end + + class DoubleRenderController < ActionController::Base + def index + render :text => "hello" + render :text => "world" + end + end + + class TestBasic < SimpleRouteCase + describe "Rendering more than once" + + test "raises an exception" do + assert_raises(AbstractController::DoubleRenderError) do + get "/render/double_render" + end + end + end + + class TestOnlyRenderPublicActions < SimpleRouteCase + describe "Only public methods on actual controllers are callable actions" + + test "raises an exception when a method of Object is called" do + assert_raises(AbstractController::ActionNotFound) do + get "/render/blank_render/clone" + end + end + + test "raises an exception when a private method is called" do + assert_raises(AbstractController::ActionNotFound) do + get "/render/blank_render/secretz" + end + end + end + + class TestVariousObjectsAvailableInView < SimpleRouteCase + test "The request object is accessible in the view" do + get "/render/blank_render/access_request" + assert_body "The request: GET" + end + + test "The action_name is accessible in the view" do + get "/render/blank_render/render_action_name" + assert_body "Action Name: render_action_name" + end + + test "The controller_name is accessible in the view" do + get "/render/blank_render/access_controller_name" + assert_body "Controller Name: blank_render" + end + end +end
\ No newline at end of file diff --git a/actionpack/test/new_base/render_text_test.rb b/actionpack/test/new_base/render_text_test.rb index a20ca5fb8c..39f2f7abbf 100644 --- a/actionpack/test/new_base/render_text_test.rb +++ b/actionpack/test/new_base/render_text_test.rb @@ -1,77 +1,82 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") -class ApplicationController < ActionController::Base2 +class ApplicationController < ActionController::Base end -module HappyPath - - class RenderTextWithoutLayoutsController < ActionController::Base2 - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new] +module RenderText + class SimpleController < ActionController::Base + self.view_paths = [ActionView::Template::FixturePath.new] - def render_hello_world + def index render :text => "hello david" end end - class RenderTextWithLayoutsController < ::ApplicationController - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + class TestSimpleTextRenderWithNoLayout < SimpleRouteCase + describe "Rendering text from a action with default options renders the text with the layout" + + get "/render_text/simple" + assert_body "hello david" + assert_status 200 + end + + class WithLayoutController < ::ApplicationController + self.view_paths = [ActionView::Template::FixturePath.new( "layouts/application.html.erb" => "<%= yield %>, I'm here!", - "layouts/greetings.html.erb" => "<%= yield %>, I wish thee well." + "layouts/greetings.html.erb" => "<%= yield %>, I wish thee well.", + "layouts/ivar.html.erb" => "<%= yield %>, <%= @ivar %>" )] - def render_hello_world + def index render :text => "hello david" end - def render_custom_code + def custom_code render :text => "hello world", :status => 404 end - def render_with_custom_code_as_string + def with_custom_code_as_string render :text => "hello world", :status => "404 Not Found" end - def render_text_with_nil + def with_nil render :text => nil end - def render_text_with_nil_and_status + def with_nil_and_status render :text => nil, :status => 403 end - def render_text_with_false + def with_false render :text => false end - def render_text_with_layout + def with_layout_true render :text => "hello world", :layout => true end - def render_text_with_layout_false + def with_layout_false render :text => "hello world", :layout => false end - def render_text_with_layout_nil + def with_layout_nil render :text => "hello world", :layout => nil end - def render_text_with_custom_layout + def with_custom_layout render :text => "hello world", :layout => "greetings" end - end - - class TestSimpleTextRenderWithNoLayout < SimpleRouteCase - describe "Rendering text from a action with default options renders the text with the layout" - get "/happy_path/render_text_without_layouts/render_hello_world" - assert_body "hello david" - assert_status 200 + def with_ivar_in_layout + @ivar = "hello world" + render :text => "hello world", :layout => "ivar" + end end - + class TestSimpleTextRenderWithLayout < SimpleRouteCase describe "Rendering text from a action with default options renders the text without the layout" - get "/happy_path/render_text_with_layouts/render_hello_world" + get "/render_text/with_layout" assert_body "hello david" assert_status 200 end @@ -79,7 +84,7 @@ module HappyPath class TestTextRenderWithStatus < SimpleRouteCase describe "Rendering text, while also providing a custom status code" - get "/happy_path/render_text_with_layouts/render_custom_code" + get "/render_text/with_layout/custom_code" assert_body "hello world" assert_status 404 end @@ -87,7 +92,7 @@ module HappyPath class TestTextRenderWithNil < SimpleRouteCase describe "Rendering text with nil returns a single space character" - get "/happy_path/render_text_with_layouts/render_text_with_nil" + get "/render_text/with_layout/with_nil" assert_body " " assert_status 200 end @@ -95,7 +100,7 @@ module HappyPath class TestTextRenderWithNilAndStatus < SimpleRouteCase describe "Rendering text with nil and custom status code returns a single space character with the status" - get "/happy_path/render_text_with_layouts/render_text_with_nil_and_status" + get "/render_text/with_layout/with_nil_and_status" assert_body " " assert_status 403 end @@ -103,7 +108,7 @@ module HappyPath class TestTextRenderWithFalse < SimpleRouteCase describe "Rendering text with false returns the string 'false'" - get "/happy_path/render_text_with_layouts/render_text_with_false" + get "/render_text/with_layout/with_false" assert_body "false" assert_status 200 end @@ -111,7 +116,7 @@ module HappyPath class TestTextRenderWithLayoutTrue < SimpleRouteCase describe "Rendering text with :layout => true" - get "/happy_path/render_text_with_layouts/render_text_with_layout" + get "/render_text/with_layout/with_layout_true" assert_body "hello world, I'm here!" assert_status 200 end @@ -119,7 +124,7 @@ module HappyPath class TestTextRenderWithCustomLayout < SimpleRouteCase describe "Rendering text with :layout => 'greetings'" - get "/happy_path/render_text_with_layouts/render_text_with_custom_layout" + get "/render_text/with_layout/with_custom_layout" assert_body "hello world, I wish thee well." assert_status 200 end @@ -127,7 +132,7 @@ module HappyPath class TestTextRenderWithLayoutFalse < SimpleRouteCase describe "Rendering text with :layout => false" - get "/happy_path/render_text_with_layouts/render_text_with_layout_false" + get "/render_text/with_layout/with_layout_false" assert_body "hello world" assert_status 200 end @@ -135,10 +140,10 @@ module HappyPath class TestTextRenderWithLayoutNil < SimpleRouteCase describe "Rendering text with :layout => nil" - get "/happy_path/render_text_with_layouts/render_text_with_layout_nil" + get "/render_text/with_layout/with_layout_nil" assert_body "hello world" assert_status 200 end end -ActionController::Base2.app_loaded!
\ No newline at end of file +ActionController::Base.app_loaded!
\ No newline at end of file diff --git a/actionpack/test/new_base/test_helper.rb b/actionpack/test/new_base/test_helper.rb index 89c29d9af3..78662dc989 100644 --- a/actionpack/test/new_base/test_helper.rb +++ b/actionpack/test/new_base/test_helper.rb @@ -5,8 +5,7 @@ $:.unshift(File.dirname(__FILE__) + '/../lib') require 'test/unit' require 'active_support' require 'active_support/test_case' -require 'action_controller' -require 'action_view/base' +require 'action_view' require 'fixture_template' begin @@ -24,35 +23,11 @@ require 'pp' # require 'pp' early to prevent hidden_methods from not picking up require 'rubygems' require 'rack/test' -module ActionController - class Base2 < AbstractBase - include AbstractController::Callbacks - include AbstractController::Helpers - include AbstractController::Logger - - include ActionController::HideActions - include ActionController::UrlFor - include ActionController::Renderer - include ActionController::Layouts - - def self.inherited(klass) - ::ActionController::Base2.subclasses << klass.to_s - super - end - - def self.subclasses - @subclasses ||= [] - end - - def self.app_loaded! - @subclasses.each do |subclass| - subclass.constantize._write_layout_method - end - end - - # append_view_path File.join(File.dirname(__FILE__), '..', 'fixtures') - - CORE_METHODS = self.public_instance_methods +module Rails + def self.env + x = Object.new + def x.test?() true end + x end end @@ -64,7 +39,7 @@ class Rack::TestCase < ActiveSupport::TestCase ActionController::Base.session_options[:key] = "abc" ActionController::Base.session_options[:secret] = ("*" * 30) - controllers = ActionController::Base2.subclasses.map do |k| + controllers = ActionController::Base.subclasses.map do |k| k.underscore.sub(/_controller$/, '') end @@ -91,7 +66,7 @@ class Rack::TestCase < ActiveSupport::TestCase end def assert_body(body) - assert_equal [body], last_response.body + assert_equal body, Array.wrap(last_response.body).join end def self.assert_body(body) @@ -132,7 +107,7 @@ class Rack::TestCase < ActiveSupport::TestCase end -class ::ApplicationController < ActionController::Base2 +class ::ApplicationController < ActionController::Base end class SimpleRouteCase < Rack::TestCase diff --git a/actionpack/test/template/output_buffer_test.rb b/actionpack/test/template/output_buffer_test.rb index 3faea64db3..171cfb63e1 100644 --- a/actionpack/test/template/output_buffer_test.rb +++ b/actionpack/test/template/output_buffer_test.rb @@ -10,32 +10,32 @@ class OutputBufferTest < ActionController::TestCase tests TestController def test_flush_output_buffer - pending do + pending # TODO: This tests needs to be rewritten due # The @response is not the same response object assigned # to the @controller.template # Start with the default body parts - get :index - assert_equal ['foo'], @response.body_parts - assert_nil @controller.template.output_buffer - - # Nil output buffer is skipped - @controller.template.flush_output_buffer - assert_nil @controller.template.output_buffer - assert_equal ['foo'], @response.body_parts - - # Empty output buffer is skipped - @controller.template.output_buffer = '' - @controller.template.flush_output_buffer - assert_equal '', @controller.template.output_buffer - assert_equal ['foo'], @response.body_parts - - # Flushing appends the output buffer to the body parts - @controller.template.output_buffer = 'bar' - @controller.template.flush_output_buffer - assert_equal '', @controller.template.output_buffer - assert_equal ['foo', 'bar'], @response.body_parts - end + # --- + # get :index + # assert_equal ['foo'], @response.body_parts + # assert_nil @controller.template.output_buffer + # + # # Nil output buffer is skipped + # @controller.template.flush_output_buffer + # assert_nil @controller.template.output_buffer + # assert_equal ['foo'], @response.body_parts + # + # # Empty output buffer is skipped + # @controller.template.output_buffer = '' + # @controller.template.flush_output_buffer + # assert_equal '', @controller.template.output_buffer + # assert_equal ['foo'], @response.body_parts + # + # # Flushing appends the output buffer to the body parts + # @controller.template.output_buffer = 'bar' + # @controller.template.flush_output_buffer + # assert_equal '', @controller.template.output_buffer + # assert_equal ['foo', 'bar'], @response.body_parts end end diff --git a/activesupport/lib/active_support/dependency_module.rb b/activesupport/lib/active_support/dependency_module.rb index c690b49a2b..8c202acc8f 100644 --- a/activesupport/lib/active_support/dependency_module.rb +++ b/activesupport/lib/active_support/dependency_module.rb @@ -4,14 +4,15 @@ module ActiveSupport return if base < self (@_dependencies ||= []).each { |dep| base.send(:include, dep) } super + base.extend const_get("ClassMethods") if const_defined?("ClassMethods") + base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block") end def included(base = nil, &block) - if base.nil? && block_given? + if base.nil? @_included_block = block else - base.extend const_get("ClassMethods") if const_defined?("ClassMethods") - base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block") + super end end diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index 7a48dbac04..9316d6d2b6 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -304,15 +304,6 @@ module ActiveSupport end end - # This method_missing is supplied to catch callbacks with keys and create - # the appropriate callback for future use. - def method_missing(meth, *args, &blk) - if meth.to_s =~ /_run__([\w:]+)__(\w+)__(\w+)__callbacks/ - return self.class._create_and_run_keyed_callback($1, $2.to_sym, $3.to_sym, self, &blk) - end - super - end - # An Array with a compile method class CallbackChain < Array def initialize(symbol) @@ -325,7 +316,7 @@ module ActiveSupport each do |callback| method << callback.start(key, options) end - method << "yield self if block_given?" + method << "yield self if block_given? && !halted" reverse_each do |callback| method << callback.end(key, options) end @@ -356,6 +347,7 @@ module ActiveSupport str = <<-RUBY_EVAL def _run_#{symbol}_callbacks(key = nil) if key + key = key.hash.to_s.gsub(/-/, '_') name = "_run__\#{self.class.name.split("::").last}__#{symbol}__\#{key}__callbacks" if respond_to?(name) diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb index abe7790ebf..dec6106ac1 100644 --- a/activesupport/test/new_callbacks_test.rb +++ b/activesupport/test/new_callbacks_test.rb @@ -255,6 +255,26 @@ module NewCallbacksTest end end + class HyphenatedCallbacks + include ActiveSupport::NewCallbacks + define_callbacks :save + attr_reader :stuff + + save_callback :before, :omg, :per_key => {:if => :yes} + + def yes() true end + + def omg + @stuff = "OMG" + end + + def save + _run_save_callbacks("hyphen-ated") do + @stuff + end + end + end + class AroundCallbacksTest < Test::Unit::TestCase def test_save_around around = AroundPerson.new @@ -345,7 +365,7 @@ module NewCallbacksTest save_callback :after, :third - attr_reader :history + attr_reader :history, :saved def initialize @history = [] end @@ -370,7 +390,9 @@ module NewCallbacksTest end def save - _run_save_callbacks + _run_save_callbacks do + @saved = true + end end end @@ -380,5 +402,19 @@ module NewCallbacksTest terminator.save assert_equal ["first", "second", "third", "second", "first"], terminator.history end + + def test_block_never_called_if_terminated + obj = CallbackTerminator.new + obj.save + assert !obj.saved + end end + + class HyphenatedKeyTest < Test::Unit::TestCase + def test_save + obj = HyphenatedCallbacks.new + obj.save + assert_equal obj.stuff, "OMG" + end + end end |