Skip to content

Instantly share code, notes, and snippets.

@agibralter
Created January 31, 2009 08:20
Show Gist options
  • Save agibralter/d079cdfedb66dff9361f to your computer and use it in GitHub Desktop.
Save agibralter/d079cdfedb66dff9361f to your computer and use it in GitHub Desktop.
class MyMigration < ActiveRecord::Migration
def self.up
# ...
Context.reset_column_information
context = Context.new
context.name = 'urtak'
context.uri = 'urtak'
context.state_status = 'published'
context.save!
Question.reset_column_information
Contextualization.reset_column_information
Question.find(:all, :select => "id, user_id, nuked, r_session_id").each do |q|
c = Contextualization.new
c.context = context
c.question = q
c.user_id = q.user_id
c.state_status_user = 'normal'
c.state_status_admin = q.nuked == true ? 'nuked' : 'approved'
c.save!
end
# ...
end
# ...
end
class Context < ActiveRecord::Base
# ...
state_machine :state_status, :initial => :draft do
event :publish do
transition :to => :published, :from => :draft
end
event :unpublish do
transition :to => :draft, :from => :published
end
event :archive do
transition :to => :archived, :from => :published
end
event :republish do
transition :to => :published, :from => :archived
end
event :nuke do
transition :to => :nuked
end
end
state_machine :state_visibility_context, :initial => :public, :namespace => :visible do
event :become_public do
transition :to => :public
end
event :become_private do
transition :to => :private
end
end
state_machine :state_membership, :initial => :open, :namespace => :membership do
event :become_anonymous do
transition :to => :anonymous
end
event :become_open do
transition :to => :open
end
event :become_closed do
transition :to => :closed
end
end
# ...
end
class Contextualization < ActiveRecord::Base
# ...
state_machine :state_status_admin, :initial => :pending, :namespace => :by_admin do
before_transition :to => :approved, :do => lambda { |c| c.approved = true; true }
before_transition :from => :approved, :do => lambda { |c| c.approved = false; true }
event :approve do
transition :to => :approved, :from => [:pending, :nuked, :archived]
end
event :nuke do
transition :to => :nuked, :from => [:pending, :approved, :archived]
end
event :archive do
transition :to => :archived, :from => [:pending, :approved, :nuked]
end
end
state_machine :state_status_user, :initial => :normal, :namespace => :by_user do
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment