Skip to content

Commit

Permalink
First, very early, AbstractController code. More to come
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Feb 25, 2009
1 parent d6b9f84 commit b1f078b
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
23 changes: 23 additions & 0 deletions actionpack/lib/action_controller/abstract/base.rb
@@ -0,0 +1,23 @@
module AbstractController
class Base

attr_internal :response_body
attr_internal :response_obj
cattr_accessor :logger

def self.process(action)
new.process(action)
end

def initialize
self.response_obj = {}
end

def process(action)
send(action)
self.response_obj[:body] = self.response_body
self
end

end
end
25 changes: 25 additions & 0 deletions actionpack/lib/action_controller/abstract/renderer.rb
@@ -0,0 +1,25 @@
module AbstractController
module Renderer

def self.included(klass)
klass.extend ClassMethods
klass.extlib_inheritable_accessor :view_paths
klass.view_paths ||= ActionView::PathSet.new
end

def _action_view
@_action_view ||= ActionView::Base.new(self.class.view_paths, {}, self)
end

def render(template)
tmp = view_paths.find_by_parts(template)
self.response_body = _action_view._render_template_with_layout(tmp)
end

module ClassMethods
def append_view_path(path)
self.view_paths << path
end
end
end
end
2 changes: 2 additions & 0 deletions actionpack/lib/action_view/template/handlers/erb.rb
@@ -1,3 +1,5 @@
require 'erb'

module ActionView module ActionView
module TemplateHandlers module TemplateHandlers
class ERB < TemplateHandler class ERB < TemplateHandler
Expand Down
60 changes: 60 additions & 0 deletions actionpack/test/abstract_controller/abstract_controller_test.rb
@@ -0,0 +1,60 @@
$:.unshift(File.dirname(__FILE__) + '/../../lib')
$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib')

require 'test/unit'
require 'active_support'
require 'active_support/test_case'
require 'action_controller'

begin
require 'ruby-debug'
Debugger.settings[:autoeval] = true
Debugger.start
rescue LoadError
# Debugging disabled. `gem install ruby-debug` to enable.
end

require 'action_controller/abstract/base'
require 'action_controller/abstract/renderer'

module AbstractController
module Testing

class SimpleController < AbstractController::Base
end

class Me < SimpleController
def index
self.response_body = "Hello world"
"Something else"
end
end

class TestBasic < ActiveSupport::TestCase
test "dispatching works" do
result = Me.process(:index)
assert_equal "Hello world", result.response_obj[:body]
end
end

class RenderingController < AbstractController::Base
include Renderer

append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views"))
end

class Me2 < RenderingController
def index
render "index.erb"
end
end

class TestRenderer < ActiveSupport::TestCase
test "rendering templates works" do
result = Me2.process(:index)
assert_equal "Hello from index.erb", result.response_obj[:body]
end
end

end
end
1 change: 1 addition & 0 deletions actionpack/test/abstract_controller/views/index.erb
@@ -0,0 +1 @@
Hello from index.erb
Expand Up @@ -138,3 +138,82 @@ def inherited_with_inheritable_attributes(child)
alias inherited_without_inheritable_attributes inherited alias inherited_without_inheritable_attributes inherited
alias inherited inherited_with_inheritable_attributes alias inherited inherited_with_inheritable_attributes
end end

class Class
# Defines class-level inheritable attribute reader. Attributes are available to subclasses,
# each subclass has a copy of parent's attribute.
#
# @param *syms<Array[#to_s]> Array of attributes to define inheritable reader for.
# @return <Array[#to_s]> Array of attributes converted into inheritable_readers.
#
# @api public
#
# @todo Do we want to block instance_reader via :instance_reader => false
# @todo It would be preferable that we do something with a Hash passed in
# (error out or do the same as other methods above) instead of silently
# moving on). In particular, this makes the return value of this function
# less useful.
def extlib_inheritable_reader(*ivars)
instance_reader = ivars.pop[:reader] if ivars.last.is_a?(Hash)

ivars.each do |ivar|
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{ivar}
return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar})
ivar = superclass.#{ivar}
return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}")
@#{ivar} = ivar && !ivar.is_a?(Module) && !ivar.is_a?(Numeric) && !ivar.is_a?(TrueClass) && !ivar.is_a?(FalseClass) ? ivar.dup : ivar
end
RUBY
unless instance_reader == false
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{ivar}
self.class.#{ivar}
end
RUBY
end
end
end

# Defines class-level inheritable attribute writer. Attributes are available to subclasses,
# each subclass has a copy of parent's attribute.
#
# @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
# define inheritable writer for.
# @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
# @return <Array[#to_s]> An Array of the attributes that were made into inheritable writers.
#
# @api public
#
# @todo We need a style for class_eval <<-HEREDOC. I'd like to make it
# class_eval(<<-RUBY, __FILE__, __LINE__), but we should codify it somewhere.
def extlib_inheritable_writer(*ivars)
instance_writer = ivars.pop[:instance_writer] if ivars.last.is_a?(Hash)
ivars.each do |ivar|
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{ivar}=(obj)
@#{ivar} = obj
end
RUBY
unless instance_writer == false
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{ivar}=(obj) self.class.#{ivar} = obj end
RUBY
end
end
end

# Defines class-level inheritable attribute accessor. Attributes are available to subclasses,
# each subclass has a copy of parent's attribute.
#
# @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
# define inheritable accessor for.
# @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
# @return <Array[#to_s]> An Array of attributes turned into inheritable accessors.
#
# @api public
def extlib_inheritable_accessor(*syms)
class_inheritable_reader(*syms)
class_inheritable_writer(*syms)
end
end

0 comments on commit b1f078b

Please sign in to comment.