Module: Templatr::ActsAsTemplatable::ActMethod

Defined in:
lib/templatr/acts_as_templatable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_templatable(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/templatr/acts_as_templatable.rb', line 4

def acts_as_templatable(options = {})
  extend Templatr::ActsAsTemplatable::ClassMethods
  include Templatr::ActsAsTemplatable::InstanceMethods

  Templatr::ActsAsTemplatable::HelperMethods.create_field_class(self)
  Templatr::ActsAsTemplatable::HelperMethods.create_tag_class(self)
  Templatr::ActsAsTemplatable::HelperMethods.create_template_class(self)

  TagFieldValue.belongs_to tag_class(false).underscore.to_sym, :foreign_key => :tag_id

  FieldValue.has_many tag_class(false).tableize.to_sym, :dependent => :destroy # Destroy all single select tags with this field value
  FieldValue.has_many :"single_value_#{name.tableize}", :source => name.underscore.to_sym, :through => tag_class(false).tableize.to_sym

  FieldValue.has_many :"multi_value_#{tag_class(false).tableize}", :source => tag_class(false).underscore.to_sym, :through => :tag_field_values, :dependent => :destroy # Destroy all multi select tags with this field value
  FieldValue.has_many :"multi_value_#{name.tableize}", :source => name.underscore.to_sym, :through => :"multi_value_#{tag_class(false).tableize}"

  FieldValue.send(:define_method, name.tableize) do
    if field.select_one?
      single_value_items
    elsif field.select_multiple?
      multi_value_items
    end
  end

  class_eval do
    belongs_to :template, :class_name => template_class
    delegate :template_fields, :to => :template

    has_many :tags, :class_name => tag_class, :foreign_key => :taggable_id, :order => 'templatr_tags.name ASC', :dependent => :destroy
    accepts_nested_attributes_for :tags, :allow_destroy => true

    class_attribute :dynamic_facets
    self.dynamic_facets = []
  end
end