Skip to content

Instantly share code, notes, and snippets.

@andrzejkrzywda
Last active December 14, 2015 18:19
Show Gist options
  • Save andrzejkrzywda/5128269 to your computer and use it in GitHub Desktop.
Save andrzejkrzywda/5128269 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
protect_from_forgery
def current_buyer
if cookies[:buyer_id].present? && (buyer = Buyer.find(cookies[:buyer_id]))
@current_buyer = buyer
else
@current_buyer = Buyer.create
cookies[:buyer_id] = { value: @current_buyer.id, expires: 1.week.from_now }
end
@current_buyer
end
helper_method :current_buyer
end
class CreateBuyersAndOrders < ActiveRecord::Migration
def up
create_table "buyers", :force => true do |t|
t.timestamps
end
create_table "orders", :force => true do |t|
t.integer "buyer_id"
t.boolean "completed", :default => false
t.datetime "ordered_at"
t.timestamps
end
create_table "order_items", :force => true do |t|
t.float "price"
t.integer "product_id"
t.integer "order_id"
t.integer "quantity", :default => 1
t.timestamps
end
end
def down
drop_table :order_items
drop_table :orders
drop_table :buyers
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment