Permalink
Browse files

Working toward getting a basic AbstractController framework

  • Loading branch information...
1 parent a2637e9 commit f55514125cae291791365effc856d237008f7cd2 @wycats wycats committed Mar 18, 2009
@@ -1,8 +1,10 @@
module AbstractController
- autoload :Base, "action_controller/abstract/base"
- autoload :Callbacks, "action_controller/abstract/callbacks"
- autoload :Helpers, "action_controller/abstract/helpers"
- autoload :Layouts, "action_controller/abstract/layouts"
- autoload :Logger, "action_controller/abstract/logger"
- autoload :Renderer, "action_controller/abstract/renderer"
+ autoload :Base, "action_controller/abstract/base"
+ autoload :Callbacks, "action_controller/abstract/callbacks"
+ autoload :Helpers, "action_controller/abstract/helpers"
+ autoload :Layouts, "action_controller/abstract/layouts"
+ autoload :Logger, "action_controller/abstract/logger"
+ autoload :Renderer, "action_controller/abstract/renderer"
+ # === Exceptions
+ autoload :ActionNotFound, "action_controller/abstract/exceptions"
end
@@ -17,14 +17,24 @@ def initialize
end
def process(action_name)
+ unless respond_to_action?(action_name)
+ raise ActionNotFound, "The action '#{action_name}' could not be found"
+ end
+
@_action_name = action_name
process_action
self.response_obj[:body] = self.response_body
self
end
+ private
+
def process_action
- send(action_name)
+ respond_to?(action_name) ? send(action_name) : send(:action_missing, action_name)
+ end
+
+ def respond_to_action?(action_name)
+ respond_to?(action_name) || respond_to?(:action_missing, true)
end
end
@@ -0,0 +1,3 @@
+module AbstractController
+ class ActionNotFound < StandardError ; end
+end
@@ -1,5 +1,6 @@
module ActionController
autoload :AbstractBase, "action_controller/new_base/base"
autoload :HideActions, "action_controller/new_base/hide_actions"
+ autoload :Renderer, "action_controller/new_base/renderer"
autoload :UrlFor, "action_controller/new_base/url_for"
end
@@ -1,26 +1,61 @@
module ActionController
class AbstractBase < AbstractController::Base
+
+ # :api: public
attr_internal :request, :response, :params
+ # :api: public
def self.controller_name
@controller_name ||= controller_path.split("/").last
end
+ # :api: public
def controller_name() self.class.controller_name end
-
+
+ # :api: public
def self.controller_path
@controller_path ||= self.name.sub(/Controller$/, '').underscore
end
+ # :api: public
def controller_path() self.class.controller_path end
+ # :api: private
def self.action_methods
@action_names ||= Set.new(self.public_instance_methods - self::CORE_METHODS)
end
+ # :api: private
def self.action_names() action_methods end
+ # :api: private
def action_methods() self.class.action_names end
+
+ # :api: private
def action_names() action_methods end
+
+ # :api: plugin
+ def self.call(env)
+ controller = new
+ controller.call(env).to_rack
+ end
+
+ # :api: plugin
+ def response_body=(body)
+ @_response["Content-Length"] = body.length
+ @_response.body = body
+ end
+
+ # :api: private
+ def call(env)
+ @_request = ActionDispatch::Request.new(env)
+ @_response = ActionDispatch::Response.new
+ process(@_request.parameters[:action])
+ end
+
+ # :api: private
+ def to_rack
+ response.to_a
+ end
end
end
@@ -10,6 +10,12 @@ def self.included(klass)
def action_methods() self.class.action_names end
def action_names() action_methods end
+
+ private
+
+ def respond_to_action?(action_name)
+ !hidden_actions.include?(action_name) && (super || respond_to?(:method_missing))
+ end
module ClassMethods
def hide_action(*args)
@@ -0,0 +1,11 @@
+module ActionController
+ module Renderer
+
+ def render(options)
+ if text = options[:text]
+ self.response_body = text
+ end
+ end
+
+ end
+end
@@ -3,6 +3,10 @@
module AbstractController
module Testing
+ # Test basic dispatching.
+ # ====
+ # * Call process
+ # * Test that the response_body is set correctly
class SimpleController < AbstractController::Base
end
@@ -20,6 +24,8 @@ class TestBasic < ActiveSupport::TestCase
end
end
+ # Test Render mixin
+ # ====
class RenderingController < AbstractController::Base
include Renderer
@@ -58,6 +64,9 @@ class TestRenderer < ActiveSupport::TestCase
end
end
+ # Test rendering with prefixes
+ # ====
+ # * self._prefix is used when defined
class PrefixedViews < RenderingController
private
def self.prefix
@@ -92,6 +101,9 @@ class TestPrefixedViews < ActiveSupport::TestCase
end
end
+ # Test rendering with layouts
+ # ====
+ # self._layout is used when defined
class WithLayouts < PrefixedViews
include Layouts
@@ -136,5 +148,63 @@ class TestLayouts < ActiveSupport::TestCase
end
end
+ # respond_to_action?(action_name)
+ # ====
+ # * A method can be used as an action only if this method
+ # returns true when passed the method name as an argument
+ # * Defaults to true in AbstractController
+ class DefaultRespondToActionController < AbstractController::Base
+ def index() self.response_body = "success" end
+ end
+
+ class ActionMissingRespondToActionController < AbstractController::Base
+ # No actions
+ private
+ def action_missing(action_name)
+ self.response_body = "success"
+ end
+ end
+
+ class RespondToActionController < AbstractController::Base;
+ def index() self.response_body = "success" end
+
+ def fail() self.response_body = "fail" end
+
+ private
+
+ def respond_to_action?(action_name)
+ action_name != :fail
+ end
+
+ end
+
+ class TestRespondToAction < ActiveSupport::TestCase
+
+ def assert_dispatch(klass, body = "success", action = :index)
+ response = klass.process(action).response_obj[:body]
+ assert_equal body, response
+ end
+
+ test "an arbitrary method is available as an action by default" do
+ assert_dispatch DefaultRespondToActionController, "success", :index
+ end
+
+ test "raises ActionNotFound when method does not exist and action_missing is not defined" do
+ assert_raise(ActionNotFound) { DefaultRespondToActionController.process(:fail) }
+ end
+
+ test "dispatches to action_missing when method does not exist and action_missing is defined" do
+ assert_dispatch ActionMissingRespondToActionController, "success", :ohai
+ end
+
+ test "a method is available as an action if respond_to_action? returns true" do
+ assert_dispatch RespondToActionController, "success", :index
+ end
+
+ test "raises ActionNotFound if method is defined but respond_to_action? returns false" do
+ assert_raise(ActionNotFound) { RespondToActionController.process(:fail) }
+ end
+ end
+
end
end
Oops, something went wrong.

0 comments on commit f555141

Please sign in to comment.