Sha256: 386b2c8e537509587b2bae3a038d06876237e10b79924d1951304ba3951bf8d9
Contents?: true
Size: 1.45 KB
Versions: 25
Compression:
Stored size: 1.45 KB
Contents
module Approvable extend ActiveSupport::Concern # Constants APPROVED = "approved" PENDING = "pending" BLOCKED = "blocked" SUSPENDED = "suspended" REMOVED = "removed" STATUS = {"Approved" => APPROVED, "Pending" => PENDING, "Suspended" => SUSPENDED, "Blocked" => BLOCKED, "Removed" => REMOVED} STATUS_REVERSE = {APPROVED => "Approved", PENDING => "Pending", SUSPENDED => "Suspended", BLOCKED => "Blocked", REMOVED => "Removed"} included do validates :status, :presence=> true, :inclusion => {:in => STATUS_REVERSE.keys, :presence_of => :status, :message => "%{value} is not a valid status" } state_machine :status, initial: PENDING do event :approve do transition [PENDING, SUSPENDED] => APPROVED end event :pending do transition [APPROVED, SUSPENDED, BLOCKED, REMOVED] => PENDING end event :suspend do transition [APPROVED, PENDING, BLOCKED] => SUSPENDED end event :block do transition [APPROVED, PENDING, SUSPENDED] => BLOCKED end event :remove do transition [APPROVED, PENDING, SUSPENDED, BLOCKED] => REMOVED end end scope :approved, -> { where(status: APPROVED) } scope :pending, -> { where(status: PENDING) } scope :blocked, -> { where(status: BLOCKED) } scope :suspended, -> { where(status: SUSPENDED) } scope :removed, -> { where(status: REMOVED) } scope :status, lambda { |status| where("LOWER(status)='#{status}'") } end end
Version data entries
25 entries across 25 versions & 1 rubygems