Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Dealing with AR pollution
module CoderetreatLive::Coderetreats
# this Coderetreat gets loaded instead of the AR
class Coderetreat
include States
end
describe "The states a coderetreat can be in" do
let(:retreat) { Coderetreat.new }
example "Going through the day of a coderetreat" do
retreat.should be_not_started
retreat.start!
retreat.should be_started
retreat.do_introduction!
retreat.should be_doing_introduction
retreat.start_session!
retreat.should be_in_session
retreat.stop_session!
retreat.should be_on_break
retreat.go_to_lunch!
retreat.should be_on_lunch
retreat.do_closing_circle!
retreat.should be_in_closing_circle
retreat.end_coderetreat!
retreat.should be_finished
end
end
end
require 'state_machine'
module CoderetreatLive
module Coderetreats
module States
TRANSITIONS = {
not_started: :not_started,
start: :started,
do_introduction: :doing_introduction,
start_session: :in_session,
stop_session: :on_break,
go_to_lunch: :on_lunch,
do_closing_circle: :in_closing_circle,
end_coderetreat: :finished
}
def self.state_names
TRANSITIONS.values
end
def self.included(klass, *_)
klass.state_machine :state, initial: :not_started do
TRANSITIONS.each_pair do |event_name, ending_state|
event event_name do
transition all => ending_state
end
end
end
end
end
end
end
Owner

coreyhaines commented Mar 21, 2012

This doesn't show the pollution exactly and could be dealt with in alternate ways. However, I assume people can extrapolate how this works when you would load up the Coderetreat AR model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment