Class: Shoppy::ProductsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Shoppy::ProductsController
- Defined in:
- app/controllers/shoppy/products_controller.rb
Direct Known Subclasses
Instance Method Summary collapse
Instance Method Details
#delete ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/controllers/shoppy/products_controller.rb', line 86 def delete @product = Product.find_by(id: params[:product_id]) if @product @product.destroy Log.newEvent("Products", "Product named '#{@product.name}' was updated", current_admin.name) flash[:notice] = "product has been deleted." redirect_to '/products/items' else page_not_found end end |
#edit ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/controllers/shoppy/products_controller.rb', line 51 def edit product = Product.find_by(id: params[:product_id]) category = Category.find(params[:category][:id]) if !params[:category][:id].empty? brand = Brand.find(params[:brand][:id]) if !params[:brand][:id].empty? manufacturer = Manufacturer.find(params[:manufacturer][:id]) if !params[:manufacturer][:id].empty? if product if category && brand && manufacturer Product.transaction do product.name = params[:name] product.category = category product.brand = brand product.manufacturer = manufacturer = ProductsHelper::convert_params_groups_to_objects(params[:options_groups][:ids]) ProductsHelper::(product, ) product.hash_data = {} hash = params[:hash] hash.each do |h_index, h_data| product.put_hash(h_data["k"], h_data["v"]) end if product.save Log.newEvent("Products", "Product named '#{product.name}' was updated", current_admin.name) flash[:notice] = "product updated successfully" else flash[:warning] = "Something went wrong. Try again later" end end else flash[:warning] = "Please select a Category, a Brand, and a Manufacturer for the product" end redirect_to "/products/items/" + product.id.to_s else page_not_found end end |
#index ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'app/controllers/shoppy/products_controller.rb', line 5 def index @products = Product.all @options = [] = OptionsGroup.all.order("name ASC") .each do |o| @options += [o.name] end end |
#new ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/controllers/shoppy/products_controller.rb', line 15 def new category = Category.find(params[:category][:id]) if !params[:category][:id].empty? brand = Brand.find(params[:brand][:id]) if !params[:brand][:id].empty? manufacturer = Manufacturer.find(params[:manufacturer][:id]) if !params[:manufacturer][:id].empty? if brand && category && manufacturer Product.transaction do @product = Product.new @product.name = params[:name] @product.category = Category.find(params[:category][:id]) if params[:category] @product.brand = Brand.find(params[:brand][:id]) if params[:brand] @product.manufacturer = Manufacturer.find(params[:manufacturer][:id]) if params[:manufacturer] if @product.save Log.newEvent("Products", "Product named '#{@product.name}' was created", current_admin.name) flash[:notice] = "Product has been created successfully." = ProductsHelper::convert_params_groups_to_objects(params[:options_groups][:ids]) ProductsHelper::(@product, ) else flash[:error] = @product.errors. end end else flash[:warning] = "Please select a Category, a Brand, and a Manufacturer for the product" end redirect_to "/products/items" end |
#show ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'app/controllers/shoppy/products_controller.rb', line 41 def show product = Product.find_by(id: params[:product_id]) if product @product = product else page_not_found end end |