Class: ActionTexter::Base

Inherits:
AbstractController::Base
  • Object
show all
Includes:
AbstractController::AssetPaths, AbstractController::Helpers, AbstractController::Layouts, AbstractController::Logger, AbstractController::Rendering, AbstractController::Translation, DeliveryMethods
Defined in:
lib/action_texter/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeliveryMethods

#wrap_delivery_behavior!

Constructor Details

#initialize(method_name = nil, *args) ⇒ Base

Returns a new instance of Base.



70
71
72
73
74
# File 'lib/action_texter/base.rb', line 70

def initialize(method_name = nil, *args)
  super()
  @_message = Message.new
  process(method_name, *args) if method_name
end

Class Attribute Details

.texter_nameObject



45
46
47
# File 'lib/action_texter/base.rb', line 45

def texter_name
  @texter_name ||= name.underscore
end

Class Method Details

.controller_pathObject



49
50
51
# File 'lib/action_texter/base.rb', line 49

def texter_name
  @texter_name ||= name.underscore
end

.default(value = nil) ⇒ Object



51
52
53
54
# File 'lib/action_texter/base.rb', line 51

def default(value = nil)
  self.default_params = default_params.merge(value).freeze if value
  default_params
end

.register_interceptor(interceptor) ⇒ Object

Register an Interceptor which will be called before mail is sent. Either a class or a string can be passed in as the Interceptor. If a string is passed in it will be constantized.



40
41
42
43
# File 'lib/action_texter/base.rb', line 40

def register_interceptor(interceptor)
  delivery_interceptor = (interceptor.is_a?(String) ? interceptor.constantize : interceptor)
  Mail.register_interceptor(delivery_interceptor)
end

.register_interceptors(*interceptors) ⇒ Object

Register one or more Interceptors which will be called before mail is sent.



25
26
27
# File 'lib/action_texter/base.rb', line 25

def register_interceptors(*interceptors)
  interceptors.flatten.compact.each { |interceptor| register_interceptor(interceptor) }
end

.register_observer(observer) ⇒ Object

Register an Observer which will be notified when mail is delivered. Either a class or a string can be passed in as the Observer. If a string is passed in it will be constantized.



32
33
34
35
# File 'lib/action_texter/base.rb', line 32

def register_observer(observer)
  delivery_observer = (observer.is_a?(String) ? observer.constantize : observer)
  Mail.register_observer(delivery_observer)
end

.register_observers(*observers) ⇒ Object

Register one or more Observers which will be notified when mail is delivered.



20
21
22
# File 'lib/action_texter/base.rb', line 20

def register_observers(*observers)
  observers.flatten.compact.each { |observer| register_observer(observer) }
end

.respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/action_texter/base.rb', line 56

def respond_to?(method, include_private = false)
  super || action_methods.include?(method.to_s)
end

Instance Method Details

#each_template(paths, name, &block) ⇒ Object



108
109
110
111
# File 'lib/action_texter/base.rb', line 108

def each_template(paths, name, &block)
  templates = lookup_context.find_all(name, Array.wrap(paths))
  templates.uniq { |t| t.formats }.each(&block)
end

#process(*args) ⇒ Object



76
77
78
79
# File 'lib/action_texter/base.rb', line 76

def process(*args)
  lookup_context.skip_default_locale!
  super
end

#text(headers = {}, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/action_texter/base.rb', line 85

def text(headers={}, &block)
  m = @_message

  wrap_delivery_behavior!(headers.delete(:delivery_method))

  # Call all the procs (if any)
  default_values = self.class.default.merge(self.class.default) do |k,v|
    v.respond_to?(:call) ? v.bind(self).call : v
  end
  headers = headers.reverse_merge(default_values)
  headers.each do |k, v|
    m[k] = v
  end

  templates_path = self.class.texter_name
  templates_name = action_name

  each_template(templates_path, templates_name) do |template|
    m.body ||= render(:template => template)
  end
  m
end

#texter_nameObject



81
82
83
# File 'lib/action_texter/base.rb', line 81

def texter_name
  self.class.texter_name
end