tap/app/controllers/products_controller.rb

73 lines
1.8 KiB
Ruby
Raw Normal View History

2016-03-08 15:53:50 +01:00
# == Schema Information
#
# Table name: products
#
# id :integer not null, primary key
# name :string not null
# price_cents :integer default(0), not null
# created_at :datetime
# updated_at :datetime
# avatar_file_name :string
# avatar_content_type :string
# avatar_file_size :integer
# avatar_updated_at :datetime
# category :integer default(0)
# stock :integer default(0), not null
# calories :integer
# deleted :boolean default(FALSE)
#
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
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 21:45:32 +01:00
else
@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
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
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!" }
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
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