1 require 'active_support/dependencies'
4 module Helpers
#:nodoc:
5 def self.included(base
) #:nodoc:
6 # Initialize the base module to aggregate its helpers.
7 base
.class_inheritable_accessor
:master_helper_module
8 base
.master_helper_module
= Module
.new
10 # Extend base with class methods to declare helpers.
11 base
.extend(ClassMethods
)
14 # Wrap inherited to create a new master helper module for subclasses.
16 alias_method_chain
:inherited, :helper
19 # Wrap initialize_template_class to extend new template class
20 # instances with the master helper module.
21 alias_method_chain
:initialize_template_class, :helper
26 # Makes all the (instance) methods in the helper module available to templates rendered through this controller.
27 # See ActionView::Helpers (link:classes/ActionView/Helpers.html) for more about making your own helper modules
28 # available to the templates.
29 def add_template_helper(helper_module
) #:nodoc:
30 master_helper_module
.module_eval
"include #{helper_module}"
35 # requires 'foo_helper' and includes FooHelper in the template class.
37 # includes FooHelper in the template class.
38 # helper { def foo() "#{bar} is the very best" end }
39 # evaluates the block in the template class, adding method +foo+.
40 # helper(:three, BlindHelper) { def mice() 'mice' end }
42 def helper(*args
, &block
)
43 args
.flatten
.each
do |arg
|
46 add_template_helper(arg
)
48 file_name
= arg
.to_s
.underscore
+ '_helper'
49 class_name
= file_name
.camelize
52 require_dependency(file_name
)
53 rescue LoadError
=> load_error
54 requiree
= / -- (.*?)(\.rb)?$/.match(load_error
.message
).to_a
[1]
55 msg
= (requiree
== file_name
) ? "Missing helper file helpers/#{file_name}.rb" : "Can't load file: #{requiree}"
56 raise LoadError
.new(msg
).copy_blame
!(load_error
)
59 add_template_helper(class_name
.constantize
)
61 raise ArgumentError
, 'helper expects String, Symbol, or Module argument'
65 # Evaluate block in template class if given.
66 master_helper_module
.module_eval(&block
) if block_given
?
69 # Declare a controller method as a helper. For example,
70 # helper_method :link_to
71 # def link_to(name, options) ... end
72 # makes the link_to controller method available in the view.
73 def helper_method(*methods
)
74 methods
.flatten
.each
do |method
|
75 master_helper_module
.module_eval
<<-end_eval
76 def #{method}(*args, &block)
77 controller.__send__(%(#{method}), *args, &block)
83 # Declare a controller attribute as a helper. For example,
86 # makes the name and name= controller methods available in the view.
87 # The is a convenience wrapper for helper_method.
88 def helper_attr(*attrs
)
89 attrs
.flatten
.each
{ |attr
| helper_method(attr
, "#{attr}=") }
93 def inherited_with_helper(child
)
94 inherited_without_helper(child
)
96 child
.master_helper_module
= Module
.new
97 child
.master_helper_module
.__send__(:include, master_helper_module
)
98 child
.helper child
.name
.to_s
.underscore
99 rescue MissingSourceFile
=> e
100 raise unless e
.is_missing
?("helpers/#{child.name.to_s.underscore}_helper")
106 # Extend the template class instance with our controller's helper module.
107 def initialize_template_class_with_helper(assigns
)
108 returning(template
= initialize_template_class_without_helper(assigns
)) do
109 template
.extend self.class.master_helper_module