Module: ActiveRecord::Fsm::ClassMethods

Defined in:
lib/activerecord/fsm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fsm_graphObject

Returns the value of attribute fsm_graph.



8
9
10
# File 'lib/activerecord/fsm.rb', line 8

def fsm_graph
  @fsm_graph
end

Instance Method Details

#fsm_define(transitions) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/activerecord/fsm.rb', line 10

def fsm_define(transitions)
  self.class_eval do
    private

    def fsm_graph_check_permit_transitions
      unless self.class.fsm_graph.valid_transition?(*self.status_change)
        self.errors.add(:status, 'no permit status change')
        throw :abort
      end
    end
  end

  ActiveRecord::Fsm::Graph.defined_klasses << self
  self.fsm_graph = ActiveRecord::Fsm::Graph.new(transitions)

  validates :status, presence: true, inclusion: { in: self.fsm_graph.states }
  validate :fsm_graph_check_permit_transitions, on: :update, if: :will_save_change_to_status?

  self.fsm_graph
end