Sha256: df9b53d6dd0af3974a9bb08a18441fb6f2722109dfe4d0388261e19bb928c497

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

module StateMachines::Story
  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]
      
      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
        
        event :activate do
          transition [:tasks_defined, :completed] => :active
        end
        
        event :complete do
          transition :active => :completed
        end
        
        event :close do
          transition :completed => :closed
        end
      end
      
      private
      
      def presence_of_tasks
        unless tasks.any?
          errors[:base] << I18n.t(
            'activerecord.errors.models.story.attributes.base.missing_tasks'
          )
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
voluntary-0.1.0 app/models/state_machines/story.rb
voluntary-0.1.0.rc4 app/models/state_machines/story.rb
voluntary-0.1.0.rc3 app/models/state_machines/story.rb
voluntary-0.1.0.rc2 app/models/state_machines/story.rb
voluntary-0.1.0.rc1 app/models/state_machines/story.rb
voluntary-0.0.3 app/models/state_machines/story.rb
voluntary-0.0.2 app/models/state_machines/story.rb
voluntary-0.0.1 app/models/state_machines/story.rb