Methods
Public Instance methods
Declare a helper:
helper :foo
requires ‘foo_helper’ and includes FooHelper in the template class.
helper FooHelper
includes FooHelper in the template class.
helper { def foo() "#{bar} is the very best" end }
evaluates the block in the template class, adding method foo.
helper(:three, BlindHelper) { def mice() 'mice' end }
does all three.
[ show source ]
# File lib/action_mailer/helpers.rb, line 44
44: def helper(*args, &block)
45: args.flatten.each do |arg|
46: case arg
47: when Module
48: add_template_helper(arg)
49: when String, Symbol
50: file_name = arg.to_s.underscore + '_helper'
51: class_name = file_name.camelize
52:
53: begin
54: require_dependency(file_name)
55: rescue LoadError => load_error
56: requiree = / -- (.*?)(\.rb)?$/.match(load_error).to_a[1]
57: msg = (requiree == file_name) ? "Missing helper file helpers/#{file_name}.rb" : "Can't load file: #{requiree}"
58: raise LoadError.new(msg).copy_blame!(load_error)
59: end
60:
61: add_template_helper(class_name.constantize)
62: else
63: raise ArgumentError, 'helper expects String, Symbol, or Module argument'
64: end
65: end
66:
67: # Evaluate block in template class if given.
68: master_helper_module.module_eval(&block) if block_given?
69: end
Declare a controller attribute as a helper. For example,
helper_attr :name attr_accessor :name
makes the name and name= controller methods available in the view. The is a convenience wrapper for helper_method.
[ show source ]
# File lib/action_mailer/helpers.rb, line 91
91: def helper_attr(*attrs)
92: attrs.flatten.each { |attr| helper_method(attr, "#{attr}=") }
93: end
Declare a controller method as a helper. For example,
helper_method :link_to def link_to(name, options) ... end
makes the link_to controller method available in the view.
[ show source ]
# File lib/action_mailer/helpers.rb, line 75
75: def helper_method(*methods)
76: methods.flatten.each do |method|
77: master_helper_module.module_eval "def \#{method}(*args, &block)\ncontroller.send(%(\#{method}), *args, &block)\nend\n"
78: end
79: end
Private Instance methods
[ show source ]
# File lib/action_mailer/helpers.rb, line 96
96: def inherited_with_helper(child)
97: inherited_without_helper(child)
98: begin
99: child.master_helper_module = Module.new
100: child.master_helper_module.send :include, master_helper_module
101: child.helper child.name.underscore
102: rescue MissingSourceFile => e
103: raise unless e.is_missing?("helpers/#{child.name.underscore}_helper")
104: end
105: end