From cf04e621270bb2e5e9e7971d2c59e73d6797482d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 17 Apr 2008 23:30:01 -0500 Subject: Tidy up ActiveSupport::Callbacks::CallbackChain instance API. --- actionpack/lib/action_controller/dispatcher.rb | 2 +- actionpack/lib/action_controller/filters.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb index 30db7d9f73..99e1f74c0a 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatcher.rb @@ -22,7 +22,7 @@ module ActionController def to_prepare(identifier = nil, &block) @prepare_dispatch_callbacks ||= ActiveSupport::Callbacks::CallbackChain.new callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier) - @prepare_dispatch_callbacks.replace_or_append_callback(callback) + @prepare_dispatch_callbacks | callback end # If the block raises, send status code as a last-ditch response. diff --git a/actionpack/lib/action_controller/filters.rb b/actionpack/lib/action_controller/filters.rb index 73721cd1ec..8c97787741 100644 --- a/actionpack/lib/action_controller/filters.rb +++ b/actionpack/lib/action_controller/filters.rb @@ -265,7 +265,7 @@ module ActionController #:nodoc: def skip_filter_in_chain(*filters, &test) filters, conditions = extract_options(filters) filters.each do |filter| - if callback = find_callback(filter) then delete(callback) end + if callback = find(filter) then delete(callback) end end if conditions.empty? update_filter_in_chain(filters, :skip => conditions, &test) end @@ -302,7 +302,7 @@ module ActionController #:nodoc: def find_or_create_filter(filter, filter_type, options = {}) update_filter_in_chain([filter], options) - if found_filter = find_callback(filter) { |f| f.type == filter_type } + if found_filter = find(filter) { |f| f.type == filter_type } found_filter else filter_kind = case @@ -326,7 +326,7 @@ module ActionController #:nodoc: end def update_filter_in_chain(filters, options, &test) - filters.map! { |f| block_given? ? find_callback(f, &test) : find_callback(f) } + filters.map! { |f| block_given? ? find(f, &test) : find(f) } filters.compact! map! do |filter| -- cgit v1.2.3 From 986aec5dbbdfb578945e706cbe6a54c4f06640e5 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 17 Apr 2008 23:49:03 +0100 Subject: Refactor Dispatcher callbacks to remove unnecessary Dependencies checks in production environment. --- actionpack/lib/action_controller/dispatcher.rb | 73 ++++++++++++-------------- actionpack/test/controller/dispatcher_test.rb | 55 ++++++------------- 2 files changed, 50 insertions(+), 78 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb index 99e1f74c0a..6e1e7a261f 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatcher.rb @@ -5,6 +5,30 @@ module ActionController @@guard = Mutex.new class << self + def define_dispatcher_callbacks(cache_classes) + unless cache_classes + # Development mode callbacks + before_dispatch :reload_application + after_dispatch :cleanup_application + end + + # Common callbacks + to_prepare :load_application_controller do + begin + require_dependency 'application' unless defined?(::ApplicationController) + rescue LoadError => error + raise unless error.message =~ /application\.rb/ + end + end + + if defined?(ActiveRecord) + before_dispatch { ActiveRecord::Base.verify_active_connections! } + to_prepare(:activerecord_instantiate_observers) { ActiveRecord::Base.instantiate_observers } + end + + after_dispatch :flush_logger if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:flush) + end + # Backward-compatible class method takes CGI-specific args. Deprecated # in favor of Dispatcher.new(output, request, response).dispatch. def dispatch(cgi = nil, session_options = CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) @@ -69,23 +93,9 @@ module ActionController cattr_accessor :error_file_path self.error_file_path = Rails.public_path if defined?(Rails.public_path) - cattr_accessor :unprepared - self.unprepared = true - include ActiveSupport::Callbacks define_callbacks :prepare_dispatch, :before_dispatch, :after_dispatch - before_dispatch :reload_application - before_dispatch :prepare_application - after_dispatch :flush_logger - after_dispatch :cleanup_application - - if defined? ActiveRecord - to_prepare :activerecord_instantiate_observers do - ActiveRecord::Base.instantiate_observers - end - end - def initialize(output, request = nil, response = nil) @output, @request, @response = output, request, response end @@ -114,40 +124,23 @@ module ActionController end def reload_application - if Dependencies.load? - Routing::Routes.reload - self.unprepared = true - end - end + # Run prepare callbacks before every request in development mode + run_callbacks :prepare_dispatch - def prepare_application(force = false) - begin - require_dependency 'application' unless defined?(::ApplicationController) - rescue LoadError => error - raise unless error.message =~ /application\.rb/ - end - - ActiveRecord::Base.verify_active_connections! if defined?(ActiveRecord) - - if unprepared || force - run_callbacks :prepare_dispatch - ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading - self.unprepared = false - end + Routing::Routes.reload + ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading end # Cleanup the application by clearing out loaded classes so they can # be reloaded on the next request without restarting the server. - def cleanup_application(force = false) - if Dependencies.load? || force - ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord) - Dependencies.clear - ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) - end + def cleanup_application + ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord) + Dependencies.clear + ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) end def flush_logger - RAILS_DEFAULT_LOGGER.flush if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:flush) + RAILS_DEFAULT_LOGGER.flush end protected diff --git a/actionpack/test/controller/dispatcher_test.rb b/actionpack/test/controller/dispatcher_test.rb index c4f49f1e16..eea0813ed5 100644 --- a/actionpack/test/controller/dispatcher_test.rb +++ b/actionpack/test/controller/dispatcher_test.rb @@ -11,7 +11,13 @@ class DispatcherTest < Test::Unit::TestCase @output = StringIO.new ENV['REQUEST_METHOD'] = 'GET' + # Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new) + Dispatcher.instance_variable_set("@before_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new) + Dispatcher.instance_variable_set("@after_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new) + + Dispatcher.stubs(:require_dependency) + @dispatcher = Dispatcher.new(@output) end @@ -20,17 +26,13 @@ class DispatcherTest < Test::Unit::TestCase end def test_clears_dependencies_after_dispatch_if_in_loading_mode - Dependencies.stubs(:load?).returns(true) - ActionController::Routing::Routes.expects(:reload).once Dependencies.expects(:clear).once - dispatch + dispatch(@output, false) end def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode - Dependencies.stubs(:load?).returns(false) - ActionController::Routing::Routes.expects(:reload).never Dependencies.expects(:clear).never @@ -51,40 +53,25 @@ class DispatcherTest < Test::Unit::TestCase assert_equal "Status: 400 Bad Request\r\nContent-Type: text/html\r\n\r\n

400 Bad Request

