Module: Citier::ClassMethods

Defined in:
lib/citier/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_citier(options = {}) ⇒ Object

any method placed here will apply to classes



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
39
40
41
42
43
44
45
46
47
48
# File 'lib/citier/class_methods.rb', line 4

def acts_as_citier(options = {})
  set_acts_as_citier(true)

  # Option for setting the inheritance columns, default value = 'type'
  db_type_field = (options[:db_type_field] || :type).to_s

  #:table_name = option for setting the name of the current class table_name, default value = 'tableized(current class name)'
  table_name = (options[:table_name] || self.name.tableize.gsub(/\//,'_')).to_s

  self.inheritance_column = "#{db_type_field}"

  if(self.superclass!=ActiveRecord::Base)
    # Non root-class

    citier_debug("Non Root Class")
    citier_debug("table_name -> #{table_name}")

    # Set up the table which contains ALL attributes we want for this class
    self.table_name = "view_#{table_name}"

    citier_debug("tablename (view) -> #{self.table_name}")

    # The the Writable. References the write-able table for the class because
    # save operations etc can't take place on the views
    self.const_set("Writable", create_class_writable(self))
    
    after_initialize do
      self.id = nil if self.new_record? && self.id == 0
    end

    # Add the functions required for children only
    send :include, Citier::ChildInstanceMethods
  else
  # Root class

    citier_debug("Root Class")

    self.table_name = "#{table_name}"

    citier_debug("table_name -> #{self.table_name}")

    # Add the functions required for root classes only
    send :include, Citier::RootInstanceMethods
  end
end