Skip to content

Instantly share code, notes, and snippets.

@jtroxel
Created January 28, 2013 23:12
Show Gist options
  • Save jtroxel/4660169 to your computer and use it in GitHub Desktop.
Save jtroxel/4660169 to your computer and use it in GitHub Desktop.
DB history for checkouts (and other stuff)
class CreateUserHistory < ActiveRecord::Migration
def change
create_table :user_history do |t|
t.association :user # Most things we care about should have a user, but nil-able if not
t.string :username # stamped from user.username
t.string :message # Log-like message
t.string :code # Specific token for sorting, or tracking error or status
t.float :timing # Optional for keeping timing like response time
#NOTE: has_many :history_snapshots # snapshot data about relevant objects
t.datetime :created_at
end
end
end
h = UserHistory.create(user: current_user, message: 'starting checkout'() # Direct create on class
h.snapshots << @cart
current_user.history(user: current_user, message: 'starting checkout', snapshots: [@cart]) # Method on User
@cart.history('starting checkout') # Or method on Order
class CreateHistorySnapshots < ActiveRecord::Migration
def change
create_table :history_snapshots do |t|
t.references :subject, :polymorphic => true # subject record id and type (i.e Order)
t.string :snapshot
t.datetime :created_at
end
end
end
@jtroxel
Copy link
Author

jtroxel commented Jan 28, 2013

Also considering taking this a step further and encapsulating all logging (at least in checkouts), ala Freeman: http://www.mockobjects.com/2007/04/test-smell-logging-is-also-feature.html

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