Class: Shoppy::ProductsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/shoppy/products_controller.rb

Direct Known Subclasses

ProductsController

Instance Method Summary collapse

Instance Method Details

#deleteObject



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

#editObject



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
          options = ProductsHelper::convert_params_groups_to_objects(params[:options_groups][:ids])
          ProductsHelper::update_options_groups_for_product(product, options)
          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

#indexObject



5
6
7
8
9
10
11
12
13
# File 'app/controllers/shoppy/products_controller.rb', line 5

def index
  @products = Product.all
  @options = []

  options = OptionsGroup.all.order("name ASC")
  options.each do |o|
    @options += [o.name]
  end
end

#newObject



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."
        options = ProductsHelper::convert_params_groups_to_objects(params[:options_groups][:ids])
        ProductsHelper::add_options_groups_to_product(@product, options)
      else
        flash[:error]   = @product.errors.messages
      end
    end
  else
    flash[:warning] = "Please select a Category, a Brand, and a Manufacturer for the product"
  end
  redirect_to "/products/items"
end

#showObject



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