aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-04-20 15:30:41 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2008-04-20 15:30:41 -0700
commit3b93ca221d3a1b777bd880c18f52010cbd56c18c (patch)
tree7b49713994125de123775102eac2655d7b29b231 /actionpack/lib/action_controller
parentdaab53d8c0bca1114c1485d0dcc52858a264ab9e (diff)
parent46ab7422d9ebac0d529f71a3a7c2feaf0b9d5dbd (diff)
downloadrails-3b93ca221d3a1b777bd880c18f52010cbd56c18c.tar.gz
rails-3b93ca221d3a1b777bd880c18f52010cbd56c18c.tar.bz2
rails-3b93ca221d3a1b777bd880c18f52010cbd56c18c.zip
Merge commit 'rails/master'
Diffstat (limited to 'actionpack/lib/action_controller')
-rwxr-xr-xactionpack/lib/action_controller/base.rb20
-rw-r--r--actionpack/lib/action_controller/caching/pages.rb16
-rw-r--r--actionpack/lib/action_controller/dispatcher.rb77
-rw-r--r--actionpack/lib/action_controller/filters.rb6
-rw-r--r--actionpack/lib/action_controller/layout.rb4
-rw-r--r--actionpack/lib/action_controller/rescue.rb4
6 files changed, 55 insertions, 72 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index 63ad4d042a..c0f3122fbf 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
@@ -330,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
@@ -870,7 +864,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]
@@ -1105,7 +1099,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
@@ -1223,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
@@ -1267,15 +1260,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/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb
index 4307f39583..7aa6ce154b 100644
--- a/actionpack/lib/action_controller/caching/pages.rb
+++ b/actionpack/lib/action_controller/caching/pages.rb
@@ -36,7 +36,7 @@ module ActionController #:nodoc:
# == Setting the cache directory
#
# The cache directory should be the document root for the web server and is set using Base.page_cache_directory = "/document/root".
- # For Rails, this directory has already been set to RAILS_ROOT + "/public".
+ # For Rails, this directory has already been set to Rails.public_path (which is usually set to RAILS_ROOT + "/public").
#
# == Setting the cache extension
#
@@ -46,7 +46,7 @@ module ActionController #:nodoc:
def self.included(base) #:nodoc:
base.extend(ClassMethods)
base.class_eval do
- @@page_cache_directory = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/public" : ""
+ @@page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : ""
cattr_accessor :page_cache_directory
@@page_cache_extension = '.html'
@@ -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/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb
index 92576bdb2b..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)
@@ -22,7 +46,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.
@@ -67,25 +91,11 @@ module ActionController
end
cattr_accessor :error_file_path
- self.error_file_path = "#{::RAILS_ROOT}/public" if defined? ::RAILS_ROOT
-
- cattr_accessor :unprepared
- self.unprepared = true
+ self.error_file_path = Rails.public_path if defined?(Rails.public_path)
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/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|
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 b91115a93c..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'
@@ -153,7 +153,7 @@ module ActionController #:nodoc:
# If the file doesn't exist, the body of the response will be left empty.
def render_optional_error_file(status_code)
status = interpret_status(status_code)
- path = "#{RAILS_ROOT}/public/#{status[0,3]}.html"
+ path = "#{Rails.public_path}/#{status[0,3]}.html"
if File.exist?(path)
render :file => path, :status => status
else