Module: Templatar::ModelAdditions

Defined in:
lib/templatar/model_additions.rb

Instance Method Summary collapse

Instance Method Details

#has_templateObject

Raises:

  • (StandardError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/templatar/model_additions.rb', line 4

def has_template
  raise StandardError.new('Cannot add has_template to a non-AR model') unless self.respond_to?(:column_names)

  self.send(:define_method, :template?) { @templatar }

  metaclass = class << self; self; end
  metaclass.send(:define_method, :template) do
    @templatar_singleton ||= begin
      t = self.new
      t.instance_variable_set :@templatar, true
      t_metaclass = class << t; self; end
      self.column_names.each do |column|
        t_metaclass.send(:define_method, column) { column.to_sym == :id ? '__ID__' : "#{column}__TEMPLATE__" }
      end
      t
    end
  end
end