Skip to content

Instantly share code, notes, and snippets.

@jlsuttles
Created February 14, 2012 18:59
Show Gist options
  • Save jlsuttles/1829189 to your computer and use it in GitHub Desktop.
Save jlsuttles/1829189 to your computer and use it in GitHub Desktop.
state machines confuse me
class Person < ActiveRecord::Base
state_machine :state, :initial => :authed do
event :filled_out_profile do
transition :authed => :unaccepted
end
event :admin_accepted do
transition :unaccepted => :accepted
end
state :authed, :unaccepted, :accepted do
validates :name, presence: true
validates :linkedin_token, presence: true
validates :linkedin_image_url, presence: true
end
state :unaccepted, :accepted do
validates :email, presence: true
end
state :accepted do
validates :is_accepted, presence: true, inclusion: { in: [true] }
validates :type, presence: true, inclusion: { in: TYPES }
end
state :authed do
def update_profile_attributes(attributes)
if update_attributes(attributes) && filled_out_profile!
In::PersonMailer.new(self).deliver
true
end
rescue
false
end
end
state :unaccepted do
def update_profile_attributes(attributes)
if update_attributes(attributes) && admin_accepted!
In::PersonMailer.accepted(self).deliver if email.present?
true
end
rescue
false
end
end
state :accepted do
def update_profile_attributes(attributes)
update_attributes(attributes)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment