Class: Shoppy::VariantsController

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

Direct Known Subclasses

VariantsController

Instance Method Summary collapse

Instance Method Details

#deleteObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/shoppy/variants_controller.rb', line 84

def delete
  product = Product.find_by(id: params[:product_id])
  variant = Variant.find_by(id: params[:variant_id])
  if product && variant && (variant.product == product)
    if variant.destroy
      Log.newEvent("Variant", "Variant named '#{variant.name}' for product '#{product.name}' was deleted", current_admin.name)
      flash[:notice] = "Variant deleted successfully."
    else
      flash[:warning] = "Something went wrong. please try again later."
    end
    if params[:viw] == "show"
      redirect_to "/products/items/#{product.id}/variants#{variant.id}"
    else
      redirect_to "/products/items/#{product.id}/variants"
    end
  else
    page_not_found
  end
end

#editObject



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
# File 'app/controllers/shoppy/variants_controller.rb', line 52

def edit
  product = Product.find_by(id: params[:product_id])
  variant = Variant.find_by(id: params[:variant_id])
  if product && variant && (variant.product == product)
    variant.name                            = params[:name]
    variant.reg_price                       = params[:reg_price]
    variant.promo_price                     = params[:promo_price]
    variant.promo_price_starts_on           = params[:promo_price_starts_on]
    variant.promo_price_ends_on             = params[:promo_price_ends_on]
    variant.points                          = params[:points]
    if params[:options]
      params[:options].each do |pok, poi|
        variant.put_option(pok, OptionsItem.find(poi.to_i).name)
      end
    end
    variant.hash_data = {}
    hash = params[:hash]
    hash.each do |h_index, h_data|
      variant.put_hash(h_data["k"], h_data["v"])
    end
    if variant.save
      Log.newEvent("Variant", "Variant named '#{variant.name}' for product '#{product.name}' was updated", current_admin.name)
      flash[:notice] = "Variant updated successfully"
    else
      flash[:error] = variant.errors.messages
    end
    redirect_to "/products/items/#{product.id}/variants/#{variant.id}"
  else
    page_not_found
  end
end

#indexObject



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

def index
  product = Product.find_by(id: params[:product_id])
  if product
    @product = product
    @variants = product.variants
  else
    page_not_found
  end
end

#newObject



14
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
40
# File 'app/controllers/shoppy/variants_controller.rb', line 14

def new
  product = Product.find_by(id: params[:product_id])
  if product
    variant                             = Variant.new
    variant.product                     = product
    variant.name                        = params[:name]
    variant.reg_price                   = params[:reg_price]
    variant.promo_price                 = params[:promo_price]
    variant.promo_price_starts_on       = params[:promo_price_starts_on]
    variant.promo_price_ends_on         = params[:promo_price_ends_on]
    variant.points                      = params[:points]
    if params[:options]
      params[:options].each do |pok, poi|
        variant.put_option(pok, OptionsItem.find(poi.to_i).name)
      end
    end
    if variant.save
      Log.newEvent("Variant", "Variant named '#{variant.name}' for product '#{product.name}' was created", current_admin.name)
      flash[:notice] = "Variant Added successfully"
    else
      flash[:error] = variant.errors.messages
    end
    redirect_to "/products/items/#{product.id}/variants"
  else
    page_not_found
  end
end

#showObject



42
43
44
45
46
47
48
49
50
# File 'app/controllers/shoppy/variants_controller.rb', line 42

def show
  product = Product.find_by(id: params[:product_id])
  variant = Variant.find_by(id: params[:variant_id])
  if product && variant && (variant.product == product)
    @variant = variant
  else
    page_not_found
  end
end