aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/metal
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-12-11 19:24:32 -0200
committerJosé Valim <jose.valim@gmail.com>2009-12-11 19:24:32 -0200
commitcf7b94c013d4e824433a018001474e71ddd81a99 (patch)
tree67b90f1e13a88d60170469e281f081711a976194 /actionpack/lib/action_controller/metal
parent6e55b32e98fcaad82184d2e21ee611a3465e4b20 (diff)
parent2297eaed5b195ea42b99d062ad45f87dde9d3c60 (diff)
downloadrails-cf7b94c013d4e824433a018001474e71ddd81a99.tar.gz
rails-cf7b94c013d4e824433a018001474e71ddd81a99.tar.bz2
rails-cf7b94c013d4e824433a018001474e71ddd81a99.zip
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'actionpack/lib/action_controller/metal')
-rw-r--r--actionpack/lib/action_controller/metal/compatibility.rb2
-rw-r--r--actionpack/lib/action_controller/metal/exceptions.rb11
-rw-r--r--actionpack/lib/action_controller/metal/flash.rb24
-rw-r--r--actionpack/lib/action_controller/metal/helpers.rb2
-rw-r--r--actionpack/lib/action_controller/metal/render_options.rb101
-rw-r--r--actionpack/lib/action_controller/metal/rescue.rb (renamed from actionpack/lib/action_controller/metal/rescuable.rb)0
-rw-r--r--actionpack/lib/action_controller/metal/responder.rb9
7 files changed, 67 insertions, 82 deletions
diff --git a/actionpack/lib/action_controller/metal/compatibility.rb b/actionpack/lib/action_controller/metal/compatibility.rb
index 0c264fcd09..a90f798cd5 100644
--- a/actionpack/lib/action_controller/metal/compatibility.rb
+++ b/actionpack/lib/action_controller/metal/compatibility.rb
@@ -1,5 +1,5 @@
module ActionController
- module Rails2Compatibility
+ module Compatibility
extend ActiveSupport::Concern
class ::ActionController::ActionControllerError < StandardError #:nodoc:
diff --git a/actionpack/lib/action_controller/metal/exceptions.rb b/actionpack/lib/action_controller/metal/exceptions.rb
index b9d23da3e0..07024d0a9a 100644
--- a/actionpack/lib/action_controller/metal/exceptions.rb
+++ b/actionpack/lib/action_controller/metal/exceptions.rb
@@ -18,18 +18,9 @@ module ActionController
def initialize(*allowed_methods)
super("Only #{allowed_methods.to_sentence(:locale => :en)} requests are allowed.")
- @allowed_methods = allowed_methods
- end
-
- def allowed_methods_header
- allowed_methods.map { |method_symbol| method_symbol.to_s.upcase } * ', '
- end
-
- def handle_response!(response)
- response.headers['Allow'] ||= allowed_methods_header
end
end
-
+
class NotImplemented < MethodNotAllowed #:nodoc:
end
diff --git a/actionpack/lib/action_controller/metal/flash.rb b/actionpack/lib/action_controller/metal/flash.rb
index f43900faa0..b2d44c6c63 100644
--- a/actionpack/lib/action_controller/metal/flash.rb
+++ b/actionpack/lib/action_controller/metal/flash.rb
@@ -133,8 +133,20 @@ module ActionController #:nodoc:
Array(key || keys).each { |k| used ? @used << k : @used.delete(k) }
return key ? self[key] : self
end
+ end
+
+ # Access the contents of the flash. Use <tt>flash["notice"]</tt> to
+ # read a notice you put there or <tt>flash["notice"] = "hello"</tt>
+ # to put a new one.
+ def flash #:doc:
+ unless @_flash
+ @_flash = session["flash"] || FlashHash.new
+ @_flash.sweep
end
+ @_flash
+ end
+
protected
def process_action(method_name)
super
@@ -146,17 +158,5 @@ module ActionController #:nodoc:
super
@_flash = nil
end
-
- # Access the contents of the flash. Use <tt>flash["notice"]</tt> to
- # read a notice you put there or <tt>flash["notice"] = "hello"</tt>
- # to put a new one.
- def flash #:doc:
- unless @_flash
- @_flash = session["flash"] || FlashHash.new
- @_flash.sweep
- end
-
- @_flash
- end
end
end
diff --git a/actionpack/lib/action_controller/metal/helpers.rb b/actionpack/lib/action_controller/metal/helpers.rb
index b4325e24ad..d0402e5bad 100644
--- a/actionpack/lib/action_controller/metal/helpers.rb
+++ b/actionpack/lib/action_controller/metal/helpers.rb
@@ -52,7 +52,7 @@ module ActionController
included do
# Set the default directory for helpers
extlib_inheritable_accessor(:helpers_dir) do
- defined?(Rails) ? "#{Rails.root}/app/helpers" : "app/helpers"
+ defined?(Rails.root) ? "#{Rails.root}/app/helpers" : "app/helpers"
end
end
diff --git a/actionpack/lib/action_controller/metal/render_options.rb b/actionpack/lib/action_controller/metal/render_options.rb
index 0d69ca10df..b6a7ca0eda 100644
--- a/actionpack/lib/action_controller/metal/render_options.rb
+++ b/actionpack/lib/action_controller/metal/render_options.rb
@@ -1,19 +1,24 @@
module ActionController
+
+ def self.add_renderer(key, &block)
+ RenderOptions.add(key, &block)
+ end
+
module RenderOptions
extend ActiveSupport::Concern
included do
extlib_inheritable_accessor :_renderers
- self._renderers = []
+ self._renderers = {}
end
module ClassMethods
def _write_render_options
- renderers = _renderers.map do |r|
+ renderers = _renderers.map do |name, value|
<<-RUBY_EVAL
- if options.key?(:#{r})
+ if options.key?(:#{name})
_process_options(options)
- return render_#{r}(options[:#{r}], options)
+ return _render_option_#{name}(options[:#{name}], options)
end
RUBY_EVAL
end
@@ -25,79 +30,63 @@ module ActionController
RUBY_EVAL
end
- def _add_render_option(name)
- _renderers << name
+ def use_renderers(*args)
+ args.each do |key|
+ _renderers[key] = RENDERERS[key]
+ end
_write_render_options
end
+ alias use_renderer use_renderers
end
def render_to_body(options)
_handle_render_options(options) || super
end
- end
-
- module RenderOption #:nodoc:
- def self.extended(base)
- base.extend ActiveSupport::Concern
- base.send :include, ::ActionController::RenderOptions
- def base.register_renderer(name)
- included { _add_render_option(name) }
- end
+ RENDERERS = {}
+ def self.add(key, &block)
+ define_method("_render_option_#{key}", &block)
+ RENDERERS[key] = block
+ All._write_render_options
end
- end
- module RenderOptions
- module Json
- extend RenderOption
- register_renderer :json
+ module All
+ extend ActiveSupport::Concern
+ include RenderOptions
- def render_json(json, options)
- json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
- json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
- self.content_type ||= Mime::JSON
- self.response_body = json
+ INCLUDED = []
+ included do
+ self._renderers = RENDERERS
+ _write_render_options
+ INCLUDED << self
end
- end
-
- module Js
- extend RenderOption
- register_renderer :js
- def render_js(js, options)
- self.content_type ||= Mime::JS
- self.response_body = js.respond_to?(:to_js) ? js.to_js : js
+ def self._write_render_options
+ INCLUDED.each(&:_write_render_options)
end
end
- module Xml
- extend RenderOption
- register_renderer :xml
-
- def render_xml(xml, options)
- self.content_type ||= Mime::XML
- self.response_body = xml.respond_to?(:to_xml) ? xml.to_xml : xml
- end
+ add :json do |json, options|
+ json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
+ json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
+ self.content_type ||= Mime::JSON
+ self.response_body = json
end
- module RJS
- extend RenderOption
- register_renderer :update
-
- def render_update(proc, options)
- generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(view_context, &proc)
- self.content_type = Mime::JS
- self.response_body = generator.to_s
- end
+ add :js do |js, options|
+ self.content_type ||= Mime::JS
+ self.response_body = js.respond_to?(:to_js) ? js.to_js : js
end
- module All
- extend ActiveSupport::Concern
+ add :xml do |xml, options|
+ self.content_type ||= Mime::XML
+ self.response_body = xml.respond_to?(:to_xml) ? xml.to_xml : xml
+ end
- include ActionController::RenderOptions::Json
- include ActionController::RenderOptions::Js
- include ActionController::RenderOptions::Xml
- include ActionController::RenderOptions::RJS
+ add :update do |proc, options|
+ generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(view_context, &proc)
+ self.content_type = Mime::JS
+ self.response_body = generator.to_s
end
end
end
diff --git a/actionpack/lib/action_controller/metal/rescuable.rb b/actionpack/lib/action_controller/metal/rescue.rb
index bbca1b2179..bbca1b2179 100644
--- a/actionpack/lib/action_controller/metal/rescuable.rb
+++ b/actionpack/lib/action_controller/metal/rescue.rb
diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb
index 6c76c57839..cb0e600871 100644
--- a/actionpack/lib/action_controller/metal/responder.rb
+++ b/actionpack/lib/action_controller/metal/responder.rb
@@ -80,6 +80,11 @@ module ActionController #:nodoc:
class Responder
attr_reader :controller, :request, :format, :resource, :resources, :options
+ ACTIONS_FOR_VERBS = {
+ :post => :new,
+ :put => :edit
+ }
+
def initialize(controller, resources, options={})
@controller = controller
@request = controller.request
@@ -138,7 +143,7 @@ module ActionController #:nodoc:
def navigation_behavior(error)
if get?
raise error
- elsif has_errors?
+ elsif has_errors? && default_action
render :action => default_action
else
redirect_to resource_location
@@ -209,7 +214,7 @@ module ActionController #:nodoc:
# the verb is post.
#
def default_action
- @action || (request.post? ? :new : :edit)
+ @action ||= ACTIONS_FOR_VERBS[request.method]
end
end
end