class Need < ActiveRecord::Base include Workflow scope :shared, -> { where(state: ["verified", "negociating", "negociated", "failed"]) } scope :top, -> { joins('left join wishes on wishes.need_id = needs.id') .select('needs.*, count(wishes.id) as wishes_count') .group("needs.id") .order("wishes_count DESC") } workflow_column :state max_paginates_per 10 acts_as_paranoid has_many :wishes has_many :customers, -> { uniq }, through: :wishes validates :title, :presence => true, length: {within: 4..128} validates :description, presence: true, length: {maximum: 65535} belongs_to :author, class_name: 'Customer' # Need's workflow lifecycle workflow do state :created do event :validate, :transitions_to => :verified event :refuse, :transitions_to => :refused end state :refused state :verified do event :negociate, :transitions_to => :negociating end state :negociating do event :accept, :transitions_to => :negociated event :reject, :transitions_to => :failed end state :negociated state :failed end # Human state conversion def human_state case state when 'created' "En attente de validation" when 'verified' "Validé" when 'refused' "Refusé" when 'negociating' "En cours de negociation" when 'negociated' "Négociation effecutée" when 'failed' "Négociation échouée" else "Inconnu" end end end