tap/app/controllers/products_controller.rb

54 lines
1.1 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
2015-03-28 13:36:07 +00:00
respond_to :html, :js
2014-11-24 20:45:32 +00:00
def create
if @product.save
flash[:success] = "Product created!"
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 20:45:32 +00:00
else
@product = Product.new
@product.barcodes.build(code: params[:barcode])
render 'products/link'
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
render 'products_list/listview' if current_user.admin?
2014-11-25 12:09:55 +00:00
end
2014-11-24 20:45:32 +00:00
def edit
2015-04-03 21:56:13 +00:00
respond_with @product
2014-11-24 20:45:32 +00:00
end
2014-11-25 12:41:22 +00:00
def update
2015-03-28 13:36:07 +00:00
@product.update_attributes product_params
respond_to do |format|
format.js { respond_with @product }
2015-09-26 09:57:41 +00:00
format.html { redirect_to barcode_products_path, notice: "Stock has been updated!" }
end
2014-11-25 12:41:22 +00:00
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, :calories, :deleted, :barcode, barcodes_attributes: [:code])
2014-11-24 20:45:32 +00:00
end
end