Module: StateMachines::Story

Included in:
Story
Defined in:
app/models/state_machines/story.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
10
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/state_machines/story.rb', line 2

def self.included(base)
  base.extend ClassMethods
  
  base.class_eval do
    include Model::MongoDb::StateVersionAttributes
    
    attr_accessor :current_user
    
    const_set 'STATES', [:new, :tasks_defined, :active, :completed, :closed]
    const_set 'EVENTS', [:initialization, :setup_tasks, :activate, :complete]
    
    after_initialize :set_initial_state
    
    state_machine :state, initial: :new do
      event :initialization do
        transition :new => :initialized
      end
      
      event :setup_tasks do
        transition :initialized => :tasks_defined  
      end
      
      state :tasks_defined do
        #validates_associated :tasks
        validate :presence_of_tasks
      end
      
      state :active do
        validate :presence_of_tasks
      end
      
      event :activate do
        transition [:new, :tasks_defined, :completed] => :active
      end
      
      event :deactivate do
        transition :active => :tasks_defined
      end
      
      event :complete do
        transition :active => :completed
      end
      
      event :close do
        transition :completed => :closed
      end
      
      before_transition do |object, transition|
        object.event = transition.event.to_s
        object.state_before = transition.from
      end
    end
    
    private
    
    def set_initial_state
      self.state ||= :new
    end
    
    def presence_of_tasks
      self.tasks.delete_if do |t| 
        t.name.blank? && (t.class.name == 'Task' && t.text.blank?)
      end
      
      if tasks.select{|t| !t.valid?}.any?
        errors[:base] << I18n.t(
          'activerecord.errors.models.story.attributes.base.invalid_tasks'
        )
      end
      
      if tasks.none?
        errors[:base] << I18n.t(
          'activerecord.errors.models.story.attributes.base.missing_tasks'
        )
      end
    end
  end
end