Module: AlterEgo::ActiveRecordAdapter

Defined in:
lib/alter_ego/active_record_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
# File 'lib/alter_ego/active_record_adapter.rb', line 4

def self.included klass
  klass.after_initialize do |r|
    r.state = r.state
  end
end

Instance Method Details

#after_initializeObject

rails needs this method to be defined for the class method after_initialize to be able to take a block this is because of some performance optimization



13
# File 'lib/alter_ego/active_record_adapter.rb', line 13

def after_initialize; end

#stateObject



22
23
24
25
26
27
# File 'lib/alter_ego/active_record_adapter.rb', line 22

def state
  result = ( read_attribute("state") || self.class.default_state)
  result = result.to_sym unless result.nil?
  assert(result.nil? || self.class.states.keys.include?(result))
  result
end

#state=(identifier) ⇒ Object

Override methods from AlterEgo to store state in @attributes instead of @state



17
18
19
20
# File 'lib/alter_ego/active_record_adapter.rb', line 17

def state=(identifier)
  # state needs to always be stored as a string - symbol serialization is strange
  write_attribute("state", identifier.to_s)  unless identifier.nil?
end