-
-
Save kivanio/d3739377f226b353b025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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