aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-08-13 10:27:53 +0200
committerJosé Valim <jose.valim@gmail.com>2009-08-13 10:27:53 +0200
commit8692b11e81ab2df2e4122c2f47d91e5eaaf508f2 (patch)
tree695173d98666ddf3554ee380849a750d353d3314 /actionpack
parent4f9047ecc8f0e2c3d4491bae0051679569da71dc (diff)
parentf413a703ba5bcf4b369932ae805615c4a34d34cb (diff)
downloadrails-8692b11e81ab2df2e4122c2f47d91e5eaaf508f2.tar.gz
rails-8692b11e81ab2df2e4122c2f47d91e5eaaf508f2.tar.bz2
rails-8692b11e81ab2df2e4122c2f47d91e5eaaf508f2.zip
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/examples/minimal.rb118
-rw-r--r--actionpack/examples/very_simple.rb50
-rw-r--r--actionpack/examples/views/_collection.erb1
-rw-r--r--actionpack/examples/views/_hello.erb1
-rw-r--r--actionpack/examples/views/_many_partials.erb10
-rw-r--r--actionpack/examples/views/_partial.erb10
-rw-r--r--actionpack/examples/views/layouts/alt.html.erb1
-rw-r--r--actionpack/examples/views/layouts/kaigi.html.erb1
-rw-r--r--actionpack/examples/views/template.html.erb1
-rw-r--r--actionpack/lib/abstract_controller/layouts.rb69
-rw-r--r--actionpack/lib/action_controller/metal/compatibility.rb12
-rw-r--r--actionpack/lib/action_controller/metal/hide_actions.rb14
-rw-r--r--actionpack/lib/action_controller/metal/streaming.rb3
-rw-r--r--actionpack/lib/action_controller/metal/url_for.rb10
-rw-r--r--actionpack/lib/action_controller/testing/process.rb2
-rw-r--r--actionpack/lib/action_controller/testing/test_case.rb1
-rwxr-xr-xactionpack/lib/action_dispatch/http/request.rb28
-rw-r--r--actionpack/lib/action_dispatch/http/response.rb140
-rw-r--r--actionpack/lib/action_view/render/partials.rb48
-rw-r--r--actionpack/lib/action_view/template/resolver.rb36
-rw-r--r--actionpack/lib/action_view/test_case.rb21
-rw-r--r--actionpack/test/controller/caching_test.rb1
-rw-r--r--actionpack/test/dispatch/request_test.rb4
-rw-r--r--actionpack/test/fixtures/test/_customer_with_var.erb2
24 files changed, 244 insertions, 340 deletions
diff --git a/actionpack/examples/minimal.rb b/actionpack/examples/minimal.rb
deleted file mode 100644
index a9015da053..0000000000
--- a/actionpack/examples/minimal.rb
+++ /dev/null
@@ -1,118 +0,0 @@
-# Pass NEW=1 to run with the new Base
-ENV['RAILS_ENV'] ||= 'production'
-ENV['NO_RELOAD'] ||= '1'
-
-$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
-$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../activesupport/lib"
-require 'action_controller'
-require 'action_controller/new_base' if ENV['NEW']
-require 'action_view'
-require 'benchmark'
-
-class Runner
- def initialize(app, output)
- @app, @output = app, output
- end
-
- def puts(*)
- super if @output
- end
-
- def call(env)
- env['n'].to_i.times { @app.call(env) }
- @app.call(env).tap { |response| report(env, response) }
- end
-
- def report(env, response)
- return unless ENV["DEBUG"]
- out = env['rack.errors']
- out.puts response[0], response[1].to_yaml, '---'
- response[2].each { |part| out.puts part }
- out.puts '---'
- end
-
- def self.puts(*)
- super if @output
- end
-
- def self.run(app, n, label, output = true)
- @output = output
- puts label, '=' * label.size if label
- env = Rack::MockRequest.env_for("/").merge('n' => n, 'rack.input' => StringIO.new(''), 'rack.errors' => $stdout)
- t = Benchmark.realtime { new(app, output).call(env) }
- puts "%d ms / %d req = %.1f usec/req" % [10**3 * t, n, 10**6 * t / n]
- puts
- end
-end
-
-
-N = (ENV['N'] || 1000).to_i
-
-module ActionController::Rails2Compatibility
- instance_methods.each do |name|
- remove_method name
- end
-end
-
-class BasePostController < ActionController::Base
- append_view_path "#{File.dirname(__FILE__)}/views"
-
- def overhead
- self.response_body = ''
- end
-
- def index
- render :text => ''
- end
-
- def partial
- render :partial => "/partial"
- end
-
- def many_partials
- render :partial => "/many_partials"
- end
-
- def partial_collection
- render :partial => "/collection", :collection => [1,2,3,4,5,6,7,8,9,10]
- end
-
- def show_template
- render :template => "template"
- end
-end
-
-OK = [200, {}, []]
-MetalPostController = lambda { OK }
-
-class HttpPostController < ActionController::Metal
- def index
- self.response_body = ''
- end
-end
-
-unless ENV["PROFILE"]
- Runner.run(BasePostController.action(:overhead), N, 'overhead', false)
- Runner.run(BasePostController.action(:index), N, 'index', false)
- Runner.run(BasePostController.action(:partial), N, 'partial', false)
- Runner.run(BasePostController.action(:many_partials), N, 'many_partials', false)
- Runner.run(BasePostController.action(:partial_collection), N, 'collection', false)
- Runner.run(BasePostController.action(:show_template), N, 'template', false)
-
- (ENV["M"] || 1).to_i.times do
- Runner.run(BasePostController.action(:overhead), N, 'overhead')
- Runner.run(BasePostController.action(:index), N, 'index')
- Runner.run(BasePostController.action(:partial), N, 'partial')
- Runner.run(BasePostController.action(:many_partials), N, 'many_partials')
- Runner.run(BasePostController.action(:partial_collection), N, 'collection')
- Runner.run(BasePostController.action(:show_template), N, 'template')
- end
-else
- Runner.run(BasePostController.action(:many_partials), N, 'many_partials')
- require "ruby-prof"
- RubyProf.start
- Runner.run(BasePostController.action(:many_partials), N, 'many_partials')
- result = RubyProf.stop
- printer = RubyProf::CallStackPrinter.new(result)
- printer.print(File.open("output.html", "w"))
-end \ No newline at end of file
diff --git a/actionpack/examples/very_simple.rb b/actionpack/examples/very_simple.rb
deleted file mode 100644
index 6714185172..0000000000
--- a/actionpack/examples/very_simple.rb
+++ /dev/null
@@ -1,50 +0,0 @@
-$:.push "rails/activesupport/lib"
-$:.push "rails/actionpack/lib"
-
-require "action_controller"
-
-class Kaigi < ActionController::Metal
- include AbstractController::Callbacks
- include ActionController::RackConvenience
- include ActionController::RenderingController
- include ActionController::Layouts
- include ActionView::Context
-
- before_filter :set_name
- append_view_path "views"
-
- def view_context
- self
- end
-
- def controller
- self
- end
-
- DEFAULT_LAYOUT = Object.new.tap {|l| def l.render(*) yield end }
-
- def render_template(template, layout = DEFAULT_LAYOUT, options = {}, partial = false)
- ret = template.render(self, {})
- layout.render(self, {}) { ret }
- end
-
- def index
- render :template => "template"
- end
-
- def alt
- render :template => "template", :layout => "alt"
- end
-
- private
- def set_name
- @name = params[:name]
- end
-end
-
-app = Rack::Builder.new do
- map("/kaigi") { run Kaigi.action(:index) }
- map("/kaigi/alt") { run Kaigi.action(:alt) }
-end.to_app
-
-Rack::Handler::Mongrel.run app, :Port => 3000
diff --git a/actionpack/examples/views/_collection.erb b/actionpack/examples/views/_collection.erb
deleted file mode 100644
index bcfe958e2c..0000000000
--- a/actionpack/examples/views/_collection.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= collection %> \ No newline at end of file
diff --git a/actionpack/examples/views/_hello.erb b/actionpack/examples/views/_hello.erb
deleted file mode 100644
index 5ab2f8a432..0000000000
--- a/actionpack/examples/views/_hello.erb
+++ /dev/null
@@ -1 +0,0 @@
-Hello \ No newline at end of file
diff --git a/actionpack/examples/views/_many_partials.erb b/actionpack/examples/views/_many_partials.erb
deleted file mode 100644
index 7e379d46f5..0000000000
--- a/actionpack/examples/views/_many_partials.erb
+++ /dev/null
@@ -1,10 +0,0 @@
-<%= render :partial => '/hello' %>
-<%= render :partial => '/hello' %>
-<%= render :partial => '/hello' %>
-<%= render :partial => '/hello' %>
-<%= render :partial => '/hello' %>
-<%= render :partial => '/hello' %>
-<%= render :partial => '/hello' %>
-<%= render :partial => '/hello' %>
-<%= render :partial => '/hello' %>
-<%= render :partial => '/hello' %> \ No newline at end of file
diff --git a/actionpack/examples/views/_partial.erb b/actionpack/examples/views/_partial.erb
deleted file mode 100644
index 3ca8e80b52..0000000000
--- a/actionpack/examples/views/_partial.erb
+++ /dev/null
@@ -1,10 +0,0 @@
-<%= "Hello" %>
-<%= "Hello" %>
-<%= "Hello" %>
-<%= "Hello" %>
-<%= "Hello" %>
-<%= "Hello" %>
-<%= "Hello" %>
-<%= "Hello" %>
-<%= "Hello" %>
-<%= "Hello" %>
diff --git a/actionpack/examples/views/layouts/alt.html.erb b/actionpack/examples/views/layouts/alt.html.erb
deleted file mode 100644
index c4816337a6..0000000000
--- a/actionpack/examples/views/layouts/alt.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-+ <%= yield %> + \ No newline at end of file
diff --git a/actionpack/examples/views/layouts/kaigi.html.erb b/actionpack/examples/views/layouts/kaigi.html.erb
deleted file mode 100644
index 274607a96a..0000000000
--- a/actionpack/examples/views/layouts/kaigi.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-Hello <%= yield %> Goodbye \ No newline at end of file
diff --git a/actionpack/examples/views/template.html.erb b/actionpack/examples/views/template.html.erb
deleted file mode 100644
index 5ab2f8a432..0000000000
--- a/actionpack/examples/views/template.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-Hello \ No newline at end of file
diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb
index 0063d54149..ac2154dffc 100644
--- a/actionpack/lib/abstract_controller/layouts.rb
+++ b/actionpack/lib/abstract_controller/layouts.rb
@@ -6,13 +6,51 @@ module AbstractController
included do
extlib_inheritable_accessor(:_layout_conditions) { Hash.new }
+ extlib_inheritable_accessor(:_action_has_layout) { Hash.new }
_write_layout_method
end
module ClassMethods
def inherited(klass)
super
- klass._write_layout_method
+ klass.class_eval do
+ _write_layout_method
+ @found_layouts = {}
+ end
+ end
+
+ def cache_layout(details)
+ layout = @found_layouts
+ values = details.values_at(:formats, :locale)
+
+ # Cache nil
+ if layout.key?(values)
+ return layout[values]
+ else
+ layout[values] = yield
+ end
+ end
+
+ # This module is mixed in if layout conditions are provided. This means
+ # that if no layout conditions are used, this method is not used
+ module LayoutConditions
+ # Determines whether the current action has a layout by checking the
+ # action name against the :only and :except conditions set on the
+ # layout.
+ #
+ # ==== Returns
+ # Boolean:: True if the action has a layout, false otherwise.
+ def _action_has_layout?
+ conditions = _layout_conditions
+
+ if only = conditions[:only]
+ only.include?(action_name)
+ elsif except = conditions[:except]
+ !except.include?(action_name)
+ else
+ true
+ end
+ end
end
# Specify the layout to use for this class.
@@ -31,6 +69,8 @@ module AbstractController
# :only<#to_s, Array[#to_s]>:: A list of actions to apply this layout to.
# :except<#to_s, Array[#to_s]>:: Apply this layout to all actions but this one
def layout(layout, conditions = {})
+ include LayoutConditions unless conditions.empty?
+
conditions.each {|k, v| conditions[k] = Array(v).map {|a| a.to_s} }
self._layout_conditions = conditions
@@ -76,10 +116,12 @@ module AbstractController
when nil
self.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
def _layout(details)
- if view_paths.exists?("#{_implied_layout_name}", details, "layouts")
- "#{_implied_layout_name}"
- else
- super
+ self.class.cache_layout(details) do
+ if view_paths.exists?("#{_implied_layout_name}", details, "layouts")
+ "#{_implied_layout_name}"
+ else
+ super
+ end
end
end
ruby_eval
@@ -136,7 +178,7 @@ module AbstractController
view_paths.find(name, details, prefix)
end
- # Returns the default layout for this controller and a given set of details.
+ # Returns the default layout for this controller and a given set of details.
# Optionally raises an exception if the layout could not be found.
#
# ==== Parameters
@@ -162,21 +204,8 @@ module AbstractController
end
end
- # Determines whether the current action has a layout by checking the
- # action name against the :only and :except conditions set on the
- # layout.
- #
- # ==== Returns
- # Boolean:: True if the action has a layout, false otherwise.
def _action_has_layout?
- conditions = _layout_conditions
- if only = conditions[:only]
- only.include?(action_name)
- elsif except = conditions[:except]
- !except.include?(action_name)
- else
- true
- end
+ true
end
end
end
diff --git a/actionpack/lib/action_controller/metal/compatibility.rb b/actionpack/lib/action_controller/metal/compatibility.rb
index f94d1c669c..5b0165f0e7 100644
--- a/actionpack/lib/action_controller/metal/compatibility.rb
+++ b/actionpack/lib/action_controller/metal/compatibility.rb
@@ -25,8 +25,9 @@ module ActionController
cattr_accessor :relative_url_root
self.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']
- cattr_accessor :default_charset
- self.default_charset = "utf-8"
+ class << self
+ delegate :default_charset=, :to => "ActionDispatch::Response"
+ end
# cattr_reader :protected_instance_variables
cattr_accessor :protected_instance_variables
@@ -101,11 +102,10 @@ module ActionController
options[:template].sub!(/^\//, '')
end
- options[:text] = nil if options[:nothing] == true
+ options[:text] = nil if options.delete(:nothing) == true
+ options[:text] = " " if options.key?(:text) && options[:text].nil?
- body = super
- body = [' '] if body.blank?
- body
+ super || " "
end
def _handle_method_missing
diff --git a/actionpack/lib/action_controller/metal/hide_actions.rb b/actionpack/lib/action_controller/metal/hide_actions.rb
index af68c772b1..cdacdc40a6 100644
--- a/actionpack/lib/action_controller/metal/hide_actions.rb
+++ b/actionpack/lib/action_controller/metal/hide_actions.rb
@@ -13,7 +13,9 @@ module ActionController
# Overrides AbstractController::Base#action_method? to return false if the
# action name is in the list of hidden actions.
def action_method?(action_name)
- !hidden_actions.include?(action_name) && super
+ self.class.visible_action?(action_name) do
+ !hidden_actions.include?(action_name) && super
+ end
end
module ClassMethods
@@ -25,6 +27,16 @@ module ActionController
hidden_actions.merge(args.map! {|a| a.to_s })
end
+ def inherited(klass)
+ klass.instance_variable_set("@visible_actions", {})
+ super
+ end
+
+ def visible_action?(action_name)
+ return @visible_actions[action_name] if @visible_actions.key?(action_name)
+ @visible_actions[action_name] = yield
+ end
+
# Overrides AbstractController::Base#action_methods to remove any methods
# that are listed as hidden methods.
def action_methods
diff --git a/actionpack/lib/action_controller/metal/streaming.rb b/actionpack/lib/action_controller/metal/streaming.rb
index 57318e8747..4761763a26 100644
--- a/actionpack/lib/action_controller/metal/streaming.rb
+++ b/actionpack/lib/action_controller/metal/streaming.rb
@@ -145,7 +145,6 @@ module ActionController #:nodoc:
def send_data(data, options = {}) #:doc:
logger.info "Sending data #{options[:filename]}" if logger
send_file_headers! options.merge(:length => data.bytesize)
- @performed_render = false
render :status => options[:status], :text => data
end
@@ -175,6 +174,8 @@ module ActionController #:nodoc:
'Content-Transfer-Encoding' => 'binary'
)
+ response.sending_file = true
+
# Fix a problem with IE 6.0 on opening downloaded files:
# If Cache-Control: no-cache is set (which Rails does by default),
# IE removes the file it just downloaded from its cache immediately
diff --git a/actionpack/lib/action_controller/metal/url_for.rb b/actionpack/lib/action_controller/metal/url_for.rb
index 7119c14cd3..14c6523045 100644
--- a/actionpack/lib/action_controller/metal/url_for.rb
+++ b/actionpack/lib/action_controller/metal/url_for.rb
@@ -4,15 +4,6 @@ module ActionController
include RackConvenience
- def process_action(*)
- initialize_current_url
- super
- end
-
- def initialize_current_url
- @url = UrlRewriter.new(request, params.clone)
- end
-
# Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in
# the form of a hash, just like the one you would use for url_for directly. Example:
#
@@ -40,6 +31,7 @@ module ActionController
when String
options
when Hash
+ @url ||= UrlRewriter.new(request, params)
@url.rewrite(rewrite_options(options))
else
polymorphic_url(options)
diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb
index d32d5562e8..09b1a59254 100644
--- a/actionpack/lib/action_controller/testing/process.rb
+++ b/actionpack/lib/action_controller/testing/process.rb
@@ -52,7 +52,7 @@ module ActionController #:nodoc:
class TestResponse < ActionDispatch::TestResponse
def recycle!
@status = 200
- @header = Rack::Utils::HeaderHash.new
+ @header = {}
@writer = lambda { |x| @body << x }
@block = nil
@length = 0
diff --git a/actionpack/lib/action_controller/testing/test_case.rb b/actionpack/lib/action_controller/testing/test_case.rb
index a11755b517..b66a4c15ff 100644
--- a/actionpack/lib/action_controller/testing/test_case.rb
+++ b/actionpack/lib/action_controller/testing/test_case.rb
@@ -179,7 +179,6 @@ module ActionController
if @controller
@controller.request = @request
@controller.params = {}
- @controller.send(:initialize_current_url)
end
end
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb
index 4190fa21cd..b23306af62 100755
--- a/actionpack/lib/action_dispatch/http/request.rb
+++ b/actionpack/lib/action_dispatch/http/request.rb
@@ -173,19 +173,15 @@ module ActionDispatch
end
end
- # Expand raw_formats by converting Mime::ALL to the Mime::SET.
- #
def formats
if ActionController::Base.use_accept_header
- raw_formats.tap do |ret|
- if ret == ONLY_ALL
- ret.replace Mime::SET
- elsif all = ret.index(Mime::ALL)
- ret.delete_at(all) && ret.insert(all, *Mime::SET)
- end
+ if param = parameters[:format]
+ Array.wrap(Mime[param])
+ else
+ accepts.dup
end
else
- raw_formats + Mime::SET
+ [format]
end
end
@@ -487,7 +483,7 @@ EOM
# matches the order array.
#
def negotiate_mime(order)
- raw_formats.each do |priority|
+ formats.each do |priority|
if priority == Mime::ALL
return order.first
elsif order.include?(priority)
@@ -500,18 +496,6 @@ EOM
private
- def raw_formats
- if ActionController::Base.use_accept_header
- if param = parameters[:format]
- Array.wrap(Mime[param])
- else
- accepts.dup
- end
- else
- [format]
- end
- end
-
def named_host?(host)
!(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
end
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb
index 03d1780b77..055f29a972 100644
--- a/actionpack/lib/action_dispatch/http/response.rb
+++ b/actionpack/lib/action_dispatch/http/response.rb
@@ -32,18 +32,35 @@ module ActionDispatch # :nodoc:
# end
# end
class Response < Rack::Response
- attr_accessor :request
+ attr_accessor :request, :blank
attr_reader :cache_control
- attr_writer :header
+ attr_writer :header, :sending_file
alias_method :headers=, :header=
- delegate :default_charset, :to => 'ActionController::Base'
-
def initialize
- super
+ @status = 200
+ @header = {}
@cache_control = {}
- @header = Rack::Utils::HeaderHash.new
+
+ @writer = lambda { |x| @body << x }
+ @block = nil
+ @length = 0
+
+ @body, @cookie = [], []
+ @sending_file = false
+
+ yield self if block_given?
+ end
+
+ def cache_control
+ @cache_control ||= {}
+ end
+
+ def write(str)
+ s = str.to_s
+ @writer.call s
+ str
end
def status=(status)
@@ -71,7 +88,10 @@ module ActionDispatch # :nodoc:
str
end
+ EMPTY = " "
+
def body=(body)
+ @blank = true if body == EMPTY
@body = body.respond_to?(:to_str) ? [body] : body
end
@@ -113,41 +133,39 @@ module ActionDispatch # :nodoc:
end
def etag
- headers['ETag']
+ @etag
end
def etag?
- headers.include?('ETag')
+ @etag
end
def etag=(etag)
- if etag.blank?
- headers.delete('ETag')
- else
- headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}")
- end
+ key = ActiveSupport::Cache.expand_cache_key(etag)
+ @etag = %("#{Digest::MD5.hexdigest(key)}")
end
- def sending_file?
- headers["Content-Transfer-Encoding"] == "binary"
- end
+ CONTENT_TYPE = "Content-Type"
+
+ cattr_accessor(:default_charset) { "utf-8" }
def assign_default_content_type_and_charset!
- return if !headers["Content-Type"].blank?
+ return if headers[CONTENT_TYPE].present?
@content_type ||= Mime::HTML
- @charset ||= default_charset
+ @charset ||= self.class.default_charset
type = @content_type.to_s.dup
- type << "; charset=#{@charset}" unless sending_file?
+ type << "; charset=#{@charset}" unless @sending_file
- headers["Content-Type"] = type
+ headers[CONTENT_TYPE] = type
end
def prepare!
assign_default_content_type_and_charset!
handle_conditional_get!
- self["Set-Cookie"] ||= ""
+ self["Set-Cookie"] = @cookie.join("\n")
+ self["ETag"] = @etag if @etag
end
def each(&callback)
@@ -168,23 +186,12 @@ module ActionDispatch # :nodoc:
str
end
- def set_cookie(key, value)
- if value.has_key?(:http_only)
- ActiveSupport::Deprecation.warn(
- "The :http_only option in ActionController::Response#set_cookie " +
- "has been renamed. Please use :httponly instead.", caller)
- value[:httponly] ||= value.delete(:http_only)
- end
-
- super(key, value)
- end
-
# Returns the response cookies, converted to a Hash of (name => value) pairs
#
# assert_equal 'AuthorOfNewPage', r.cookies['author']
def cookies
cookies = {}
- if header = headers['Set-Cookie']
+ if header = @cookie
header = header.split("\n") if header.respond_to?(:to_str)
header.each do |cookie|
if pair = cookie.split(';').first
@@ -196,12 +203,43 @@ module ActionDispatch # :nodoc:
cookies
end
+ def set_cookie(key, value)
+ case value
+ when Hash
+ domain = "; domain=" + value[:domain] if value[:domain]
+ path = "; path=" + value[:path] if value[:path]
+ # According to RFC 2109, we need dashes here.
+ # N.B.: cgi.rb uses spaces...
+ expires = "; expires=" + value[:expires].clone.gmtime.
+ strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires]
+ secure = "; secure" if value[:secure]
+ httponly = "; HttpOnly" if value[:httponly]
+ value = value[:value]
+ end
+ value = [value] unless Array === value
+ cookie = Rack::Utils.escape(key) + "=" +
+ value.map { |v| Rack::Utils.escape v }.join("&") +
+ "#{domain}#{path}#{expires}#{secure}#{httponly}"
+
+ @cookie << cookie
+ end
+
+ def delete_cookie(key, value={})
+ @cookie.reject! { |cookie|
+ cookie =~ /\A#{Rack::Utils.escape(key)}=/
+ }
+
+ set_cookie(key,
+ {:value => '', :path => nil, :domain => nil,
+ :expires => Time.at(0) }.merge(value))
+ end
+
private
def handle_conditional_get!
- if etag? || last_modified? || !cache_control.empty?
+ if etag? || last_modified? || !@cache_control.empty?
set_conditional_cache_control!
elsif nonempty_ok_response?
- self.etag = body
+ self.etag = @body
if request && request.etag_matches?(etag)
self.status = 304
@@ -215,29 +253,33 @@ module ActionDispatch # :nodoc:
end
def nonempty_ok_response?
- ok = !@status || @status == 200
- ok && string_body?
+ @status == 200 && string_body?
end
def string_body?
- !body_parts.respond_to?(:call) && body_parts.any? && body_parts.all? { |part| part.is_a?(String) }
+ !@blank && @body.respond_to?(:all?) && @body.all? { |part| part.is_a?(String) }
end
+ DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate"
+
def set_conditional_cache_control!
- if cache_control.empty?
- cache_control.merge!(:public => false, :max_age => 0, :must_revalidate => true)
- end
+ control = @cache_control
+
+ if control.empty?
+ headers["Cache-Control"] = DEFAULT_CACHE_CONTROL
+ else
+ extras = control[:extras]
+ max_age = control[:max_age]
- public_cache, max_age, must_revalidate, extras =
- cache_control.values_at(:public, :max_age, :must_revalidate, :extras)
+ options = []
+ options << "max-age=#{max_age}" if max_age
+ options << (control[:public] ? "public" : "private")
+ options << "must-revalidate" if control[:must_revalidate]
+ options.concat(extras) if extras
- options = []
- options << "max-age=#{max_age}" if max_age
- options << (public_cache ? "public" : "private")
- options << "must-revalidate" if must_revalidate
- options.concat(extras) if extras
+ headers["Cache-Control"] = options.join(", ")
+ end
- headers["Cache-Control"] = options.join(", ")
end
end
end
diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb
index 64f08c447d..83175ab4cf 100644
--- a/actionpack/lib/action_view/render/partials.rb
+++ b/actionpack/lib/action_view/render/partials.rb
@@ -184,6 +184,7 @@ module ActionView
def initialize(view_context, options, block)
partial = options[:partial]
+ @memo = {}
@view = view_context
@options = options
@locals = options[:locals] || {}
@@ -207,9 +208,7 @@ module ActionView
end
def render_collection
- # Even if no template is rendered, this will ensure that the MIME type
- # for the empty response is the same as the provided template
- @options[:_template] = default_template = find_template
+ @options[:_template] = template = find_template
return nil if collection.blank?
@@ -217,15 +216,46 @@ module ActionView
spacer = find_template(@options[:spacer_template]).render(@view, @locals)
end
- segments = []
+ result = template ? collection_with_template(template) : collection_without_template
+ result.join(spacer)
+ end
+
+ def collection_with_template(template)
+ options = @options
+
+ segments, locals, as = [], @locals, options[:as] || :object
+
+ variable_name = template.variable_name
+ counter_name = template.counter_name
+ locals[counter_name] = -1
+
+ collection.each do |object|
+ locals[counter_name] += 1
+ locals[variable_name] = object
+ locals[as] = object if as
+
+ segments << template.render(@view, locals)
+ end
+ segments
+ end
+
+ def collection_without_template
+ options = @options
+
+ segments, locals, as = [], @locals, options[:as] || :object
+ index, template = -1, nil
+
+ collection.each do |object|
+ template = find_template(partial_path(object))
+ locals[template.counter_name] = (index += 1)
+ locals[template.variable_name] = object
+ locals[as] = object if as
- collection.each_with_index do |object, index|
- template = default_template || find_template(partial_path(object))
- @locals[template.counter_name] = index
- segments << render_template(template, object)
+ segments << template.render(@view, locals)
end
- segments.join(spacer)
+ @options[:_template] = template
+ segments
end
def render_template(template, object = @object)
diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb
index ebfc6cc8ce..10f664736f 100644
--- a/actionpack/lib/action_view/template/resolver.rb
+++ b/actionpack/lib/action_view/template/resolver.rb
@@ -41,8 +41,10 @@ module ActionView
end
def handler_glob
- e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join
- "{#{e}}"
+ @handler_glob ||= begin
+ e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join
+ "{#{e}}"
+ end
end
def formats_glob
@@ -60,6 +62,10 @@ module ActionView
class FileSystemResolver < Resolver
+ def self.cached_glob
+ @@cached_glob ||= {}
+ end
+
def initialize(path, options = {})
raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver)
super(options)
@@ -105,20 +111,22 @@ module ActionView
# :api: plugin
def details_to_glob(name, details, prefix, partial, root)
- path = ""
- path << "#{prefix}/" unless prefix.empty?
- path << (partial ? "_#{name}" : name)
-
- extensions = ""
- [:locales, :formats].each do |k|
- extensions << if exts = details[k]
- '{' + exts.map {|e| ".#{e},"}.join + '}'
- else
- k == :formats ? formats_glob : ''
+ self.class.cached_glob[[name, prefix, partial, details, root]] ||= begin
+ path = ""
+ path << "#{prefix}/" unless prefix.empty?
+ path << (partial ? "_#{name}" : name)
+
+ extensions = ""
+ [:locales, :formats].each do |k|
+ extensions << if exts = details[k]
+ '{' + exts.map {|e| ".#{e},"}.join + '}'
+ else
+ k == :formats ? formats_glob : ''
+ end
end
- end
- "#{root}#{path}#{extensions}#{handler_glob}"
+ "#{root}#{path}#{extensions}#{handler_glob}"
+ end
end
# TODO: fix me
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
index e51744d095..c2ccd1d3a5 100644
--- a/actionpack/lib/action_view/test_case.rb
+++ b/actionpack/lib/action_view/test_case.rb
@@ -9,14 +9,16 @@ module ActionView
end
attr_internal :rendered
- alias_method :_render_template_without_template_tracking, :_render_single_template
- def _render_single_template(template, local_assigns, &block)
- if template.respond_to?(:identifier) && template.present?
- @_rendered[:partials][template] += 1 if template.partial?
- @_rendered[:template] ||= []
- @_rendered[:template] << template
- end
- _render_template_without_template_tracking(template, local_assigns, &block)
+ end
+
+ class Template
+ alias_method :render_without_tracking, :render
+ def render(view, locals, &blk)
+ rendered = view.rendered
+ rendered[:partials][self] += 1 if partial?
+ rendered[:template] ||= []
+ rendered[:template] << self
+ render_without_tracking(view, locals, &blk)
end
end
@@ -68,9 +70,8 @@ module ActionView
def initialize
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
-
+
@params = {}
- send(:initialize_current_url)
end
end
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index 346fa09414..f1c93e6b1e 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -536,7 +536,6 @@ class FragmentCachingTest < ActionController::TestCase
@controller.params = @params
@controller.request = @request
@controller.response = @response
- @controller.send(:initialize_current_url)
@controller.send(:initialize_template_class, @response)
@controller.send(:assign_shortcuts, @request, @response)
end
diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb
index f3500fca34..b626063df4 100644
--- a/actionpack/test/dispatch/request_test.rb
+++ b/actionpack/test/dispatch/request_test.rb
@@ -427,7 +427,7 @@ class RequestTest < ActiveSupport::TestCase
request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
request.expects(:parameters).at_least_once.returns({})
- assert_equal with_set(Mime::XML, Mime::HTML), request.formats
+ assert_equal with_set(Mime::XML, Mime::HTML, Mime::ALL), request.formats
end
with_accept_header false do
@@ -460,7 +460,7 @@ protected
end
def with_set(*args)
- args + Mime::SET
+ args
end
def with_accept_header(value)
diff --git a/actionpack/test/fixtures/test/_customer_with_var.erb b/actionpack/test/fixtures/test/_customer_with_var.erb
index 3379246b7e..c28824936b 100644
--- a/actionpack/test/fixtures/test/_customer_with_var.erb
+++ b/actionpack/test/fixtures/test/_customer_with_var.erb
@@ -1 +1 @@
-<%= customer.name %> <%= object.name %> <%= customer_with_var.name %> \ No newline at end of file
+<%= customer.name %> <%= customer.name %> <%= customer_with_var.name %> \ No newline at end of file