Module: Citier::RootInstanceMethods

Includes:
InstanceMethods
Defined in:
lib/citier/root_instance_methods.rb

Instance Method Summary collapse

Methods included from InstanceMethods

included

Instance Method Details

#acts_as_citier?Boolean

For testing whther we are using the framework or not

Returns:

  • (Boolean)


35
36
37
# File 'lib/citier/root_instance_methods.rb', line 35

def acts_as_citier?
  true
end

#as_childObject

Instantiates the instance as it’s lowest root class. Used when destroying a root class to make sure we’re not leaving children behind



8
9
10
11
# File 'lib/citier/root_instance_methods.rb', line 8

def as_child
  #instance_class = Object.const_get(self.type)
  return bottom_class_instance = Kernel.const_get(self.type).where(:id => self.id).first
end

#as_rootObject

Access the root class if ever you need.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/citier/root_instance_methods.rb', line 14

def as_root
   if !self.is_root?
     root_class = self.class.base_class

     #get the attributes of the class which are inherited from it's parent.
     attributes_for_parent = self.attributes.reject{|key,value| !root_class.column_names.include?(key) }

     #create a new instance of the superclass, passing the inherited attributes.
     parent = root_class.new(attributes_for_parent)
     parent.id = self.id
     parent.type = self.type
   
     parent.is_new_record(new_record?)

     parent
   else
      self #just return self if we are the root
   end
end

#is_root?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/citier/root_instance_methods.rb', line 39

def is_root?
  self.class.superclass==ActiveRecord::Base
end