Skip to content

Instantly share code, notes, and snippets.

@yoshikischmitz
Last active January 3, 2016 22:19
Show Gist options
  • Save yoshikischmitz/8527277 to your computer and use it in GitHub Desktop.
Save yoshikischmitz/8527277 to your computer and use it in GitHub Desktop.
Order Management System Modes
class Brand < ActiveRecord::Base
has_many :products
validates :name, presence: true
has_attached_file :logo, :styles => { :medium => "300x300>", :thumb => "100x100>" }
has_attached_file :header, styles: {medium: "x200"}
end
class BrandsController < ApplicationController
def new
@brand = Brand.new
@brands = Brand.all
end
def create
@brand = Brand.new(brand_params)
if @brand.save
redirect_to @brand
else
render 'new'
end
end
def show
@brands = Brand.all
@brand = Brand.find(params[:id])
end
def edit
@brand = Brand.find(params[:id])
end
def update
@brand = Brand.find(params[:id])
@brand.update(brand_params)
redirect_to @brand
end
def index
@brands = Brand.all
end
def add_products
@brands = Brand.all
@brand = Brand.find(params[:id])
end
private #!!!!!!!!!!!!!!!!!
def brand_params
params.require(:brand).permit(:name, :address, :contact, :phone_numer, :description, :logo, :header)
end
end
class Order < ActiveRecord::Base
belongs_to :user
has_many :order_line_items
accepts_nested_attributes_for :order_line_items
end
class OrderLineItem < ActiveRecord::Base
belongs_to :product
belongs_to :order
def product
return Product.find(self.product_id)
end
end
<%= form_tag("/orders?brand=#{@brand.id}", method: "post" ) do %>
<h1><%= @brand.name %> Order Form</h1>
<table class="table">
<tr>
<th>Name</th>
<th>sku</th>
<th>MSRP</th>
<th>Size Info</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>
<% @brand.products.each do |product| %>
<tr class="targetfields">
<td><%= product.name %></td>
<td><%= product.sku %></td>
<td>$<span class="rate"><%=product.price_retail%></span></td>
<td style="overflow:hidden;" ><%= product.size_info %></td>
<td><%= number_field_tag product.id, product.name %></td>
<td class="subtotal">$0.00</td>
</tr>
<% end %>
<td id="total"></td>
</tr>
</table>
<button id="submit" class="btn btn-large" type="submit" >
Submit
</button>
<% end %>
class OrdersController < ApplicationController
def new
@brand = Brand.find(params[:id])
end
def create
@brand = Brand.find(params[:brand])
@order = Order.new(user: current_user, brand_id: params[:brand])
@brand.products.each do |x|
order_line_item_qty = params[x.id.to_s.to_sym].to_i
if order_line_item_qty != 0
@order.order_line_items.build(product: x, quantity: order_line_item_qty)
end
end
@order.save!
redirect_to @order
end
def index
@orders = current_user.orders
end
def show
@order = Order.find(params[:id])
end
def edit
@order = Order.find(params[:id])
end
end
<%= form_tag("/orders?brand=#{@brand.id}", method: "post" ) do %>
<h1><%= @brand.name %> Order Form</h1>
<table class="table">
<tr>
<th>Name</th>
<th>sku</th>
<th>MSRP</th>
<th>Size Info</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>
<% @brand.products.each do |product| %>
<tr class="targetfields">
<td><%= product.name %></td>
<td><%= product.sku %></td>
<td>$<span class="rate"><%=product.price_retail%></span></td>
<td style="overflow:hidden;" ><%= product.size_info %></td>
<td><%= number_field_tag product.id, product.name %></td>
<td class="subtotal">$0.00</td>
</tr>
<% end %>
<td id="total"></td>
</tr>
</table>
<button id="submit" class="btn btn-large" type="submit" >
Submit
</button>
<% end %>
class Product < ActiveRecord::Base
belongs_to :brand
validates :name, presence: true
validates :brand_id, presence: true
def self.import(file, brand_id)
CSV.foreach(file.path, headers: true) do |row|
a = row.to_hash
a[:brand_id] = brand_id
Product.create! a
end
end
end
class ProductsController < ApplicationController
def new
@product = Product.new
end
def create
@brand = Brand.find(params[:brand_id])
if params[:product][:file] != nil
Product.import(params[:product][:file], params[:brand_id])
else
@product = @brand.products.create(params[:product].permit!)
end
redirect_to @brand
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment