Module: 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



3
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/citier/class_methods.rb', line 3

def acts_as_citier(options = {})

  # 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

  set_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
    set_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))

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

    after_save :updatetype

    citier_debug("Root Class")

    set_table_name "#{table_name}"

    citier_debug("table_name -> #{self.table_name}")
    #returns the root class (the highest inherited class before ActiveRecord)
    def self.root_class
      if(self.superclass!=ActiveRecord::Base)
      self.superclass.root_class
      else
      return self
      end
    end

    def self.find(*args) #overrides find to get all attributes
      tuples = super

      # in case of many objects, return an array of them, reloaded to pull in inherited attributes
      return tuples.map{|x| x.reload} if tuples.kind_of?(Array)

      # in case of only one tuple, return it reloaded.
      # Can't use reload as would loop inifinitely, so do a search by id instead.
      # Probably a nice way of cleaning this a bit
      return tuples.class.where(tuples.class[:id].eq(tuples.id))[0]
    end

    # Unlike destroy_all it is useful to override this method.
    # In fact destroy_all will explicitly call a destroy method on each object
    # whereas delete_all doesn't and only calls specific SQL requests.
    # To be even more precise call delete_all with special conditions
    def self.delete_all
      #call delete method for each instance of the class
      self.all.each{|o| o.delete }
    end

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