diff options
Diffstat (limited to 'actionpack/lib/action_controller')
14 files changed, 151 insertions, 53 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index ad2b68af21..5797282b41 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -19,6 +19,7 @@ module ActionController include SessionManagement include ActionController::Caching include ActionController::MimeResponds + include ActionController::PolymorphicRoutes # Rails 2.x compatibility include ActionController::Compatibility diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb index 35111a4b92..43ddf6435a 100644 --- a/actionpack/lib/action_controller/caching/actions.rb +++ b/actionpack/lib/action_controller/caching/actions.rb @@ -80,6 +80,7 @@ module ActionController #:nodoc: def caches_action(*actions) return unless cache_configured? options = actions.extract_options! + options[:layout] = true unless options.key?(:layout) filter_options = options.extract!(:if, :unless).merge(:only => actions) cache_options = options.extract!(:layout, :cache_path).merge(:store_options => options) @@ -87,14 +88,12 @@ module ActionController #:nodoc: end end - def _render_cache_fragment(cache, extension, layout) - render :text => cache, :layout => layout, :content_type => Mime[extension || :html] - end - - def _save_fragment(name, layout, options) + def _save_fragment(name, options) return unless caching_allowed? - content = layout ? view_context.content_for(:layout) : response_body + content = response_body + content = content.join if content.is_a?(Array) + write_fragment(name, content, options) end @@ -112,7 +111,7 @@ module ActionController #:nodoc: class ActionCacheFilter #:nodoc: def initialize(options, &block) - @cache_path, @store_options, @layout = + @cache_path, @store_options, @cache_layout = options.values_at(:cache_path, :store_options, :layout) end @@ -125,12 +124,19 @@ module ActionController #:nodoc: cache_path = ActionCachePath.new(controller, path_options || {}) - if cache = controller.read_fragment(cache_path.path, @store_options) - controller._render_cache_fragment(cache, cache_path.extension, @layout == false) - else + body = controller.read_fragment(cache_path.path, @store_options) + + unless body + controller.action_has_layout = false unless @cache_layout yield - controller._save_fragment(cache_path.path, @layout == false, @store_options) + controller.action_has_layout = true + body = controller._save_fragment(cache_path.path, @store_options) end + + body = controller.render_to_string(:text => cache, :layout => true) unless @cache_layout + + controller.response_body = body + controller.content_type = Mime[cache_path.extension || :html] end end diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb index 89787727bd..473a2fe214 100644 --- a/actionpack/lib/action_controller/caching/fragments.rb +++ b/actionpack/lib/action_controller/caching/fragments.rb @@ -34,20 +34,6 @@ module ActionController #:nodoc: ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :views) end - def fragment_for(buffer, name = {}, options = nil, &block) #:nodoc: - if perform_caching - if fragment_exist?(name, options) - buffer.safe_concat(read_fragment(name, options)) - else - pos = buffer.length - block.call - write_fragment(name, buffer[pos..-1], options) - end - else - block.call - end - end - # Writes <tt>content</tt> to the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats) def write_fragment(key, content, options = nil) return content unless cache_configured? diff --git a/actionpack/lib/action_controller/deprecated/base.rb b/actionpack/lib/action_controller/deprecated/base.rb index 1d05b3fbd6..bbde570ca9 100644 --- a/actionpack/lib/action_controller/deprecated/base.rb +++ b/actionpack/lib/action_controller/deprecated/base.rb @@ -63,7 +63,7 @@ module ActionController def ip_spoofing_check ActiveSupport::Deprecation.warn "ActionController::Base.ip_spoofing_check is deprecated. " << "Configuring ip_spoofing_check on the application configures a middleware.", caller - Rails.application.config.action_disaptch.ip_spoofing_check + Rails.application.config.action_dispatch.ip_spoofing_check end def trusted_proxies=(value) diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb index eebd2c943a..30aa34d956 100644 --- a/actionpack/lib/action_controller/metal.rb +++ b/actionpack/lib/action_controller/metal.rb @@ -34,11 +34,12 @@ module ActionController # and response object available. You might wish to control the # environment and response manually for performance reasons. - attr_internal :status, :headers, :content_type, :response, :request + attr_internal :headers, :response, :request delegate :session, :to => "@_request" def initialize(*) - @_headers = {} + @_headers = {"Content-Type" => "text/html"} + @_status = 200 super end @@ -62,10 +63,19 @@ module ActionController headers["Location"] = url end + def status + @_status + end + def status=(status) @_status = Rack::Utils.status_code(status) end + def response_body=(val) + body = val.respond_to?(:each) ? val : [val] + super body + end + # :api: private def dispatch(name, request) @_request = request diff --git a/actionpack/lib/action_controller/metal/compatibility.rb b/actionpack/lib/action_controller/metal/compatibility.rb index ab8d87b2c4..e6cea483bb 100644 --- a/actionpack/lib/action_controller/metal/compatibility.rb +++ b/actionpack/lib/action_controller/metal/compatibility.rb @@ -40,15 +40,6 @@ module ActionController def initialize_template_class(*) end def assign_shortcuts(*) end - def template - @template ||= view_context - end - - def process_action(*) - template - super - end - def _normalize_options(options) if options[:action] && options[:action].to_s.include?(?/) ActiveSupport::Deprecation.warn "Giving a path to render :action is deprecated. " << diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index 2ac199265d..4f384d1ec5 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -262,7 +262,7 @@ module ActionController #:nodoc: if format = request.negotiate_mime(collector.order) self.content_type ||= format.to_s - self.formats = [format.to_sym] + lookup_context.freeze_formats([format.to_sym]) collector.response_for(format) else head :not_acceptable diff --git a/actionpack/lib/action_controller/metal/rack_delegation.rb b/actionpack/lib/action_controller/metal/rack_delegation.rb index 37106733cb..060117756e 100644 --- a/actionpack/lib/action_controller/metal/rack_delegation.rb +++ b/actionpack/lib/action_controller/metal/rack_delegation.rb @@ -5,10 +5,8 @@ module ActionController module RackDelegation extend ActiveSupport::Concern - included do - delegate :headers, :status=, :location=, :content_type=, - :status, :location, :content_type, :to => "@_response" - end + delegate :headers, :status=, :location=, :content_type=, + :status, :location, :content_type, :to => "@_response" def dispatch(action, request) @_response = ActionDispatch::Response.new diff --git a/actionpack/lib/action_controller/metal/redirecting.rb b/actionpack/lib/action_controller/metal/redirecting.rb index 25e4e18493..b5f1d23ef0 100644 --- a/actionpack/lib/action_controller/metal/redirecting.rb +++ b/actionpack/lib/action_controller/metal/redirecting.rb @@ -76,7 +76,7 @@ module ActionController # The scheme name consist of a letter followed by any combination of # letters, digits, and the plus ("+"), period ("."), or hyphen ("-") # characters; and is terminated by a colon (":"). - when %r{^\w[\w\d+.-]*:.*} + when %r{^\w[\w+.-]*:.*} options when String request.protocol + request.host_with_port + options diff --git a/actionpack/lib/action_controller/metal/renderers.rb b/actionpack/lib/action_controller/metal/renderers.rb index 08325b468c..d906e1fb5b 100644 --- a/actionpack/lib/action_controller/metal/renderers.rb +++ b/actionpack/lib/action_controller/metal/renderers.rb @@ -87,7 +87,7 @@ module ActionController end add :update do |proc, options| - _evaluate_assigns(view_context) + view_context = self.view_context generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(view_context, &proc) self.content_type = Mime::JS self.response_body = generator.to_s diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb index 6178a59029..0b2cee6868 100644 --- a/actionpack/lib/action_controller/metal/responder.rb +++ b/actionpack/lib/action_controller/metal/responder.rb @@ -1,3 +1,5 @@ +require 'active_support/json' + module ActionController #:nodoc: # Responder is responsible for exposing a resource to different mime requests, # usually depending on the HTTP verb. The responder is triggered when diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index 6a3afbb157..0ec89928af 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -1,16 +1,17 @@ require "rails" require "action_controller" +require "action_dispatch/railtie" require "action_view/railtie" require "active_support/core_ext/class/subclasses" require "active_support/deprecation/proxy_wrappers" require "active_support/deprecation" +require "action_controller/railties/log_subscriber" +require "action_controller/railties/url_helpers" + module ActionController class Railtie < Rails::Railtie - railtie_name :action_controller - - require "action_controller/railties/log_subscriber" - require "action_controller/railties/url_helpers" + config.action_controller = ActiveSupport::OrderedOptions.new ad = config.action_dispatch config.action_controller.singleton_class.send(:define_method, :session) do @@ -37,7 +38,7 @@ module ActionController ad.session_store = val end - log_subscriber ActionController::Railties::LogSubscriber.new + log_subscriber :action_controller, ActionController::Railties::LogSubscriber.new initializer "action_controller.logger" do ActionController.base_hook { self.logger ||= Rails.logger } @@ -52,7 +53,9 @@ module ActionController ac.stylesheets_dir = paths.public.stylesheets.to_a.first ac.secret = app.config.cookie_secret - ActionController.base_hook { self.config.replace(ac) } + ActionController.base_hook do + self.config.merge!(ac) + end end initializer "action_controller.initialize_framework_caches" do @@ -67,7 +70,7 @@ module ActionController initializer "action_controller.url_helpers" do |app| ActionController.base_hook do - extend ::ActionController::Railtie::UrlHelpers.with(app.routes) + extend ::ActionController::Railties::UrlHelpers.with(app.routes) end message = "ActionController::Routing::Routes is deprecated. " \ diff --git a/actionpack/lib/action_controller/railties/url_helpers.rb b/actionpack/lib/action_controller/railties/url_helpers.rb index ad2a8d4ef3..5f95e1c621 100644 --- a/actionpack/lib/action_controller/railties/url_helpers.rb +++ b/actionpack/lib/action_controller/railties/url_helpers.rb @@ -1,5 +1,5 @@ module ActionController - class Railtie + module Railties module UrlHelpers def self.with(router) Module.new do diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index cdb5db32aa..120f34460e 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -1,7 +1,107 @@ require 'rack/session/abstract/id' -require 'action_view/test_case' module ActionController + module TemplateAssertions + extend ActiveSupport::Concern + + included do + setup :setup_subscriptions + teardown :teardown_subscriptions + end + + def setup_subscriptions + @partials = Hash.new(0) + @templates = Hash.new(0) + @layouts = Hash.new(0) + + ActiveSupport::Notifications.subscribe("action_view.render_template") do |name, start, finish, id, payload| + path = payload[:layout] + @layouts[path] += 1 + end + + ActiveSupport::Notifications.subscribe("action_view.render_template!") do |name, start, finish, id, payload| + path = payload[:virtual_path] + next unless path + partial = path =~ /^.*\/_[^\/]*$/ + if partial + @partials[path] += 1 + @partials[path.split("/").last] += 1 + @templates[path] += 1 + else + @templates[path] += 1 + end + end + end + + def teardown_subscriptions + ActiveSupport::Notifications.unsubscribe("action_view.render_template!") + end + + # Asserts that the request was rendered with the appropriate template file or partials + # + # ==== Examples + # + # # assert that the "new" view template was rendered + # assert_template "new" + # + # # assert that the "_customer" partial was rendered twice + # assert_template :partial => '_customer', :count => 2 + # + # # assert that no partials were rendered + # assert_template :partial => false + # + def assert_template(options = {}, message = nil) + validate_request! + + case options + when NilClass, String + rendered = @templates + msg = build_message(message, + "expecting <?> but rendering with <?>", + options, rendered.keys.join(', ')) + assert_block(msg) do + if options.nil? + @templates.blank? + else + rendered.any? { |t,num| t.match(options) } + end + end + when Hash + if expected_partial = options[:partial] + if expected_count = options[:count] + actual_count = @partials[expected_partial] + # actual_count = found.nil? ? 0 : found[1] + 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) + elsif options.key?(:layout) + msg = build_message(message, + "expecting layout <?> but action rendered <?>", + expected_layout, @layouts.keys) + + case layout = options[:layout] + when String + assert(@layouts.include?(expected_layout), msg) + when Regexp + assert(@layouts.any? {|l| l =~ layout }, msg) + when nil + assert(@layouts.empty?, msg) + end + else + msg = build_message(message, + "expecting partial <?> but action rendered <?>", + options[:partial], @partials.keys) + assert(@partials.include?(expected_partial), msg) + end + else + assert @partials.empty?, + "Expected no partials to be rendered" + end + end + end + end + class TestRequest < ActionDispatch::TestRequest #:nodoc: def initialize(env = {}) super @@ -181,6 +281,7 @@ module ActionController # assert_redirected_to page_url(:title => 'foo') class TestCase < ActiveSupport::TestCase include ActionDispatch::TestProcess + include ActionController::TemplateAssertions # Executes a request simulating GET HTTP method and set/volley the response def get(action, parameters = nil, session = nil, flash = nil) |