class EmailTemplate::JModels::MailTemplate

Attributes

prepared[RW]

Public Instance Methods

as_html(attrs={}) click to toggle source
# File lib/email_template/j_models.rb, line 30
def as_html(attrs={})
  prepare(attrs)
end
as_text(attrs={}) click to toggle source
# File lib/email_template/j_models.rb, line 34
def as_text(attrs={})
  strip_tags(prepare(attrs))
  #sanitize(prepare(attrs), :tags => %w(a), :attributes => %w(href))
end
prepare(attrs={}) click to toggle source
# File lib/email_template/j_models.rb, line 16
def prepare(attrs={})
  @prepared ||= lambda{ |attrs|
    attrs.stringify_keys!
    body.gsub(/\#{.*?}/) do |match|
      elem, action = match[2..-2].split('.')
      if (action)
        attrs[elem].try(action.to_sym) rescue match.to_s
      else
        attrs[elem] || match.to_s
      end
    end
  }.call(attrs)
end
prepare_fields() click to toggle source
# File lib/email_template/j_models.rb, line 39
def prepare_fields
  possible_attrs = []
  self.classes.each do |resource_class|
    current_class = resource_class.downcase.match(/\w*/mix).to_a.first
    if (current_class[0] == '_')
      possible_attrs << obj(current_class.from(1))
    else
      const_obj = current_class.camelize.constantize
      [:find_attributes, :find_columns, :find_methods].each do |method|
        possible_attrs += self.send(method, current_class, const_obj)
      end
    end
  end
  possible_attrs.uniq
end

Protected Instance Methods

find_attributes(classname, object) click to toggle source
# File lib/email_template/j_models.rb, line 62
def find_attributes(classname, object)
  if object.respond_to?(:attributes)
    finder(classname, object.attributes, :first, attr_pattern(classname))
  end || []
end
find_columns(classname, object) click to toggle source
# File lib/email_template/j_models.rb, line 68
def find_columns(classname, object)
  if object.respond_to?(:columns)
    finder(classname, object.columns, :name, column_pattern(classname))
  end || []
end
find_devise_methods(object) click to toggle source
# File lib/email_template/j_models.rb, line 80
def find_devise_methods(object)
  res = []
  res << obj('confirmation_token')    if object.respond_to? :confirmation_token
  res << obj('reset_password_token')  if object.respond_to? :reset_password_token
  res << obj('unlock_token')          if object.respond_to? :unlock_token
  res
end
find_methods(classname, object) click to toggle source
# File lib/email_template/j_models.rb, line 74
def find_methods(classname, object)
  object.public_instance_methods.each_with_object([]) do |m_alias, ret|
    (ret << obj(classname, m_alias.to_s.from(methods_header.length))) if m_alias.to_s.start_with?(methods_header)
  end
end
finder(classname, items, val, current_pattern) click to toggle source
# File lib/email_template/j_models.rb, line 56
def finder(classname, items, val, current_pattern)
  items.each_with_object([]) do |attr, ret|
    (ret << obj(classname, attr.send(val))) if ((attr.send(val) =~ /#{current_pattern}/).nil? || current_pattern.nil?)
  end
end

Private Instance Methods

attr_pattern(object) click to toggle source
# File lib/email_template/j_models.rb, line 105
def attr_pattern(object)
  pattern(attributes_black_list, object)
end
column_pattern(object) click to toggle source
# File lib/email_template/j_models.rb, line 101
def column_pattern(object)
  pattern(columns_black_list, object)
end
obj(clas, name = nil) click to toggle source
# File lib/email_template/j_models.rb, line 109
def obj(clas, name = nil)
  "\#{#{[clas, name].compact.join('.')}}"
end
pattern(elems, object) click to toggle source
# File lib/email_template/j_models.rb, line 97
def pattern(elems, object)
  elems.is_a?(Array) ? regex_from_list(elems) : regex_from_hash(elems, object)
end
regex_from_hash(hash, object) click to toggle source
# File lib/email_template/j_models.rb, line 93
def regex_from_hash(hash, object)
  regex_from_list(hash[object] || hash['*'] || [])
end
regex_from_list(list) click to toggle source
# File lib/email_template/j_models.rb, line 89
def regex_from_list(list)
  list.blank? ? nil : "(" + list.join(")|(") + ")"
end