Created
January 28, 2013 23:12
-
-
Save jtroxel/4660169 to your computer and use it in GitHub Desktop.
DB history for checkouts (and other stuff)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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