Sha256: 99708d4f6ceb96c45488884fdfa5d67fdf57b4cc8c69b3cb052ebc59f533cce9

Contents?: true

Size: 1019 Bytes

Versions: 8

Compression:

Stored size: 1019 Bytes

Contents

module StateMachines::Candidature
  def self.included(base)
    base.extend ClassMethods
    
    base.class_eval do
      attr_accessor :current_user
      
      const_set 'STATES', [:new, :accepted, :denied]
      const_set 'EVENTS', [:accept, :deny, :quit]
      
      state_machine :state, initial: :new do
        event :accept do
          transition [:new, :denied] => :accepted
        end
        
        state :accepted do
          validate :candidatures_limit_not_reached
        end
        
        event :deny do
          transition :new => :denied
        end
        
        event :quit do
          transition :accepted => :denied
        end
      end
      
      private
      
      # state validations
      def candidatures_limit_not_reached
        if vacancy.limit == vacancy.candidatures.where(state: 'accepted').count
          errors[:state] << I18n.t('activerecord.errors.models.vacancy.attributes.limit.reached')
        end
      end
    end
  end
  
  module ClassMethods
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

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