Module: Hifsm::Adapters::ActiveRecordAdapter::ClassMethods

Defined in:
lib/hifsm/adapters/active_record_adapter.rb

Instance Method Summary collapse

Instance Method Details

#hifsm(column, &block) ⇒ Object



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
# File 'lib/hifsm/adapters/active_record_adapter.rb', line 11

def hifsm(column, &block)
  super

  before_save "hifsm_write_#{column}_attribute"

  send("#{column}_machine_definition").all_states.each do |st|
    state_name = st.to_s
    scope_name = state_name.gsub('.', '_')
    if st.sub_fsm
      scope scope_name, lambda { where(arel_table[column].matches(state_name + '.%')) }
    else
      scope scope_name, lambda { where(column => state_name) }
    end
  end

  define_method "#{column}=" do |value|
    raise 'not (sure will be) implemented'
  end

  define_method "initial_#{column}" do
    read_attribute(column)
  end

  define_method "hifsm_write_#{column}_attribute" do
    write_attribute(column, send(column))
  end
end