Class: Caboose::VariantsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject

POST /admin/products/:id/variants



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/controllers/caboose/variants_controller.rb', line 141

def admin_add
  return if !user_is_allowed('variants', 'add')
  resp = Caboose::StdClass.new(
    :error   => nil,
    :refresh => nil
  )

  p = Product.find(params[:id])
  v = Variant.new(:product_id => p.id)
  v.option1 = p.default1
  v.option2 = p.default2
  v.option3 = p.default3
  v.status  = 'Active'
  v.save
  resp.refresh = true
  render :json => resp
end

#admin_attach_to_imageObject

PUT /admin/variants/:id/attach-to-image



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/controllers/caboose/variants_controller.rb', line 160

def admin_attach_to_image
  render :json => false if !user_is_allowed('variants', 'edit')         
  variant_id = params[:id].to_i
  img = ProductImage.find(params[:product_image_id])

  exists = false
  img.variants.each do |v|
    if v.id == variant_id
      exists = true
      break
    end
  end
  if exists
    render :json => true
    return
  end

  img.variants = [] if img.variants.nil?
  img.variants << Variant.find(variant_id)
  img.save

  render :json => true
end

#admin_deleteObject

DELETE /admin/variants/:id



195
196
197
198
199
200
201
202
203
# File 'app/controllers/caboose/variants_controller.rb', line 195

def admin_delete
  return if !user_is_allowed('variants', 'delete')
  v = Variant.find(params[:id])
  v.status = 'Deleted'
  v.save
  render :json => Caboose::StdClass.new({
    :redirect => "/admin/products/#{v.product_id}/variants"
  })
end

#admin_editObject

GET /admin/variants/:variant_id/edit GET /admin/products/:product_id/variants/:variant_id/edit



34
35
36
37
38
39
# File 'app/controllers/caboose/variants_controller.rb', line 34

def admin_edit
  return if !user_is_allowed('variants', 'edit')    
  @variant = Variant.find(params[:variant_id])
  @product = @variant.product
  render :layout => 'caboose/admin'
end

#admin_groupObject

GET /admin/variants



42
43
44
45
46
47
48
49
50
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
# File 'app/controllers/caboose/variants_controller.rb', line 42

def admin_group
  return if !user_is_allowed('variants', 'edit')
  
  joins  = []
  where  = ''
  values = []
  
  if params[:category_ids]
    joins  << [:category_memberships]
    where  << 'store_category_memberships.category_id IN (?)'
    values << params[:category_ids]
  end
  
  if params[:vendor_ids]
    joins  << [:vendor]
    where  << 'store_vendors.id IN (?)'
    values << params[:vendor_ids]
  end
  
  if params[:title]
    where  << 'LOWER(store_products.title) LIKE ?'
    values << "%#{params[:title].downcase}%"
  end
  
  # Query for all relevant products
  products = values.any? ? Caboose::Product.joins(joins).where([where].concat(values)) : []
  
  # Grab variants for each product
  @variants = products.collect { |product| product.variants }.flatten
  
  # Grab all categories; except for all and uncategorized
  @categories = Caboose::Category.where('parent_id IS NOT NULL')
  
  # Grab all vendors
  @vendors = Caboose::Vendor.all
  
  render layout: 'caboose/admin'
end

#admin_newObject

GET /admin/products/:id/variants/new



132
133
134
135
136
137
138
# File 'app/controllers/caboose/variants_controller.rb', line 132

def admin_new
  return if !user_is_allowed('variants', 'add')
  @top_categories = ProductCategory.where(:parent_id => nil).reorder('name').all
  @product_id = params[:id] 
  @variant = Variant.new
  render :layout => 'caboose/admin'
end

#admin_status_optionsObject

GET /admin/variants/status-options



206
207
208
209
210
211
212
213
214
215
216
# File 'app/controllers/caboose/variants_controller.rb', line 206

def admin_status_options
  arr = ['Active', 'Inactive', 'Deleted']
  options = []
  arr.each do |status|
    options << {
      :value => status,
      :text => status
    }
  end
  render :json => options
end

#admin_unattach_from_imageObject

PUT /admin/variants/:id/unattach-from-image



185
186
187
188
189
190
191
192
# File 'app/controllers/caboose/variants_controller.rb', line 185

def admin_unattach_from_image
  render :json => false if !user_is_allowed('variants', 'edit')
  v = Variant.find(params[:id])
  img = ProductImage.find(params[:product_image_id])       
  img.variants.delete(v)
  img.save
  render :json => true
end

#admin_updateObject

PUT /admin/variants/:id



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/caboose/variants_controller.rb', line 82

def admin_update
  return if !user_is_allowed('variants', 'edit')
  
  resp = Caboose::StdClass.new({'attributes' => {}})
  v = Variant.find(params[:id])    
  
  save = true    
  params.each do |name,value|
    case name        
      when 'alternate_id'
        v.alternate_id = value
      when 'sku'
        v.sku = value
      when 'barcode'
        v.barcode = value
      when 'price'
        v.price = value
      when 'quantity_in_stock'
        v.quantity_in_stock = value
      when 'ignore_quantity'
        v.ignore_quantity = value
      when 'allow_backorder'
        v.allow_backorder = value
      when 'status'
        v.status = value
      when 'weight'
        v.weight = value
      when 'length'
        v.length = value
      when 'width'
        v.width = value
      when 'height'
        v.height = value
      when 'option1'
        v.option1 = value
      when 'option2'
        v.option2 = value
      when 'option3'
        v.option3 = value
      when 'requires_shipping'
        v.requires_shipping = value
      when 'taxable'
        v.taxable = value
    end
  end
  resp.success = save && v.save
  render :json => resp
end

#display_imageObject

GET /variants/:id/display-image



23
24
25
26
# File 'app/controllers/caboose/variants_controller.rb', line 23

def display_image
  ap "File is found"
  render :json => Variant.find(params[:id]).product_images.first
end

#find_by_optionsObject

POST /variants/find-by-options



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/caboose/variants_controller.rb', line 5

def find_by_options
  
  # Find the variant based on the product ID and options
  variant = Variant.find_by_options(params[:product_id], params[:option1], params[:option2], params[:option3])
  
  # If there are customizations, find the correct variant
  customizations = if params[:customizations]
    params[:customizations].map do |customization_id, options|
      Variant.find_by_options(customization_id, options[:option1], options[:option2], options[:option3])
    end
  else
    Array.new
  end
  
  render :json => { :variant => variant, :customizations => customizations }
end