Class: FSM::StateAttributeInterceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/fsm/state_attribute_interceptor.rb

Class Method Summary collapse

Class Method Details

.add_interceptor(klass) ⇒ Object



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

def self.add_interceptor(klass)
  state_attribute_name = Machine[klass].current_state_attribute_name
  hierarchy = klass.ancestors.map {|ancestor| ancestor.to_s}
  
  if hierarchy.include?("ActiveRecord::Base")
    bar(klass)
    
    klass.class_eval do
      before_save :assign_default_state
      define_method(:assign_default_state) do
        if read_attribute(Machine[self.class].current_state_attribute_name).blank?
          self.send("#{Machine[self.class].current_state_attribute_name}=", Machine[self.class].initial_state_name.to_s)
        end
      end
    end
  else 
    foo(klass)
  end
 
end