Module: Shoppy::ProductsHelper

Defined in:
app/helpers/shoppy/products_helper.rb

Class Method Summary collapse

Class Method Details

.add_options_groups_to_product(product, options_groups) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/shoppy/products_helper.rb', line 3

def self.add_options_groups_to_product(product, options_groups)
  product.options_groups += options_groups
  product.save
  vs = product.variants
  vs.each do |v|
    options_groups.each do |og|
      v.put_option(og.name, "")
    end
    v.save
  end
  return product
end

.convert_params_groups_to_objects(params) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'app/helpers/shoppy/products_helper.rb', line 55

def self.convert_params_groups_to_objects(params)
   options_ids = params
   options = []
   options_ids.each do |o_id|
     o = OptionsGroup.find_by(id: o_id.to_i)
     (options += [o]) if o
   end
   return options
end

.createVariants_for_product(product, variants_names) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/shoppy/products_helper.rb', line 41

def self.createVariants_for_product(product, variants_names)
  variants_names.each do |vn|
    v = Variant.new
    v.name = vn
    v.product = product
    product.options_groups.each do |h|
      v.put_option(h.name, "")
    end
    v.save
  end
  return product
end

.remove_options_groups_from_product(product, options_groups) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/shoppy/products_helper.rb', line 16

def self.remove_options_groups_from_product(product, options_groups)
  product.options_groups -= options_groups
  product.save
  vs = product.variants
  vs.each do |v|
    options_groups.each do |og|
      v.delete_option(og.name)
    end
    v.save
  end
  return product
end

.update_options_groups_for_product(product, options_groups) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/shoppy/products_helper.rb', line 29

def self.update_options_groups_for_product(product, options_groups)
  Product.transaction do
    original_groups = product.options_groups
    new_groups = options_groups
    added_groups = new_groups - original_groups
    removed_groups = original_groups - new_groups
    puts remove_options_groups_from_product(product, removed_groups)
    puts add_options_groups_to_product(product, added_groups)
    return true
  end
end