tap/app/controllers/products_controller.rb

61 lines
1.2 KiB
Ruby
Raw Normal View History

2014-11-24 20:45:32 +00:00
class ProductsController < ApplicationController
2014-12-17 06:31:51 +00:00
load_and_authorize_resource
2014-11-24 20:45:32 +00:00
def new
@product = Product.new
end
def create
2014-11-25 12:41:22 +00:00
@product = Product.new(product_params)
2014-11-24 20:45:32 +00:00
if @product.save
2014-12-09 13:39:27 +00:00
redirect_to action: :index
2014-11-24 20:45:32 +00:00
else
2014-12-09 13:39:27 +00:00
render :new
2014-11-24 20:45:32 +00:00
end
end
2014-11-25 12:09:55 +00:00
def index
@products = Product.all
2014-12-10 21:06:36 +00:00
@categories = Product.categories
2014-11-25 12:09:55 +00:00
end
2014-11-24 20:45:32 +00:00
def edit
2014-11-25 12:09:55 +00:00
@product = Product.find(params[:id])
2014-11-24 20:45:32 +00:00
end
2014-11-25 12:41:22 +00:00
def update
@product = Product.find(params[:id])
if @product.update_attributes(product_params)
2014-12-09 13:42:50 +00:00
flash[:success] = "Succesfully updated product"
2014-12-09 13:39:27 +00:00
redirect_to action: :index
2014-11-25 12:41:22 +00:00
else
render 'edit'
end
end
2014-11-26 10:58:51 +00:00
def destroy
Product.find(params[:id]).destroy
2014-12-09 13:42:50 +00:00
flash[:success] = "Succesfully removed product"
redirect_to action: :index
2014-11-24 20:45:32 +00:00
end
2015-02-08 09:11:23 +00:00
def stock
@products = Product.all
end
def update_stock
@products = Product.all
@products.each do |product|
stock_inc = params[:products][product.id.to_s][:stock_inc].to_i
product.increment!(:stock, stock_inc) if stock_inc > 0
end
redirect_to products_path
end
2014-11-24 20:45:32 +00:00
private
2014-11-25 12:41:22 +00:00
def product_params
params.require(:product).permit(:name, :price, :avatar, :category, :stock)
2014-11-24 20:45:32 +00:00
end
end