2014-11-24 21:45:32 +01:00
|
|
|
class ProductsController < ApplicationController
|
2014-12-17 07:31:51 +01:00
|
|
|
load_and_authorize_resource
|
|
|
|
|
2015-03-28 14:36:07 +01:00
|
|
|
respond_to :html, :js
|
|
|
|
|
2014-11-24 21:45:32 +01:00
|
|
|
def create
|
|
|
|
if @product.save
|
2015-08-31 14:33:15 +02:00
|
|
|
flash[:success] = "Product created!"
|
2015-09-25 14:24:32 +02:00
|
|
|
redirect_to barcode_products_path
|
|
|
|
else
|
|
|
|
render 'link'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def barcode
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_barcode
|
|
|
|
@product = Barcode.find_by(code: params[:barcode]).try(:product)
|
|
|
|
if @product
|
|
|
|
render 'products/stock_entry'
|
2014-11-24 21:45:32 +01:00
|
|
|
else
|
2015-09-25 14:24:32 +02:00
|
|
|
@product = Product.new
|
|
|
|
@product.barcodes.build(code: params[:barcode])
|
|
|
|
render 'products/link'
|
2014-11-24 21:45:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-25 13:09:55 +01:00
|
|
|
def index
|
|
|
|
@products = Product.all
|
2014-12-10 22:06:36 +01:00
|
|
|
@categories = Product.categories
|
2015-08-31 14:33:15 +02:00
|
|
|
|
|
|
|
render 'products_list/listview' if current_user.admin?
|
2014-11-25 13:09:55 +01:00
|
|
|
end
|
|
|
|
|
2014-11-24 21:45:32 +01:00
|
|
|
def edit
|
2015-04-03 23:56:13 +02:00
|
|
|
respond_with @product
|
2014-11-24 21:45:32 +01:00
|
|
|
end
|
|
|
|
|
2014-11-25 13:41:22 +01:00
|
|
|
def update
|
2015-03-28 14:36:07 +01:00
|
|
|
@product.update_attributes product_params
|
2015-09-25 14:24:32 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.js { respond_with @product }
|
2015-09-26 11:57:41 +02:00
|
|
|
format.html { redirect_to barcode_products_path, notice: "Stock has been updated!" }
|
2015-09-25 14:24:32 +02:00
|
|
|
end
|
2014-11-25 13:41:22 +01:00
|
|
|
end
|
|
|
|
|
2014-11-24 21:45:32 +01:00
|
|
|
private
|
|
|
|
|
2014-11-25 13:41:22 +01:00
|
|
|
def product_params
|
2015-09-25 14:24:32 +02:00
|
|
|
params.require(:product).permit(:name, :price, :avatar, :category, :stock, :calories, :deleted, :barcode, barcodes_attributes: [:code])
|
2014-11-24 21:45:32 +01:00
|
|
|
end
|
|
|
|
end
|