helpers.rb 1.2 KB
Newer Older
Y
Yehuda Katz 已提交
1 2
module AbstractController
  module Helpers
3 4 5 6 7
    depends_on Renderer
    
    setup do
      extlib_inheritable_accessor :master_helper_module
      self.master_helper_module = Module.new
Y
Yehuda Katz 已提交
8
    end
Y
Yehuda Katz 已提交
9

Y
Yehuda Katz 已提交
10
    def _action_view
Y
Yehuda Katz 已提交
11 12 13 14 15
      @_action_view ||= begin
        av = super
        av.helpers.send(:include, master_helper_module)
        av
      end
Y
Yehuda Katz 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    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