", @output.string end - def test_reload_application_sets_unprepared_if_loading_dependencies - Dependencies.stubs(:load?).returns(false) - ActionController::Routing::Routes.expects(:reload).never - @dispatcher.unprepared = false - @dispatcher.send!(:reload_application) - assert !@dispatcher.unprepared - - Dependencies.stubs(:load?).returns(true) - ActionController::Routing::Routes.expects(:reload).once - @dispatcher.send!(:reload_application) - assert @dispatcher.unprepared - end - - def test_prepare_application_runs_callbacks_if_unprepared + def test_prepare_callbacks a = b = c = nil Dispatcher.to_prepare { |*args| a = b = c = 1 } Dispatcher.to_prepare { |*args| b = c = 2 } Dispatcher.to_prepare { |*args| c = 3 } - # Skip the callbacks when already prepared. - @dispatcher.unprepared = false - @dispatcher.send! :prepare_application + # Ensure to_prepare callbacks are not run when defined assert_nil a || b || c - # Perform the callbacks when unprepared. - @dispatcher.unprepared = true - @dispatcher.send! :prepare_application + # Run callbacks + @dispatcher.send :run_callbacks, :prepare_dispatch + assert_equal 1, a assert_equal 2, b assert_equal 3, c - # But when not :load, make sure they are only run once + # Make sure they are only run once a = b = c = nil - @dispatcher.send! :prepare_application + @dispatcher.send :dispatch assert_nil a || b || c end @@ -93,28 +80,20 @@ class DispatcherTest < Test::Unit::TestCase Dispatcher.to_prepare(:unique_id) { |*args| a = b = 1 } Dispatcher.to_prepare(:unique_id) { |*args| a = 2 } - @dispatcher.unprepared = true - @dispatcher.send! :prepare_application + @dispatcher.send :run_callbacks, :prepare_dispatch assert_equal 2, a assert_equal nil, b end - def test_to_prepare_only_runs_once_if_not_loading_dependencies - Dependencies.stubs(:load?).returns(false) - called = 0 - Dispatcher.to_prepare(:unprepared_test) { |*args| called += 1 } - 2.times { dispatch } - assert_equal 1, called - end - private - def dispatch(output = @output) + def dispatch(output = @output, cache_classes = true) controller = mock controller.stubs(:process).returns(controller) controller.stubs(:out).with(output).returns('response') ActionController::Routing::Routes.stubs(:recognize).returns(controller) + Dispatcher.define_dispatcher_callbacks(cache_classes) Dispatcher.dispatch(nil, {}, output) end -- cgit v1.2.3 From 534c6b2444970d59aea654aa3c6aeb41c206d14d Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 19 Apr 2008 16:16:32 +0100 Subject: Introduce ActionView::InlineTemplate class --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 2 +- actionpack/lib/action_view.rb | 1 + actionpack/lib/action_view/base.rb | 2 +- actionpack/lib/action_view/inline_template.rb | 20 ++++++++++++++++++++ actionpack/lib/action_view/template.rb | 22 +++++++++------------- actionpack/test/controller/custom_handler_test.rb | 6 +++--- 7 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 actionpack/lib/action_view/inline_template.rb (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 1e53a39667..e8cef9eb71 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Introduce ActionView::InlineTemplate class. [Pratik] + * Automatically parse posted JSON content for Mime::JSON requests. [rick] POST /posts diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 63ad4d042a..f620c442fd 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -870,7 +870,7 @@ module ActionController #:nodoc: elsif inline = options[:inline] add_variables_to_assigns - tmpl = ActionView::Template.new(@template, options[:inline], false, options[:locals], true, options[:type]) + tmpl = ActionView::InlineTemplate.new(@template, options[:inline], options[:locals], options[:type]) render_for_text(@template.render_template(tmpl), options[:status]) elsif action_name = options[:action] diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb index e20812d308..f9de5c1307 100644 --- a/actionpack/lib/action_view.rb +++ b/actionpack/lib/action_view.rb @@ -30,6 +30,7 @@ require 'action_view/template_handlers/rjs' require 'action_view/template_finder' require 'action_view/template' require 'action_view/partial_template' +require 'action_view/inline_template' require 'action_view/base' require 'action_view/partials' diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index e83c8b6bd3..f001b81eca 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -279,7 +279,7 @@ If you are rendering a subtemplate, you must now use controller-like partial syn elsif options[:partial] render_partial(options[:partial], ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals]) elsif options[:inline] - template = Template.new(self, options[:inline], false, options[:locals], true, options[:type]) + template = InlineTemplate.new(self, options[:inline], options[:locals], options[:type]) render_template(template) end end diff --git a/actionpack/lib/action_view/inline_template.rb b/actionpack/lib/action_view/inline_template.rb new file mode 100644 index 0000000000..87c012d181 --- /dev/null +++ b/actionpack/lib/action_view/inline_template.rb @@ -0,0 +1,20 @@ +module ActionView #:nodoc: + class InlineTemplate < Template #:nodoc: + + def initialize(view, source, locals = {}, type = nil) + @view = view + @finder = @view.finder + + @source = source + @extension = type + @locals = locals || {} + + @handler = self.class.handler_class_for_extension(@extension).new(@view) + end + + def method_key + @source + end + + end +end diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index 0cd6784fd8..985aa090e5 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -2,22 +2,18 @@ module ActionView #:nodoc: class Template #:nodoc: attr_accessor :locals - attr_reader :handler, :path, :source, :extension, :filename, :path_without_extension, :method + attr_reader :handler, :path, :extension, :filename, :path_without_extension, :method - def initialize(view, path_or_source, use_full_path, locals = {}, inline = false, inline_type = nil) + def initialize(view, path, use_full_path, locals = {}) @view = view @finder = @view.finder - unless inline - # Clear the forward slash at the beginning if exists - @path = use_full_path ? path_or_source.sub(/^\//, '') : path_or_source - @view.first_render ||= @path - @source = nil # Don't read the source until we know that it is required - set_extension_and_file_name(use_full_path) - else - @source = path_or_source - @extension = inline_type - end + # Clear the forward slash at the beginning if exists + @path = use_full_path ? path.sub(/^\//, '') : path + @view.first_render ||= @path + @source = nil # Don't read the source until we know that it is required + set_extension_and_file_name(use_full_path) + @locals = locals || {} @handler = self.class.handler_class_for_extension(@extension).new(@view) end @@ -32,7 +28,7 @@ module ActionView #:nodoc: end def method_key - @method_key ||= (@filename || @source) + @filename end def base_path_for_exception diff --git a/actionpack/test/controller/custom_handler_test.rb b/actionpack/test/controller/custom_handler_test.rb index cf1e2361bd..ac484ae17e 100644 --- a/actionpack/test/controller/custom_handler_test.rb +++ b/actionpack/test/controller/custom_handler_test.rb @@ -20,7 +20,7 @@ class CustomHandlerTest < Test::Unit::TestCase end def test_custom_render - template = ActionView::Template.new(@view, "hello <%= one %>", false, { :one => "two" }, true, "foo") + template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo") result = @view.render_template(template) assert_equal( @@ -29,7 +29,7 @@ class CustomHandlerTest < Test::Unit::TestCase end def test_custom_render2 - template = ActionView::Template.new(@view, "hello <%= one %>", false, { :one => "two" }, true, "foo2") + template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo2") result = @view.render_template(template) assert_equal( [ "hello <%= one %>", { :one => "two" }, @view ], @@ -38,7 +38,7 @@ class CustomHandlerTest < Test::Unit::TestCase def test_unhandled_extension # uses the ERb handler by default if the extension isn't recognized - template = ActionView::Template.new(@view, "hello <%= one %>", false, { :one => "two" }, true, "bar") + template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "bar") result = @view.render_template(template) assert_equal "hello two", result end -- cgit v1.2.3 From ef4c65088fb907fc819e6b5d83d284c38cdaabfc Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 19 Apr 2008 18:52:14 +0100 Subject: Move missing template logic to ActionView --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 13 ------------- actionpack/lib/action_controller/layout.rb | 4 +--- actionpack/lib/action_controller/rescue.rb | 2 +- actionpack/lib/action_view/base.rb | 3 +++ actionpack/lib/action_view/template.rb | 16 ++++++++++------ actionpack/test/controller/cookie_test.rb | 2 +- actionpack/test/controller/flash_test.rb | 2 +- actionpack/test/controller/layout_test.rb | 2 +- actionpack/test/controller/mime_responds_test.rb | 2 +- actionpack/test/controller/new_render_test.rb | 4 ++-- actionpack/test/controller/rescue_test.rb | 2 +- actionpack/test/template/template_object_test.rb | 2 +- 13 files changed, 25 insertions(+), 31 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index e8cef9eb71..4694c0e996 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Move missing template logic to ActionView. [Pratik] + * Introduce ActionView::InlineTemplate class. [Pratik] * Automatically parse posted JSON content for Mime::JSON requests. [rick] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index f620c442fd..32df035871 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -16,9 +16,6 @@ module ActionController #:nodoc: class SessionRestoreError < ActionControllerError #:nodoc: end - class MissingTemplate < ActionControllerError #:nodoc: - end - class RenderError < ActionControllerError #:nodoc: end @@ -1105,7 +1102,6 @@ module ActionController #:nodoc: private def render_for_file(template_path, status = nil, use_full_path = false, locals = {}) #:nodoc: add_variables_to_assigns - assert_existence_of_template_file(template_path) if use_full_path logger.info("Rendering #{template_path}" + (status ? " (#{status})" : '')) if logger render_for_text(@template.render_file(template_path, use_full_path, locals), status) end @@ -1267,15 +1263,6 @@ module ActionController #:nodoc: @@exempt_from_layout.any? { |ext| name_with_extension =~ ext } end - def assert_existence_of_template_file(template_name) - unless template_exists?(template_name) || ignore_missing_templates - full_template_path = template_name.include?('.') ? template_name : "#{template_name}.#{@template.template_format}.erb" - display_paths = view_paths.join(':') - template_type = (template_name =~ /layouts/i) ? 'layout' : 'template' - raise(MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}") - end - end - def default_template_name(action_name = self.action_name) if action_name action_name = action_name.to_s diff --git a/actionpack/lib/action_controller/layout.rb b/actionpack/lib/action_controller/layout.rb index 5484add3b5..b5b59f2d7c 100644 --- a/actionpack/lib/action_controller/layout.rb +++ b/actionpack/lib/action_controller/layout.rb @@ -244,9 +244,7 @@ module ActionController #:nodoc: def render_with_a_layout(options = nil, extra_options = {}, &block) #:nodoc: template_with_options = options.is_a?(Hash) - if apply_layout?(template_with_options, options) && (layout = pick_layout(template_with_options, options)) - assert_existence_of_template_file(layout) - + if (layout = pick_layout(template_with_options, options)) && apply_layout?(template_with_options, options) options = options.merge :layout => false if template_with_options logger.info("Rendering template within #{layout}") if logger diff --git a/actionpack/lib/action_controller/rescue.rb b/actionpack/lib/action_controller/rescue.rb index f5ad04532b..d4d561bdb7 100644 --- a/actionpack/lib/action_controller/rescue.rb +++ b/actionpack/lib/action_controller/rescue.rb @@ -26,7 +26,7 @@ module ActionController #:nodoc: DEFAULT_RESCUE_TEMPLATE = 'diagnostics' DEFAULT_RESCUE_TEMPLATES = { - 'ActionController::MissingTemplate' => 'missing_template', + 'ActionView::MissingTemplate' => 'missing_template', 'ActionController::RoutingError' => 'routing_error', 'ActionController::UnknownAction' => 'unknown_action', 'ActionView::TemplateError' => 'template_error' diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index f001b81eca..e57c447682 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -1,6 +1,9 @@ module ActionView #:nodoc: class ActionViewError < StandardError #:nodoc: end + + class MissingTemplate < ActionViewError #:nodoc: + end # Action View templates can be written in three ways. If the template file has a +.erb+ (or +.rhtml+) extension then it uses a mixture of ERb # (included in Ruby) and HTML. If the template file has a +.builder+ (or +.rxml+) extension then Jim Weirich's Builder::XmlMarkup library is used. diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index 985aa090e5..bc3d8d5e52 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -54,9 +54,8 @@ module ActionView #:nodoc: @filename = @finder.pick_template(@path_without_extension, @extension) else @extension = @finder.pick_template_extension(@path).to_s - unless @extension - raise ActionViewError, "No template found for #{@path} in #{@finder.view_paths.inspect}" - end + raise_missing_template_exception unless @extension + @filename = @finder.pick_template(@path, @extension) @extension = @extension.gsub(/^.+\./, '') # strip off any formats end @@ -64,9 +63,14 @@ module ActionView #:nodoc: @filename = @path end - if @filename.blank? - raise ActionViewError, "Couldn't find template file for #{@path} in #{@finder.view_paths.inspect}" - end + raise_missing_template_exception if @filename.blank? + end + + def raise_missing_template_exception + full_template_path = @path.include?('.') ? @path : "#{@path}.#{@view.template_format}.erb" + display_paths = @finder.view_paths.join(':') + template_type = (@path =~ /layouts/i) ? 'layout' : 'template' + raise(MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}") end # Template Handlers diff --git a/actionpack/test/controller/cookie_test.rb b/actionpack/test/controller/cookie_test.rb index 0483fe918a..42f3bd26a4 100644 --- a/actionpack/test/controller/cookie_test.rb +++ b/actionpack/test/controller/cookie_test.rb @@ -37,7 +37,7 @@ class CookieTest < Test::Unit::TestCase end def rescue_action(e) - raise unless ActionController::MissingTemplate # No templates here, and we don't care about the output + raise unless ActionView::MissingTemplate # No templates here, and we don't care about the output end end diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index f672f2f427..e562531bf3 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -52,7 +52,7 @@ class FlashTest < Test::Unit::TestCase end def rescue_action(e) - raise unless ActionController::MissingTemplate === e + raise unless ActionView::MissingTemplate === e end # methods for test_sweep_after_halted_filter_chain diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index 145543a357..3dc311b78a 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -216,7 +216,7 @@ class LayoutExceptionRaised < Test::Unit::TestCase @controller = SetsNonExistentLayoutFile.new get :hello @response.template.class.module_eval { attr_accessor :exception } - assert_equal ActionController::MissingTemplate, @response.template.exception.class + assert_equal ActionView::MissingTemplate, @response.template.exception.class end end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index c34643ddd5..c617cb2e84 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -468,7 +468,7 @@ class MimeControllerTest < Test::Unit::TestCase assert_equal '
Hello future from Firefox!
', @response.body @request.env["HTTP_ACCEPT"] = "text/iphone" - assert_raises(ActionController::MissingTemplate) { get :iphone_with_html_response_type_without_layout } + assert_raises(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout } end end diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 342e2e7f87..80cf09e5f3 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -652,7 +652,7 @@ EOS end def test_bad_render_to_string_still_throws_exception - assert_raises(ActionController::MissingTemplate) { get :render_to_string_with_exception } + assert_raises(ActionView::MissingTemplate) { get :render_to_string_with_exception } end def test_render_to_string_that_throws_caught_exception_doesnt_break_assigns @@ -787,7 +787,7 @@ EOS end def test_render_missing_partial_template - assert_raises(ActionView::ActionViewError) do + assert_raises(ActionView::MissingTemplate) do get :missing_partial end end diff --git a/actionpack/test/controller/rescue_test.rb b/actionpack/test/controller/rescue_test.rb index 011992474f..27fcc5e04c 100644 --- a/actionpack/test/controller/rescue_test.rb +++ b/actionpack/test/controller/rescue_test.rb @@ -279,7 +279,7 @@ class RescueTest < Test::Unit::TestCase assert_equal ActionController::Rescue::DEFAULT_RESCUE_TEMPLATE, templates.default assert_equal ActionController::Rescue::DEFAULT_RESCUE_TEMPLATE, templates[Exception.new] - assert_equal 'missing_template', templates[ActionController::MissingTemplate.name] + assert_equal 'missing_template', templates[ActionView::MissingTemplate.name] assert_equal 'routing_error', templates[ActionController::RoutingError.name] assert_equal 'unknown_action', templates[ActionController::UnknownAction.name] assert_equal 'template_error', templates[ActionView::TemplateError.name] diff --git a/actionpack/test/template/template_object_test.rb b/actionpack/test/template/template_object_test.rb index b3a33938cf..7adcde421f 100644 --- a/actionpack/test/template/template_object_test.rb +++ b/actionpack/test/template/template_object_test.rb @@ -82,7 +82,7 @@ class TemplateObjectTest < Test::Unit::TestCase def test_xml @view.template_format = :xml - assert_raise ActionView::ActionViewError do + assert_raise ActionView::MissingTemplate do ActionView::PartialTemplate.new(@view, @path, nil) end end -- cgit v1.2.3 From 17d4164a16e5fe7b252375211424a2999a331291 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 19 Apr 2008 13:06:57 -0500 Subject: Introduce ActionView::TestCase for testing view helpers. --- actionpack/lib/action_view.rb | 6 +- actionpack/lib/action_view/base.rb | 10 ++-- actionpack/lib/action_view/test_case.rb | 64 ++++++++++++++++++++++ actionpack/test/abstract_unit.rb | 1 + .../test/template/active_record_helper_test.rb | 9 +-- actionpack/test/template/asset_tag_helper_test.rb | 12 ++-- actionpack/test/template/benchmark_helper_test.rb | 4 +- actionpack/test/template/date_helper_test.rb | 5 +- actionpack/test/template/form_helper_test.rb | 11 +--- .../test/template/form_options_helper_test.rb | 5 +- actionpack/test/template/form_tag_helper_test.rb | 8 +-- actionpack/test/template/javascript_helper_test.rb | 10 +--- actionpack/test/template/number_helper_test.rb | 4 +- actionpack/test/template/prototype_helper_test.rb | 56 +++++++------------ actionpack/test/template/record_tag_helper_test.rb | 11 +--- actionpack/test/template/sanitize_helper_test.rb | 5 +- .../test/template/scriptaculous_helper_test.rb | 14 +---- actionpack/test/template/tag_helper_test.rb | 7 +-- actionpack/test/template/test_test.rb | 56 +++++++++++++++++++ actionpack/test/template/text_helper_test.rb | 5 +- actionpack/test/template/url_helper_test.rb | 18 +++--- 21 files changed, 190 insertions(+), 131 deletions(-) create mode 100644 actionpack/lib/action_view/test_case.rb create mode 100644 actionpack/test/template/test_test.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb index f9de5c1307..609334d52d 100644 --- a/actionpack/lib/action_view.rb +++ b/actionpack/lib/action_view.rb @@ -38,6 +38,8 @@ require 'action_view/template_error' ActionView::Base.class_eval do include ActionView::Partials -end -ActionView::Base.load_helpers + ActionView::Base.helper_modules.each do |helper_module| + include helper_module + end +end diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index e57c447682..12dd7d2bc9 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -205,15 +205,17 @@ module ActionView #:nodoc: class ObjectWrapper < Struct.new(:value) #:nodoc: end - def self.load_helpers #:nodoc: - Dir.entries("#{File.dirname(__FILE__)}/helpers").sort.each do |file| + def self.helper_modules #:nodoc: + helpers = [] + Dir.entries(File.expand_path("#{File.dirname(__FILE__)}/helpers")).sort.each do |file| next unless file =~ /^([a-z][a-z_]*_helper).rb$/ require "action_view/helpers/#{$1}" helper_module_name = $1.camelize if Helpers.const_defined?(helper_module_name) - include Helpers.const_get(helper_module_name) + helpers << Helpers.const_get(helper_module_name) end end + return helpers end def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc: @@ -323,7 +325,7 @@ If you are rendering a subtemplate, you must now use controller-like partial syn end end - private + private def wrap_content_for_layout(content) original_content_for_layout = @content_for_layout @content_for_layout = content diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb new file mode 100644 index 0000000000..b2e6589d81 --- /dev/null +++ b/actionpack/lib/action_view/test_case.rb @@ -0,0 +1,64 @@ +require 'active_support/test_case' + +module ActionView + class NonInferrableHelperError < ActionViewError + def initialize(name) + super "Unable to determine the helper to test from #{name}. " + + "You'll need to specify it using tests YourHelper in your " + + "test case definition" + end + end + + class TestCase < ActiveSupport::TestCase + class_inheritable_accessor :helper_class + @@helper_class = nil + + class << self + def tests(helper_class) + self.helper_class = helper_class + end + + def helper_class + if current_helper_class = read_inheritable_attribute(:helper_class) + current_helper_class + else + self.helper_class = determine_default_helper_class(name) + end + end + + def determine_default_helper_class(name) + name.sub(/Test$/, '').constantize + rescue NameError + raise NonInferrableHelperError.new(name) + end + end + + ActionView::Base.helper_modules.each do |helper_module| + include helper_module + end + include ActionController::PolymorphicRoutes + include ActionController::RecordIdentifier + + setup :setup_with_helper_class + + def setup_with_helper_class + self.class.send(:include, helper_class) + end + + class TestController < ActionController::Base + attr_accessor :request, :response + + def initialize + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + end + + private + def method_missing(selector, *args) + controller = TestController.new + return controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector) + super + end + end +end diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 700bc1f5e3..d90f299b8a 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -8,6 +8,7 @@ require 'test/unit' require 'action_controller' require 'action_controller/cgi_ext' require 'action_controller/test_process' +require 'action_view/test_case' begin require 'ruby-debug' diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb index 31fe7bbc29..dfc30e651a 100644 --- a/actionpack/test/template/active_record_helper_test.rb +++ b/actionpack/test/template/active_record_helper_test.rb @@ -1,12 +1,7 @@ require 'abstract_unit' -class ActiveRecordHelperTest < Test::Unit::TestCase - include ActionView::Helpers::FormHelper - include ActionView::Helpers::ActiveRecordHelper - include ActionView::Helpers::TextHelper - include ActionView::Helpers::TagHelper - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::FormTagHelper +class ActiveRecordHelperTest < ActionView::TestCase + tests ActionView::Helpers::ActiveRecordHelper silence_warnings do Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on) diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index ffb8856a59..4a8117a88a 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -1,9 +1,7 @@ require 'abstract_unit' -class AssetTagHelperTest < Test::Unit::TestCase - include ActionView::Helpers::TagHelper - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::AssetTagHelper +class AssetTagHelperTest < ActionView::TestCase + tests ActionView::Helpers::AssetTagHelper def setup silence_warnings do @@ -445,10 +443,8 @@ class AssetTagHelperTest < Test::Unit::TestCase end end -class AssetTagHelperNonVhostTest < Test::Unit::TestCase - include ActionView::Helpers::TagHelper - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::AssetTagHelper +class AssetTagHelperNonVhostTest < ActionView::TestCase + tests ActionView::Helpers::AssetTagHelper def setup @controller = Class.new do diff --git a/actionpack/test/template/benchmark_helper_test.rb b/actionpack/test/template/benchmark_helper_test.rb index 76c0780482..d95a3dee26 100644 --- a/actionpack/test/template/benchmark_helper_test.rb +++ b/actionpack/test/template/benchmark_helper_test.rb @@ -1,8 +1,8 @@ require 'abstract_unit' require 'action_view/helpers/benchmark_helper' -class BenchmarkHelperTest < Test::Unit::TestCase - include ActionView::Helpers::BenchmarkHelper +class BenchmarkHelperTest < ActionView::TestCase + tests ActionView::Helpers::BenchmarkHelper class MockLogger attr_reader :logged diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 25b1f9f002..9bd433e76b 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1,8 +1,7 @@ require 'abstract_unit' -class DateHelperTest < Test::Unit::TestCase - include ActionView::Helpers::DateHelper - include ActionView::Helpers::FormHelper +class DateHelperTest < ActionView::TestCase + tests ActionView::Helpers::DateHelper silence_warnings do Post = Struct.new("Post", :id, :written_on, :updated_at) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 766e88375b..c48d5dfd2d 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -30,15 +30,8 @@ end class Comment::Nested < Comment; end -class FormHelperTest < Test::Unit::TestCase - include ActionView::Helpers::FormHelper - include ActionView::Helpers::FormTagHelper - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::TagHelper - include ActionView::Helpers::TextHelper - include ActionView::Helpers::ActiveRecordHelper - include ActionView::Helpers::RecordIdentificationHelper - include ActionController::PolymorphicRoutes +class FormHelperTest < ActionView::TestCase + tests ActionView::Helpers::FormHelper def setup @post = Post.new diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index f3ecc18233..48a26deea9 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -22,9 +22,8 @@ end ActionView::Helpers::FormOptionsHelper::TimeZone = MockTimeZone -class FormOptionsHelperTest < Test::Unit::TestCase - include ActionView::Helpers::FormHelper - include ActionView::Helpers::FormOptionsHelper +class FormOptionsHelperTest < ActionView::TestCase + tests ActionView::Helpers::FormOptionsHelper silence_warnings do Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin) diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 7caa85802a..73a8bd4d87 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -1,11 +1,7 @@ require 'abstract_unit' -class FormTagHelperTest < Test::Unit::TestCase - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::TagHelper - include ActionView::Helpers::FormTagHelper - include ActionView::Helpers::TextHelper - include ActionView::Helpers::CaptureHelper +class FormTagHelperTest < ActionView::TestCase + tests ActionView::Helpers::FormTagHelper def setup @controller = Class.new do diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index 581ca58f89..f18adb990c 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -1,13 +1,7 @@ require 'abstract_unit' -class JavaScriptHelperTest < Test::Unit::TestCase - include ActionView::Helpers::JavaScriptHelper - - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::TagHelper - include ActionView::Helpers::TextHelper - include ActionView::Helpers::FormHelper - include ActionView::Helpers::CaptureHelper +class JavaScriptHelperTest < ActionView::TestCase + tests ActionView::Helpers::JavaScriptHelper def test_define_javascript_functions # check if prototype.js is included first diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index 7065ca7a84..4a8d09b544 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -1,7 +1,7 @@ require 'abstract_unit' -class NumberHelperTest < Test::Unit::TestCase - include ActionView::Helpers::NumberHelper +class NumberHelperTest < ActionView::TestCase + tests ActionView::Helpers::NumberHelper def test_number_to_phone assert_equal("800-555-1212", number_to_phone(8005551212)) diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 28e58b0a08..a84d4e72af 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -24,24 +24,11 @@ end class Author::Nested < Author; end -module BaseTest - def self.included(base) - base.send :attr_accessor, :template_format - end +class PrototypeHelperBaseTest < ActionView::TestCase + tests ActionView::Helpers::PrototypeHelper + + attr_accessor :template_format - include ActionView::Helpers::JavaScriptHelper - include ActionView::Helpers::PrototypeHelper - include ActionView::Helpers::ScriptaculousHelper - - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::TagHelper - include ActionView::Helpers::TextHelper - include ActionView::Helpers::FormTagHelper - include ActionView::Helpers::FormHelper - include ActionView::Helpers::CaptureHelper - include ActionView::Helpers::RecordIdentificationHelper - include ActionController::PolymorphicRoutes - def setup @template = nil @controller = Class.new do @@ -59,25 +46,22 @@ module BaseTest end.new end -protected - - def request_forgery_protection_token - nil - end - - def protect_against_forgery? - false - end - - def create_generator - block = Proc.new { |*args| yield *args if block_given? } - JavaScriptGenerator.new self, &block - end + protected + def request_forgery_protection_token + nil + end + + def protect_against_forgery? + false + end + + def create_generator + block = Proc.new { |*args| yield *args if block_given? } + JavaScriptGenerator.new self, &block + end end -class PrototypeHelperTest < Test::Unit::TestCase - include BaseTest - +class PrototypeHelperTest < PrototypeHelperBaseTest def setup @record = @author = Author.new @article = Article.new @@ -294,9 +278,7 @@ class PrototypeHelperTest < Test::Unit::TestCase end end -class JavaScriptGeneratorTest < Test::Unit::TestCase - include BaseTest - +class JavaScriptGeneratorTest < PrototypeHelperBaseTest def setup super @generator = create_generator diff --git a/actionpack/test/template/record_tag_helper_test.rb b/actionpack/test/template/record_tag_helper_test.rb index bb5440be20..0afbb54f57 100644 --- a/actionpack/test/template/record_tag_helper_test.rb +++ b/actionpack/test/template/record_tag_helper_test.rb @@ -9,14 +9,9 @@ class Post end end -class RecordTagHelperTest < Test::Unit::TestCase - include ActionView::Helpers::RecordTagHelper - include ActionView::Helpers::CaptureHelper - include ActionView::Helpers::RecordIdentificationHelper - include ActionView::Helpers::TagHelper - include ActionView::Helpers::TextHelper - include ActionView::Helpers::UrlHelper - +class RecordTagHelperTest < ActionView::TestCase + tests ActionView::Helpers::RecordTagHelper + def setup @post = Post.new end diff --git a/actionpack/test/template/sanitize_helper_test.rb b/actionpack/test/template/sanitize_helper_test.rb index a840c8b4cb..e5427d9dc1 100644 --- a/actionpack/test/template/sanitize_helper_test.rb +++ b/actionpack/test/template/sanitize_helper_test.rb @@ -3,9 +3,8 @@ require 'testing_sandbox' # The exhaustive tests are in test/controller/html/sanitizer_test.rb. # This tests the that the helpers hook up correctly to the sanitizer classes. -class SanitizeHelperTest < Test::Unit::TestCase - include ActionView::Helpers::SanitizeHelper - include ActionView::Helpers::TagHelper +class SanitizeHelperTest < ActionView::TestCase + tests ActionView::Helpers::SanitizeHelper include TestingSandbox def test_strip_links diff --git a/actionpack/test/template/scriptaculous_helper_test.rb b/actionpack/test/template/scriptaculous_helper_test.rb index 91856ff980..690a7751b5 100644 --- a/actionpack/test/template/scriptaculous_helper_test.rb +++ b/actionpack/test/template/scriptaculous_helper_test.rb @@ -1,16 +1,8 @@ require 'abstract_unit' -class ScriptaculousHelperTest < Test::Unit::TestCase - include ActionView::Helpers::JavaScriptHelper - include ActionView::Helpers::PrototypeHelper - include ActionView::Helpers::ScriptaculousHelper - - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::TagHelper - include ActionView::Helpers::TextHelper - include ActionView::Helpers::FormHelper - include ActionView::Helpers::CaptureHelper - +class ScriptaculousHelperTest < ActionView::TestCase + tests ActionView::Helpers::ScriptaculousHelper + def setup @controller = Class.new do def url_for(options) diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb index 4b73289060..4da6116095 100644 --- a/actionpack/test/template/tag_helper_test.rb +++ b/actionpack/test/template/tag_helper_test.rb @@ -1,10 +1,7 @@ require 'abstract_unit' -class TagHelperTest < Test::Unit::TestCase - include ActionView::Helpers::TagHelper - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::TextHelper - include ActionView::Helpers::CaptureHelper +class TagHelperTest < ActionView::TestCase + tests ActionView::Helpers::TagHelper def test_tag assert_equal "
", tag("br") diff --git a/actionpack/test/template/test_test.rb b/actionpack/test/template/test_test.rb new file mode 100644 index 0000000000..660f51b3be --- /dev/null +++ b/actionpack/test/template/test_test.rb @@ -0,0 +1,56 @@ +require 'abstract_unit' + +module PeopleHelper + def title(text) + content_tag(:h1, text) + end + + def homepage_path + people_path + end + + def homepage_url + people_url + end + + def link_to_person(person) + link_to person.name, person + end +end + +class PeopleHelperTest < ActionView::TestCase + def setup + ActionController::Routing::Routes.draw do |map| + map.people 'people', :controller => 'people', :action => 'index' + map.connect ':controller/:action/:id' + end + end + + def test_title + assert_equal "

Ruby on Rails

", title("Ruby on Rails") + end + + def test_homepage_path + assert_equal "/people", homepage_path + end + + def test_homepage_url + assert_equal "http://test.host/people", homepage_url + end + + uses_mocha "link_to_person" do + def test_link_to_person + person = mock(:name => "David") + expects(:mocha_mock_path).with(person).returns("/people/1") + assert_equal 'David', link_to_person(person) + end + end +end + +class CrazyHelperTest < ActionView::TestCase + tests PeopleHelper + + def test_helper_class_can_be_set_manually_not_just_inferred + assert_equal PeopleHelper, self.class.helper_class + end +end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 7d92bce4bd..25ecda687f 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -1,9 +1,8 @@ require 'abstract_unit' require 'testing_sandbox' -class TextHelperTest < Test::Unit::TestCase - include ActionView::Helpers::TextHelper - include ActionView::Helpers::TagHelper +class TextHelperTest < ActionView::TestCase + tests ActionView::Helpers::TextHelper include TestingSandbox def setup diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 9cd3b6e2f9..d45ea08a6f 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -2,10 +2,8 @@ require 'abstract_unit' RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port, :env) -class UrlHelperTest < Test::Unit::TestCase - include ActionView::Helpers::AssetTagHelper - include ActionView::Helpers::UrlHelper - include ActionView::Helpers::TagHelper +class UrlHelperTest < ActionView::TestCase + tests ActionView::Helpers::UrlHelper def setup @controller = Class.new do @@ -293,7 +291,7 @@ class UrlHelperTest < Test::Unit::TestCase end end -class UrlHelperWithControllerTest < Test::Unit::TestCase +class UrlHelperWithControllerTest < ActionView::TestCase class UrlHelperController < ActionController::Base self.view_paths = [ "#{File.dirname(__FILE__)}/../fixtures/" ] @@ -310,7 +308,7 @@ class UrlHelperWithControllerTest < Test::Unit::TestCase def rescue_action(e) raise e end end - include ActionView::Helpers::UrlHelper + tests ActionView::Helpers::UrlHelper def setup @request = ActionController::TestRequest.new @@ -348,7 +346,7 @@ class UrlHelperWithControllerTest < Test::Unit::TestCase end end -class LinkToUnlessCurrentWithControllerTest < Test::Unit::TestCase +class LinkToUnlessCurrentWithControllerTest < ActionView::TestCase class TasksController < ActionController::Base self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"] @@ -372,7 +370,7 @@ class LinkToUnlessCurrentWithControllerTest < Test::Unit::TestCase end end - include ActionView::Helpers::UrlHelper + tests ActionView::Helpers::UrlHelper def setup @request = ActionController::TestRequest.new @@ -440,7 +438,7 @@ class Session end end -class PolymorphicControllerTest < Test::Unit::TestCase +class PolymorphicControllerTest < ActionView::TestCase class WorkshopsController < ActionController::Base self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"] @@ -479,7 +477,7 @@ class PolymorphicControllerTest < Test::Unit::TestCase def rescue_action(e) raise e end end - include ActionView::Helpers::UrlHelper + tests ActionView::Helpers::UrlHelper def setup @request = ActionController::TestRequest.new -- cgit v1.2.3 From 3f8d3cd04ff0bd7cbf70c11d49a3dc009dfa98a0 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 19 Apr 2008 19:26:56 +0100 Subject: Remove unused ignore_missing_templates option --- actionpack/lib/action_controller/base.rb | 5 +---- actionpack/test/abstract_unit.rb | 1 - 2 files changed, 1 insertion(+), 5 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 32df035871..c0f3122fbf 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -327,9 +327,6 @@ module ActionController #:nodoc: # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers. cattr_accessor :logger - # Turn on +ignore_missing_templates+ if you want to unit test actions without making the associated templates. - cattr_accessor :ignore_missing_templates - # Controls the resource action separator @@resource_action_separator = "/" cattr_accessor :resource_action_separator @@ -1219,7 +1216,7 @@ module ActionController #:nodoc: end def add_class_variables_to_assigns - %w(view_paths logger ignore_missing_templates).each do |cvar| + %w(view_paths logger).each do |cvar| @assigns[cvar] = self.send(cvar) end end diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index d90f299b8a..fa1c3293b4 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -20,7 +20,6 @@ end ActiveSupport::Deprecation.debug = true ActionController::Base.logger = nil -ActionController::Base.ignore_missing_templates = false ActionController::Routing::Routes.reload rescue nil -- cgit v1.2.3 From 14a40804a29a57ad05ca6bffbe1e5334089593a9 Mon Sep 17 00:00:00 2001 From: Paul Horsfall Date: Sat, 19 Apr 2008 16:19:47 -0500 Subject: Add conditional options to caches_page method [#25 state:resolved] Signed-off-by: Joshua Peek --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/caching/pages.rb | 12 ++++++++++-- actionpack/test/controller/caching_test.rb | 9 ++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 4694c0e996..9c72fd945a 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add conditional options to caches_page method. [Paul Horsfall] + * Move missing template logic to ActionView. [Pratik] * Introduce ActionView::InlineTemplate class. [Pratik] diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb index 755f1e4e0a..7aa6ce154b 100644 --- a/actionpack/lib/action_controller/caching/pages.rb +++ b/actionpack/lib/action_controller/caching/pages.rb @@ -78,10 +78,18 @@ module ActionController #:nodoc: # Caches the +actions+ using the page-caching approach that'll store the cache in a path within the page_cache_directory that # matches the triggering url. + # + # Usage: + # + # # cache the index action + # caches_page :index + # + # # cache the index action except for JSON requests + # caches_page :index, :if => Proc.new { |c| !c.request.format.json? } def caches_page(*actions) return unless perform_caching - actions = actions.map(&:to_s) - after_filter { |c| c.cache_page if actions.include?(c.action_name) } + options = actions.extract_options! + after_filter({:only => actions}.merge(options)) { |c| c.cache_page } end private diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 7bd64e2870..ddc1c68383 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -8,7 +8,8 @@ ActionController::Base.page_cache_directory = FILE_STORE_PATH ActionController::Base.cache_store = :file_store, FILE_STORE_PATH class PageCachingTestController < ActionController::Base - caches_page :ok, :no_content, :found, :not_found + caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? } + caches_page :found, :not_found def ok head :ok @@ -127,6 +128,12 @@ class PageCachingTest < Test::Unit::TestCase end end end + + def test_page_caching_conditional_options + @request.env['HTTP_ACCEPT'] = 'application/json' + get :ok + assert_page_not_cached :ok + end private def assert_page_cached(action, message = "#{action} should have been cached") -- cgit v1.2.3 From caa03a5c870c6a03a35f6dcfaf040a6d689eaee2 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 21 Apr 2008 02:43:48 +0100 Subject: Remove ActionController::Base#add_class_variables_to_assigns --- actionpack/lib/action_controller/base.rb | 7 ------- 1 file changed, 7 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index c0f3122fbf..93007e2d1c 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -1194,7 +1194,6 @@ module ActionController #:nodoc: def add_variables_to_assigns unless @variables_added add_instance_variables_to_assigns - add_class_variables_to_assigns if view_controller_internals @variables_added = true end end @@ -1215,12 +1214,6 @@ module ActionController #:nodoc: end end - def add_class_variables_to_assigns - %w(view_paths logger).each do |cvar| - @assigns[cvar] = self.send(cvar) - end - end - def protected_instance_variables if view_controller_internals %w(@assigns @performed_redirect @performed_render) -- cgit v1.2.3 From 2b69840e5efba885c8ec6281d5b8a56fcabff283 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 21 Apr 2008 03:38:16 +0100 Subject: Remove ActionController::Base#view_controller_internals Get rid of ActionController::Base#view_controller_internals flag and use @@protected_view_variables for storing the list of controller specific instance variables which should be inaccessible inside views. --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 30 +++++----------------- .../lib/action_controller/caching/actions.rb | 5 ---- actionpack/test/controller/new_render_test.rb | 16 ------------ 4 files changed, 8 insertions(+), 45 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 9c72fd945a..6555560bdd 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Remove ActionController::Base#view_controller_internals flag. [Pratik] + * Add conditional options to caches_page method. [Paul Horsfall] * Move missing template logic to ActionView. [Pratik] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 93007e2d1c..1aa027f9d7 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -253,16 +253,11 @@ module ActionController #:nodoc: DEFAULT_RENDER_STATUS_CODE = "200 OK" include StatusCodes - - # Determines whether the view has access to controller internals @request, @response, @session, and @template. - # By default, it does. - @@view_controller_internals = true - cattr_accessor :view_controller_internals - - # Protected instance variable cache - @@protected_variables_cache = nil - cattr_accessor :protected_variables_cache - + + # Controller specific instance variables which will not be accessible inside views. + @@protected_view_variables = %w(@assigns @performed_redirect @performed_render @variables_added @request_origin @url @parent_controller + @action_name @before_filter_chain_aborted @action_cache_path) + # Prepends all the URL-generating helpers from AssetHelper. This makes it possible to easily move javascripts, stylesheets, # and images to a dedicated asset server away from the main web server. Example: # ActionController::Base.asset_host = "http://assets.example.com" @@ -1207,24 +1202,11 @@ module ActionController #:nodoc: end def add_instance_variables_to_assigns - @@protected_variables_cache ||= Set.new(protected_instance_variables) - instance_variable_names.each do |var| - next if @@protected_variables_cache.include?(var) + (instance_variable_names - @@protected_view_variables).each do |var| @assigns[var[1..-1]] = instance_variable_get(var) end end - def protected_instance_variables - if view_controller_internals - %w(@assigns @performed_redirect @performed_render) - else - %w(@assigns @performed_redirect @performed_render - @_request @request @_response @response @_params @params - @_session @session @_cookies @cookies - @template @request_origin @parent_controller) - end - end - def request_origin # this *needs* to be cached! # otherwise you'd get different results if calling it more than once diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb index 4410e47eb3..7b0551c664 100644 --- a/actionpack/lib/action_controller/caching/actions.rb +++ b/actionpack/lib/action_controller/caching/actions.rb @@ -41,7 +41,6 @@ module ActionController #:nodoc: base.extend(ClassMethods) base.class_eval do attr_accessor :rendered_action_cache, :action_cache_path - alias_method_chain :protected_instance_variables, :action_caching end end @@ -55,10 +54,6 @@ module ActionController #:nodoc: end protected - def protected_instance_variables_with_action_caching - protected_instance_variables_without_action_caching + %w(@action_cache_path) - end - def expire_action(options = {}) return unless cache_configured? diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 80cf09e5f3..9f9d861d32 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -529,26 +529,10 @@ class NewRenderTest < Test::Unit::TestCase end def test_access_to_request_in_view - view_internals_old_value = ActionController::Base.view_controller_internals - - ActionController::Base.view_controller_internals = false - ActionController::Base.protected_variables_cache = nil - - get :hello_world - assert !assigns.include?('_request'), '_request should not be in assigns' - assert !assigns.include?('request'), 'request should not be in assigns' - - ActionController::Base.view_controller_internals = true - ActionController::Base.protected_variables_cache = nil - get :hello_world assert !assigns.include?('request'), 'request should not be in assigns' assert_kind_of ActionController::AbstractRequest, assigns['_request'] assert_kind_of ActionController::AbstractRequest, @response.template.request - - ensure - ActionController::Base.view_controller_internals = view_internals_old_value - ActionController::Base.protected_variables_cache = nil end def test_render_xml -- cgit v1.2.3 From c2c779044ffb1c435f4722f62fcbd400883f3225 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Mon, 21 Apr 2008 00:08:45 -0500 Subject: datetime_select defaults to Time.zone.now when config.time_zone is set --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/date_helper.rb | 2 +- actionpack/test/template/date_helper_test.rb | 36 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 6555560bdd..46251534ec 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* datetime_select defaults to Time.zone.now when config.time_zone is set [Geoff Buesing] + * Remove ActionController::Base#view_controller_internals flag. [Pratik] * Add conditional options to caches_page method. [Paul Horsfall] diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index c71d59f373..23b7f3ba7c 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -675,7 +675,7 @@ module ActionView def default_time_from_options(default) case default when nil - Time.now + Time.current when Date, Time default else diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 9bd433e76b..fab41d801d 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1219,6 +1219,42 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, datetime_select("post", "updated_at") end + + uses_mocha 'TestDatetimeSelectDefaultsToTimeZoneNowWhenConfigTimeZoneIsSet' do + def test_datetime_select_defaults_to_time_zone_now_when_config_time_zone_is_set + time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0) + time_zone = mock() + time_zone.expects(:now).returns time + Time.zone_default = time_zone + @post = Post.new + + expected = %{\n" + + expected << %{\n" + + expected << %{\n" + + expected << " — " + + expected << %{\n" + expected << " : " + expected << %{\n" + + assert_dom_equal expected, datetime_select("post", "updated_at") + ensure + Time.zone_default = nil + end + end def test_datetime_select_within_fields_for @post = Post.new -- cgit v1.2.3 From f757f5838818ce35f7927a10a8cda6f9583869c5 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Mon, 21 Apr 2008 00:40:04 -0500 Subject: select_datetime and select_time default to Time.zone.now when config.time_zone is set --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/date_helper.rb | 4 ++-- actionpack/test/template/date_helper_test.rb | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 46251534ec..a3b425287d 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* select_datetime and select_time default to Time.zone.now when config.time_zone is set [Geoff Buesing] + * datetime_select defaults to Time.zone.now when config.time_zone is set [Geoff Buesing] * Remove ActionController::Base#view_controller_internals flag. [Pratik] diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 23b7f3ba7c..9f7790d0f9 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -250,7 +250,7 @@ module ActionView # # prefixed with 'payday' rather than 'date' # select_datetime(my_date_time, :prefix => 'payday') # - def select_datetime(datetime = Time.now, options = {}, html_options = {}) + def select_datetime(datetime = Time.current, options = {}, html_options = {}) separator = options[:datetime_separator] || '' select_date(datetime, options, html_options) + separator + select_time(datetime, options, html_options) end @@ -321,7 +321,7 @@ module ActionView # # separated by ':' and includes an input for seconds # select_time(my_time, :time_separator => ':', :include_seconds => true) # - def select_time(datetime = Time.now, options = {}, html_options = {}) + def select_time(datetime = Time.current, options = {}, html_options = {}) separator = options[:time_separator] || '' select_hour(datetime, options, html_options) + separator + select_minute(datetime, options, html_options) + (options[:include_seconds] ? separator + select_second(datetime, options, html_options) : '') end diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index fab41d801d..2373600bfe 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -933,6 +933,24 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), {}, :class => 'selector') assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), {:include_seconds => false}, :class => 'selector') end + + uses_mocha 'TestDatetimeAndTimeSelectUseTimeCurrentAsDefault' do + def test_select_datetime_uses_time_current_as_default + time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0) + Time.expects(:current).returns time + expects(:select_date).with(time, anything, anything).returns('') + expects(:select_time).with(time, anything, anything).returns('') + select_datetime + end + + def test_select_time_uses_time_current_as_default + time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0) + Time.expects(:current).returns time + expects(:select_hour).with(time, anything, anything).returns('') + expects(:select_minute).with(time, anything, anything).returns('') + select_time + end + end def test_date_select @post = Post.new -- cgit v1.2.3 From a04f0228776e7616c372f867a1212b5798cde80a Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 21 Apr 2008 11:39:46 +0100 Subject: Delegate ivars to controller instead of copying Reduce number of instance variables being copied from controller to view. Instead, delegate them to controller instance. --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 3 ++- actionpack/lib/action_view/base.rb | 9 ++++----- actionpack/test/controller/new_render_test.rb | 19 +++++++++++++++---- 4 files changed, 23 insertions(+), 10 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index a3b425287d..5a2122b64b 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Reduce number of instance variables being copied from controller to view. [Pratik] + * select_datetime and select_time default to Time.zone.now when config.time_zone is set [Geoff Buesing] * datetime_select defaults to Time.zone.now when config.time_zone is set [Geoff Buesing] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 1aa027f9d7..0c0d0ec4ac 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -256,7 +256,8 @@ module ActionController #:nodoc: # Controller specific instance variables which will not be accessible inside views. @@protected_view_variables = %w(@assigns @performed_redirect @performed_render @variables_added @request_origin @url @parent_controller - @action_name @before_filter_chain_aborted @action_cache_path) + @action_name @before_filter_chain_aborted @action_cache_path @_session @_cookies @_headers @_params + @_flash @_response) # Prepends all the URL-generating helpers from AssetHelper. This makes it possible to easily move javascripts, stylesheets, # and images to a dedicated asset server away from the main web server. Example: diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 12dd7d2bc9..4ed20fec89 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -156,9 +156,6 @@ module ActionView #:nodoc: attr_reader :finder attr_accessor :base_path, :assigns, :template_extension, :first_render attr_accessor :controller - - attr_reader :logger, :response, :headers - attr_internal :cookies, :flash, :headers, :params, :request, :response, :session attr_writer :template_format attr_accessor :current_render_extension @@ -185,7 +182,10 @@ module ActionView #:nodoc: @@erb_variable = '_erbout' cattr_accessor :erb_variable - delegate :request_forgery_protection_token, :to => :controller + attr_internal :request + + delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers, + :flash, :logger, :to => :controller module CompiledTemplates #:nodoc: # holds compiled template code @@ -222,7 +222,6 @@ module ActionView #:nodoc: @assigns = assigns_for_first_render @assigns_added = nil @controller = controller - @logger = controller && controller.logger @finder = TemplateFinder.new(self, view_paths) end diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 9f9d861d32..8e39057f55 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -239,6 +239,14 @@ class NewRenderTestController < ActionController::Base render :inline => "Hello: <%= params[:name] %>" end + def accessing_request_in_template + render :inline => "Hello: <%= request.host %>" + end + + def accessing_logger_in_template + render :inline => "<%= logger.class %>" + end + def accessing_params_in_template_with_layout render :layout => nil, :inline => "Hello: <%= params[:name] %>" end @@ -529,10 +537,13 @@ class NewRenderTest < Test::Unit::TestCase end def test_access_to_request_in_view - get :hello_world - assert !assigns.include?('request'), 'request should not be in assigns' - assert_kind_of ActionController::AbstractRequest, assigns['_request'] - assert_kind_of ActionController::AbstractRequest, @response.template.request + get :accessing_request_in_template + assert_equal "Hello: www.nextangle.com", @response.body + end + + def test_access_to_logger_in_view + get :accessing_logger_in_template + assert_equal "Logger", @response.body end def test_render_xml -- cgit v1.2.3 From e6a3ce3392812f707b78d64ffb04ee52f4517d20 Mon Sep 17 00:00:00 2001 From: Eugene Pimenov Date: Fri, 18 Apr 2008 15:45:33 +0400 Subject: Make sure member names aren't mistakenly set to nil when providing :path_names [#19 state:resolved] Signed-off-by: Michael Koziarski --- actionpack/lib/action_controller/resources.rb | 2 +- actionpack/test/controller/resources_test.rb | 45 ++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb index df7d6ead1b..d3cedfdac7 100644 --- a/actionpack/lib/action_controller/resources.rb +++ b/actionpack/lib/action_controller/resources.rb @@ -527,7 +527,7 @@ module ActionController action_path = action if resource.options[:path_names] action_path = resource.options[:path_names][action] - action_path ||= Base.resources_path_names[action] + action_path ||= Base.resources_path_names[action] || action end map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action_path}", action_options) diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb index 0f1ac30f04..b138cee29f 100644 --- a/actionpack/test/controller/resources_test.rb +++ b/actionpack/test/controller/resources_test.rb @@ -209,6 +209,23 @@ class ResourcesTest < Test::Unit::TestCase end end + def test_member_when_override_paths_for_default_restful_actions_with + [:put, :post].each do |method| + with_restful_routing :messages, :member => { :mark => method }, :path_names => {:new => 'nuevo'} do + mark_options = {:action => 'mark', :id => '1', :controller => "messages"} + mark_path = "/messages/1/mark" + + assert_restful_routes_for :messages, :path_names => {:new => 'nuevo'} do |options| + assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method) + end + + assert_restful_named_routes_for :messages, :path_names => {:new => 'nuevo'} do |options| + assert_named_route mark_path, :mark_message_path, mark_options + end + end + end + end + def test_with_two_member_actions_with_same_method [:put, :post].each do |method| with_restful_routing :messages, :member => { :mark => method, :unmark => method } do @@ -674,11 +691,18 @@ class ResourcesTest < Test::Unit::TestCase options[:options] ||= {} options[:options][:controller] = options[:controller] || controller_name.to_s + new_action = "new" + edit_action = "edit" + if options[:path_names] + new_action = options[:path_names][:new] || "new" + edit_action = options[:path_names][:edit] || "edit" + end + collection_path = "/#{options[:path_prefix]}#{options[:as] || controller_name}" member_path = "#{collection_path}/1" - new_path = "#{collection_path}/new" - edit_member_path = "#{member_path}/edit" - formatted_edit_member_path = "#{member_path}/edit.xml" + new_path = "#{collection_path}/#{new_action}" + edit_member_path = "#{member_path}/#{edit_action}" + formatted_edit_member_path = "#{member_path}/#{edit_action}.xml" with_options(options[:options]) do |controller| controller.assert_routing collection_path, :action => 'index' @@ -730,15 +754,22 @@ class ResourcesTest < Test::Unit::TestCase full_prefix = "/#{options[:path_prefix]}#{options[:as] || controller_name}" name_prefix = options[:name_prefix] + new_action = "new" + edit_action = "edit" + if options[:path_names] + new_action = options[:path_names][:new] || "new" + edit_action = options[:path_names][:edit] || "edit" + end + assert_named_route "#{full_prefix}", "#{name_prefix}#{controller_name}_path", options[:options] assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge( :format => 'xml') assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1') assert_named_route "#{full_prefix}/1.xml", "formatted_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') - assert_named_route "#{full_prefix}/new", "new_#{name_prefix}#{singular_name}_path", options[:options] - assert_named_route "#{full_prefix}/new.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge( :format => 'xml') - assert_named_route "#{full_prefix}/1/edit", "edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1') - assert_named_route "#{full_prefix}/1/edit.xml", "formatted_edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') + assert_named_route "#{full_prefix}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", options[:options] + assert_named_route "#{full_prefix}/#{new_action}.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge( :format => 'xml') + assert_named_route "#{full_prefix}/1/#{edit_action}", "edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1') + assert_named_route "#{full_prefix}/1/#{edit_action}.xml", "formatted_edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') yield options[:options] if block_given? end -- cgit v1.2.3