31f7de8d60a6278b8e5d5d2570daa45fb892e7b6
[feedcatcher.git] / vendor / rails / actionmailer / lib / action_mailer / helpers.rb
1 require 'active_support/dependencies'
2
3 module ActionMailer
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
9
10 # Extend base with class methods to declare helpers.
11 base.extend(ClassMethods)
12
13 base.class_eval do
14 # Wrap inherited to create a new master helper module for subclasses.
15 class << self
16 alias_method_chain :inherited, :helper
17 end
18
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
22 end
23 end
24
25 module ClassMethods
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}"
31 end
32
33 # Declare a helper:
34 # helper :foo
35 # requires 'foo_helper' and includes FooHelper in the template class.
36 # helper FooHelper
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 }
41 # does all three.
42 def helper(*args, &block)
43 args.flatten.each do |arg|
44 case arg
45 when Module
46 add_template_helper(arg)
47 when String, Symbol
48 file_name = arg.to_s.underscore + '_helper'
49 class_name = file_name.camelize
50
51 begin
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)
57 end
58
59 add_template_helper(class_name.constantize)
60 else
61 raise ArgumentError, 'helper expects String, Symbol, or Module argument'
62 end
63 end
64
65 # Evaluate block in template class if given.
66 master_helper_module.module_eval(&block) if block_given?
67 end
68
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)
78 end
79 end_eval
80 end
81 end
82
83 # Declare a controller attribute as a helper. For example,
84 # helper_attr :name
85 # attr_accessor :name
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}=") }
90 end
91
92 private
93 def inherited_with_helper(child)
94 inherited_without_helper(child)
95 begin
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")
101 end
102 end
103 end
104
105 private
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
110 end
111 end
112 end
113 end