Module: StateMachines::Candidature
- Included in:
- Candidature
- Defined in:
- app/models/state_machines/candidature.rb
Defined Under Namespace
Modules: ClassMethods
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 |
# File 'app/models/state_machines/candidature.rb', line 2 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] after_initialize :set_initial_state 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, :accepted] => :denied end event :quit do transition :accepted => :denied end after_transition do |object, transition| case transition.to when 'accepted' ProjectUser.find_or_create_by_project_id_and_vacancy_id_and_user_id!( project_id: object.vacancy.project_id, vacancy_id: object.vacancy_id, user_id: object.resource_id ) if object.vacancy.limit == object.vacancy.candidatures.accepted.count object.vacancy.close! unless object.vacancy.closed? end when 'denied' # if comming from :accepted then the vacancy offerer has to reopen the vacancy manually end 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 def set_initial_state self.state ||= :new end end end |