Skip to content

Commit

Permalink
Helpers with an initial test
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Mar 4, 2009
1 parent cde9aab commit 6001cea
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 44 deletions.
3 changes: 3 additions & 0 deletions actionpack/lib/action_controller/abstract/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def self.process(action)
new.process(action) new.process(action)
end end


def self.inherited(klass)
end

def initialize def initialize
self.response_obj = {} self.response_obj = {}
end end
Expand Down
55 changes: 55 additions & 0 deletions actionpack/lib/action_controller/abstract/helpers.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,55 @@
module AbstractController
module Helpers

def self.included(klass)
klass.class_eval do
extend ClassMethods
unless self < ::AbstractController::Renderer
raise "You need to include AbstractController::Renderer before including " \
"AbstractController::Helpers"
end
extlib_inheritable_accessor :master_helper_module
self.master_helper_module = Module.new
end
end

def _action_view
av = super
av.helpers.send(:include, master_helper_module)
end

module ClassMethods
def inherited(klass)
klass.master_helper_module = Module.new
klass.master_helper_module.__send__ :include, master_helper_module

super
end

def add_template_helper(mod)
master_helper_module.module_eval { include mod }
end

def helper_method(*meths)
meths.flatten.each do |meth|
master_helper_module.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
def #{meth}(*args, &blk)
controller.send(%(#{meth}), *args, &blk)
end
ruby_eval
end
end

def helper(*args, &blk)
args.flatten.each do |arg|
case arg
when Module
add_template_helper(arg)
end
end
master_helper_module.module_eval(&blk) if block_given?
end
end

end
end
21 changes: 1 addition & 20 deletions actionpack/test/abstract_controller/abstract_controller_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,23 +1,4 @@
$:.unshift(File.dirname(__FILE__) + '/../../lib') require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")
$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib')

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

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'
require 'action_controller/abstract/layouts'


module AbstractController module AbstractController
module Testing module Testing
Expand Down
22 changes: 1 addition & 21 deletions actionpack/test/abstract_controller/callbacks_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,24 +1,4 @@
$:.unshift(File.dirname(__FILE__) + '/../../lib') require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")
$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib')

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

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'
require 'action_controller/abstract/layouts'
require 'action_controller/abstract/callbacks'


module AbstractController module AbstractController
module Testing module Testing
Expand Down
39 changes: 39 additions & 0 deletions actionpack/test/abstract_controller/helper_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,39 @@
require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")

module AbstractController
module Testing

class ControllerWithHelpers < AbstractController::Base
include Renderer
include Helpers

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

module HelperyTest
def included_method
"Included"
end
end

class MyHelpers1 < ControllerWithHelpers
helper(HelperyTest) do
def helpery_test
"World"
end
end

def index
render "helper_test.erb"
end
end

class TestHelpers < ActiveSupport::TestCase
def test_helpers
result = MyHelpers1.process(:index)
assert_equal "Hello World : Included", result.response_obj[:body]
end
end

end
end
22 changes: 22 additions & 0 deletions actionpack/test/abstract_controller/test_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,22 @@
$:.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'
require 'action_view/base'

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'
require 'action_controller/abstract/layouts'
require 'action_controller/abstract/callbacks'
require 'action_controller/abstract/helpers'
1 change: 1 addition & 0 deletions actionpack/test/abstract_controller/views/helper_test.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
Hello <%= helpery_test %> : <%= included_method %>
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def #{ivar}
# @todo We need a style for class_eval <<-HEREDOC. I'd like to make it # @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. # class_eval(<<-RUBY, __FILE__, __LINE__), but we should codify it somewhere.
def extlib_inheritable_writer(*ivars) def extlib_inheritable_writer(*ivars)
instance_writer = ivars.pop[:instance_writer] if ivars.last.is_a?(Hash) instance_writer = ivars.pop[:writer] if ivars.last.is_a?(Hash)
ivars.each do |ivar| ivars.each do |ivar|
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{ivar}=(obj) def self.#{ivar}=(obj)
Expand All @@ -213,7 +213,7 @@ def #{ivar}=(obj) self.class.#{ivar} = obj end
# #
# @api public # @api public
def extlib_inheritable_accessor(*syms) def extlib_inheritable_accessor(*syms)
class_inheritable_reader(*syms) extlib_inheritable_reader(*syms)
class_inheritable_writer(*syms) extlib_inheritable_writer(*syms)
end end
end end

0 comments on commit 6001cea

Please sign in to comment.