Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from eLafo/state_machine_example.rb
Created June 22, 2014 21:59
Show Gist options
  • Save kivanio/d3739377f226b353b025 to your computer and use it in GitHub Desktop.
Save kivanio/d3739377f226b353b025 to your computer and use it in GitHub Desktop.
module SamyRoad
module FeedsEntryStatus
extend ActiveSupport::Concern
STATES = {
ignored: 1,
pending: 0,
published: 2,
erased: 3
}
included do
STATES.each_pair do |state, value|
scope state, where(status: value)
end
state_machine :status, :initial => :pending do
after_transition any - :pending => :pending, do: :set_as_pending
after_transition :pending => any - :pending, do: :unset_as_pending
STATES.each_pair do |s, value|
state s, value: value
end
event :publish do
transition [:pending, :ignored, :erased] => :published
end
event :ignore do
transition :pending => :ignored
end
event :pending do
transition :ignored => :pending
end
event :erase do
transition :published => :erased
end
end
end
module ClassMethods
def states
SamyRoad::FeedsEntryStatus::STATES.keys
end
end
def set_as_pending
notify_observers(:after_set_as_pending)
end
def unset_as_pending
notify_observers(:after_unset_as_pending)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment