Skip to content

Instantly share code, notes, and snippets.

@trek
Created February 16, 2010 23:06
Show Gist options
  • Save trek/306056 to your computer and use it in GitHub Desktop.
Save trek/306056 to your computer and use it in GitHub Desktop.
class CreateShirts < ActiveRecord::Migration
def self.up
create_table :shirts do |t|
t.string :size
t.string :color
t.string :style
t.belongs_to :design
t.belongs_to :order
t.timestamps
end
end
def self.down
drop_table :shirts
end
end
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
t.datetime :paid
t.datetime :shipped
t.belongs_to :customer
t.timestamps
end
end
def self.down
drop_table :orders
end
end
class CreateCustomers < ActiveRecord::Migration
def self.up
create_table :customers do |t|
t.string :first_name
t.string :last_name
t.boolean :artist
t.timestamps
end
end
def self.down
drop_table :customers
end
end
class CreateDesigns < ActiveRecord::Migration
def self.up
create_table :designs do |t|
t.string :image
t.belongs_to :customer
t.timestamps
end
end
def self.down
drop_table :designs
end
end
class Customer < ActiveRecord::Base
Customer.has_many(:designs) # as a designer
Customer.has_many(:orders)
end
class Design < ActiveRecord::Base
Design.has_many(:shirts)
Design.belongs_to(:customer) # the person who designed it
Design.has_many(:orders, {:through => :shirts})
end
class DesignsController < ApplicationController
def index
end
def show
end
end
class ItemsController < ApplicationController
def new
end
def create
end
end
class Order < ActiveRecord::Base
Order.belongs_to(:customer)
Order.has_many(:items)
end
class OrdersController < ApplicationController
def new
end
def new
end
def create
end
end
ActionController::Routing::Routes.draw do |map|
map.site_root('/', {
:controller => 'designs',
:action => 'index',
:conditions => {:method => :get}
})
map.design('/designs/:permalink', {
:controller => 'designs',
:action => 'show',
:conditions => {:method => :get}
})
map.new_item('/buy/:design_permalink', {
:controller => 'items',
:action => 'new',
:conditions => {:method => :get}
})
map.add_item_to_cart('/items', {
:controller => 'items',
:action => 'create',
:conditions => {:method => :post}
})
map.shopping_cart('/checkout', {
:controller => 'orders',
:action => 'new',
:conditions => {:method => :get}
})
map.finalize_order('/orders', {
:controller => 'orders',
:action => 'create',
:conditions => {:method => :post}
})
end
class Shirt < ActiveRecord::Base
Shirt.belongs_to(:design)
Shirt.belongs_to(:order)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment