Skip to content

Instantly share code, notes, and snippets.

@rocodev-tech
rocodev-tech / create_order.rb
Last active August 29, 2015 14:02
create_order.rb
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
t.integer :user_id
t.integer :total
t.boolean :paid, :default => false
t.timestamps
end
end
end
@rocodev-tech
rocodev-tech / create_order_info.rb
Created June 13, 2014 04:00
create_order_info.rb
class CreateOrderInfos < ActiveRecord::Migration
def change
create_table :order_infos do |t|
t.integer :order_id
t.string :billing_name
t.string :billing_address
t.string :shipping_name
t.string :shipping_address
t.timestamps
@rocodev-tech
rocodev-tech / create_order_item.rb
Created June 13, 2014 04:00
create_order_item.rb
class CreateOrderItems < ActiveRecord::Migration
def change
create_table :order_items do |t|
t.string :product_name
t.float :price
t.integer :quantity
t.integer :order_id
t.timestamps
end
end
@rocodev-tech
rocodev-tech / orders_controller.rb
Last active August 29, 2015 14:02
app/controllers/orders_controller.rb
class OrdersController < ApplicationController
before_action :authenticate_user!
def create
@order = current_user.orders.build(order_params)
if @order.save
@order.build_item_cache_from_cart(current_cart)
@order.calculate_total!(current_cart)
@rocodev-tech
rocodev-tech / order.rb
Created June 13, 2014 04:09
app/models/order.rb
class Order < ActiveRecord::Base
belongs_to :user
has_many :items, :class_name => "OrderItem", :dependent => :destroy
has_one :info, :class_name => "OrderInfo", :dependent => :destroy
accepts_nested_attributes_for :info
def build_item_cache_from_cart(cart)
@rocodev-tech
rocodev-tech / order_info.rb
Created June 13, 2014 04:30
app/models/order_info.rb
class OrderInfo < ActiveRecord::Base
belongs_to :order
validates :billing_name , :presence => true
validates :billing_address , :presence => true
validates :shipping_name , :presence => true
validates :shipping_address , :presence => true
end
@rocodev-tech
rocodev-tech / show.html.erb
Last active August 29, 2015 14:02
app/views/orders/show.html.erb
<div class="row">
<div class="col-md-12">
<h2> 訂單明細 </h2>
<table class="table table-bordered">
<thead>
<tr>
<th width="80%">商品明細</th>
<th>單價</th>
@rocodev-tech
rocodev-tech / orders_controller.rb
Created June 13, 2014 06:14
app/controllers/orders_controller.rb
def show
@order = current_user.orders.find_by_token(params[:id])
@order_info = @order.info
@order_items = @order.items
end
@rocodev-tech
rocodev-tech / notify_order_placed.html.erb
Last active August 29, 2015 14:02
app/views/order_mailer/notify_order_placed.html.erb
<link rel="stylesheet" type="text/css" href="/assets/application.css" />
<div class="row">
<div class="col-md-12">
<h2> 訂單明細 </h2>
<table class="table table-borderd">
<thead>
<tr>
@rocodev-tech
rocodev-tech / order_mailer.rb
Created June 13, 2014 09:32
app/mailers/order_mailer.rb
class OrderMailer < ActionMailer::Base
default from: "[email protected]"
def notify_order_placed(order)
@order = order
@user = order.user
@order_items = @order.items
@order_info = @order.info