Skip to content

Instantly share code, notes, and snippets.

View obrie's full-sized avatar

Aaron Pfeifer obrie

View GitHub Profile
class CreditCardType < ActiveRecord::Base
acts_as_enumeration
column :display_name
# defaults :name => :default_name # Uses the active_record_defaults plugin
def initialize(attributes = nil)
super
self.name = default_name unless attributes && attributes.include?(:name)
end
class CreditCardType < ActiveRecord::Base
acts_as_enumeration :lookup_name
column :name
def initialize(attributes = nil)
super
self.lookup_name ||= name.to_s.gsub(/[^A-Za-z0-9-]/, '').underscore
end
create :id => 1, :name => 'Visa'
# Solution in 0.9.2
state_machine :integration => nil do
...
end
class User < ActiveRecord::Base
state_machine :initial => :pending do
# Event to call each time user proceeds to the next step
event :proceed do
transition :pending => :personal_info_registered
transition :personal_info_registered => :payment_info_registered
transition :payment_info_registered => :activated
end
# Attributes that must be present in at least Step 1
@obrie
obrie / vehicle.rb
Created March 19, 2011 18:27
Demonstrates a possible technique for running events / defining callbacks
class Vehicle
attr_accessor :running
state_machine :initial => :parked do
after_transition {|object, transition| object.send(transition.event)}
event :ignite do
transition :parked => :idling
end
end
@obrie
obrie / state_machine_model.rb
Created May 5, 2011 16:55 — forked from mghaught/state_machine_model.rb
state_machine setup
state_machine :checkout_state, :initial => :address, :use_transactions => false do
state :delivery
state :payment
state :confirm
state :complete
event :next do
transition :from => :address, :to => :delivery
transition :from => :delivery, :to => :payment
transition :from => :payment, :to => :confirm
@obrie
obrie / gist:1128744
Created August 5, 2011 23:12
state_machine alternate syntax support
diff --git a/lib/state_machine/condition_proxy.rb b/lib/state_machine/condition_proxy.rb
index a738d02..53ae3c1 100644
--- a/lib/state_machine/condition_proxy.rb
+++ b/lib/state_machine/condition_proxy.rb
@@ -51,9 +51,18 @@ module StateMachine
include EvalHelpers
# Creates a new proxy to the given class, merging in the given condition
- def initialize(klass, condition)
- @klass = klass
@obrie
obrie / 1. vmstat output during RabbitMQ anomaly
Created October 11, 2012 17:33
system data during RabbitMQ anomaly
# see http://linux.die.net/man/8/vmstat
r b swpd free buff cache si so bi bo in cs us sy id wa
Thu Oct 11 17:42:25 2012: 11 0 0 9617072 13156 168244 0 0 0 41828 16175 17291 11 3 77 8
Thu Oct 11 17:42:26 2012: 2 1 0 9548672 13156 170124 0 0 0 25492 11297 11707 17 10 66 7
Thu Oct 11 17:42:27 2012: 1 7 0 9514320 13164 170576 0 0 0 7413 6212 6348 22 9 15 54
Thu Oct 11 17:42:28 2012: 1 1 0 9493192 13164 170596 0 0 0 109 4339 4114 22 12 50 16
Thu Oct 11 17:42:29 2012: 2 12 0 9483008 13164 170892 0 0 0 505 6746 6331 29 5 13 53
Thu Oct 11 17:42:30 2012: 2 6 0 9414176 13164 170816 0 0 0 8 9120 9462 17 9 22 51
Thu Oct 11 17:42:31 2012: 2 2 0 9483776 13164 171692 0 0 0 1801 4152 3614 23 8 32 37
Take the following log for example:
Completed 204 No Content in 280ms
Started GET "/some/url?..."
Processing by MythicalContentController#index as HTML
Parameters: {...}
Doing some stuff
Completed 204 No Content in 41ms
Doing some more stuff
Started GET "/some/url?..."
Processing by MythicalContentController#index as HTML
Take the following log for example:
Completed 204 No Content in 280ms
Started GET "/some/url?..."
Processing by MythicalContentController#index as HTML
Parameters: {...}
Doing some stuff
Completed 204 No Content in 41ms
Doing some more stuff
Started GET "/some/url?..."
Processing by MythicalContentController#index as HTML