Class: Caboose::VariantsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Caboose::VariantsController
- Defined in:
- app/controllers/caboose/variants_controller.rb
Instance Method Summary collapse
- #admin_add ⇒ Object
- #admin_attach_to_image ⇒ Object
- #admin_bulk_add ⇒ Object
- #admin_bulk_delete ⇒ Object
- #admin_delete ⇒ Object
- #admin_download_url ⇒ Object
- #admin_edit ⇒ Object
- #admin_edit_option1_media ⇒ Object
- #admin_edit_option2_media ⇒ Object
- #admin_edit_option3_media ⇒ Object
- #admin_edit_sort_order ⇒ Object
- #admin_group ⇒ Object
- #admin_index ⇒ Object
- #admin_json ⇒ Object
- #admin_json_single ⇒ Object
- #admin_new ⇒ Object
- #admin_options ⇒ Object
- #admin_remove_variants ⇒ Object
- #admin_unattach_from_image ⇒ Object
- #admin_update ⇒ Object
- #admin_update_option1_sort_order ⇒ Object
- #admin_update_option2_sort_order ⇒ Object
- #admin_update_option3_sort_order ⇒ Object
- #display_image ⇒ Object
- #find_by_options ⇒ Object
Methods inherited from ApplicationController
#add_ga_event, #admin_bulk_update, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in
Instance Method Details
#admin_add ⇒ Object
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'app/controllers/caboose/variants_controller.rb', line 403 def admin_add resp = Caboose::StdClass.new p = Caboose::Product.find(params[:product_id]) pd = @site.product_default vd = @site.variant_default v = Caboose::Variant.where(:alternate_id => params[:alternate_id]).first v = Caboose::Variant.new(:product_id => p.id) if v.nil? v.product_id = p.id v.alternate_id = params[:alternate_id].strip v.quantity_in_stock = params[:quantity_in_stock].strip.to_i v.price = '%.2f' % params[:price].strip.to_f v.option1 = params[:option1] if p.option1 v.option2 = params[:option2] if p.option2 v.option3 = params[:option3] if p.option3 v.cost = vd.cost v.available = vd.available v.ignore_quantity = vd.ignore_quantity v.allow_backorder = vd.allow_backorder v.weight = vd.weight v.length = vd.length v.width = vd.width v.height = vd.height v.volume = vd.volume v.cylinder = vd.cylinder v.requires_shipping = vd.requires_shipping v.taxable = vd.taxable v.status = vd.status v.downloadable = vd.downloadable v.is_bundle = vd.is_bundle v.save resp.success = true render :json => resp end |
#admin_attach_to_image ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'app/controllers/caboose/variants_controller.rb', line 154 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_bulk_add ⇒ Object
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'app/controllers/caboose/variants_controller.rb', line 444 def admin_bulk_add product = Product.find(params[:product_id]) resp = Caboose::StdClass.new p = Caboose::Product.find(params[:product_id]) # Check for data integrity first CSV.parse(params[:csv_data]).each do |row| if row[1].nil? then resp.error = "Quantity is not defined for variant: #{row[0].strip}" and break elsif row[2].nil? then resp.error = "Price is not defined for variant: #{row[0].strip}" and break elsif p.option1 && row[3].nil? then resp.error = "#{p.option1} is not defined for variant: #{row[0].strip}" and break elsif p.option2 && row[4].nil? then resp.error = "#{p.option2} is not defined for variant: #{row[0].strip}" and break elsif p.option3 && row[5].nil? then resp.error = "#{p.option3} is not defined for variant: #{row[0].strip}" and break end end if resp.error.nil? CSV.parse(params[:csv_data]).each do |row| v = nil if row[0].strip.length == 0 v = Caboose::Variant.new(:product_id => p.id) else v = Caboose::Variant.where(:alternate_id => row[0]).first v = Caboose::Variant.new(:product_id => p.id) if v.nil? end v.product_id = p.id v.alternate_id = row[0].strip v.quantity_in_stock = row[1].strip.to_i v.price = '%.2f' % row[2].strip.to_f v.option1 = row[3] if p.option1 v.option2 = row[4] if p.option2 v.option3 = row[5] if p.option3 v.status = 'Active' v.save end resp.success = true end render :json => resp end |
#admin_bulk_delete ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/controllers/caboose/variants_controller.rb', line 200 def admin_bulk_delete return if !user_is_allowed('variants', 'delete') resp = Caboose::StdClass.new params[:model_ids].each do |variant_id| v = Variant.find(variant_id) v.status = 'Deleted' v.save end resp.success = true render :json => resp end |
#admin_delete ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'app/controllers/caboose/variants_controller.rb', line 189 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_download_url ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/controllers/caboose/variants_controller.rb', line 80 def admin_download_url return if !user_is_allowed('variants', 'edit') resp = StdClass.new v = Variant.find(params[:id]) expires_in = params[:expires_in].to_i if !v.downloadable resp.error = "This variant is not downloadable." else config = YAML.load_file("#{::Rails.root}/config/aws.yml") AWS.config({ :access_key_id => config[Rails.env]['access_key_id'], :secret_access_key => config[Rails.env]['secret_access_key'] }) bucket = AWS::S3::Bucket.new(config[Rails.env]['bucket']) s3object = AWS::S3::S3Object.new(bucket, v.download_path) resp.url = s3object.url_for(:read, :expires => expires_in.minutes).to_s resp.success = true end render :json => resp end |
#admin_edit ⇒ Object
146 147 148 149 150 151 |
# File 'app/controllers/caboose/variants_controller.rb', line 146 def admin_edit return if !user_is_allowed('variants', 'edit') @variant = Variant.find(params[:id]) @product = @variant.product render :layout => 'caboose/admin' end |
#admin_edit_option1_media ⇒ Object
121 122 123 124 125 126 |
# File 'app/controllers/caboose/variants_controller.rb', line 121 def admin_edit_option1_media return if !user_is_allowed('products', 'edit') @product = Product.find(params[:product_id]) @variants = Variant.where(:product_id => @product.id, :option1 => params[:option_value]).reorder(:option1_sort_order).all render :layout => 'caboose/modal' end |
#admin_edit_option2_media ⇒ Object
129 130 131 132 133 134 |
# File 'app/controllers/caboose/variants_controller.rb', line 129 def admin_edit_option2_media return if !user_is_allowed('products', 'edit') @product = Product.find(params[:product_id]) @variants = Variant.where(:product_id => @product.id, :option2 => params[:option_value]).reorder(:option2_sort_order).all render :layout => 'caboose/modal' end |
#admin_edit_option3_media ⇒ Object
137 138 139 140 141 142 |
# File 'app/controllers/caboose/variants_controller.rb', line 137 def admin_edit_option3_media return if !user_is_allowed('products', 'edit') @product = Product.find(params[:product_id]) @variants = Variant.where(:product_id => @product.id, :option3 => params[:option_value]).reorder(:option3_sort_order).all render :layout => 'caboose/modal' end |
#admin_edit_sort_order ⇒ Object
114 115 116 117 118 |
# File 'app/controllers/caboose/variants_controller.rb', line 114 def admin_edit_sort_order return if !user_is_allowed('products', 'edit') @product = Product.find(params[:product_id]) render :layout => 'caboose/admin' end |
#admin_group ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'app/controllers/caboose/variants_controller.rb', line 363 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.join(' AND ')].concat(values)) : [] # Grab variants for each product @variants = products.collect { |product| product.variants }.flatten # Grab all categories; except for all and uncategorized @categories = Category.where('site_id = ? and parent_id IS NOT NULL AND name IS NOT NULL', @site.id).reorder(:url) # Grab all vendors @vendors = Vendor.where('site_id = ? and name IS NOT NULL', @site.id).reorder(:name) render :layout => 'caboose/admin' end |
#admin_index ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/controllers/caboose/variants_controller.rb', line 33 def admin_index return if !user_is_allowed('products', 'edit') @product = Product.find(params[:product_id]) if @product.variants.nil? || @product.variants.count == 0 v = Variant.new v.product_id = @product.id v.option1 = @product.default1 if @product.option1 v.option2 = @product.default2 if @product.option2 v.option3 = @product.default3 if @product.option3 v.status = 'Active' v.save end @variant = params[:variant_id] ? Variant.find(params[:variant_id]) : @product.variants[0] @highlight_variant_id = params[:highlight] ? params[:highlight].to_i : nil render :layout => 'caboose/admin' end |
#admin_json ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/controllers/caboose/variants_controller.rb', line 52 def admin_json return if !user_is_allowed('products', 'view') pager = Caboose::PageBarGenerator.new(params, { 'product_id' => params[:product_id] }, { 'model' => 'Caboose::Variant', 'sort' => 'option1, option2, option3', 'desc' => false, 'base_url' => "/admin/products/#{params[:product_id]}/variants", 'items_per_page' => 100, 'use_url_params' => false }) render :json => { :pager => pager, :models => pager.items.as_json(:include => [:flat_rate_shipping_package, :flat_rate_shipping_method]) } end |
#admin_json_single ⇒ Object
72 73 74 75 76 77 |
# File 'app/controllers/caboose/variants_controller.rb', line 72 def admin_json_single return if !user_is_allowed('products', 'view') v = Variant.find(params[:id]) render :json => v end |
#admin_new ⇒ Object
105 106 107 108 109 110 111 |
# File 'app/controllers/caboose/variants_controller.rb', line 105 def admin_new return if !user_is_allowed('variants', 'add') @top_categories = ProductCategory.where(:parent_id => nil).reorder('name').all @product_id = params[:product_id] @variant = Variant.new render :layout => 'caboose/admin' end |
#admin_options ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'app/controllers/caboose/variants_controller.rb', line 506 def if !user_is_allowed('variants', 'edit') render :json => false return end = [] case params[:field] when 'subscription-interval' = [ { 'value' => Variant::SUBSCRIPTION_INTERVAL_MONTHLY , 'text' => 'Monthly' }, { 'value' => Variant::SUBSCRIPTION_INTERVAL_YEARLY , 'text' => 'Yearly' } ] when 'subscription-prorate-method' = [ { 'value' => Variant::SUBSCRIPTION_PRORATE_METHOD_FLAT , 'text' => 'Flat Amount' }, { 'value' => Variant::SUBSCRIPTION_PRORATE_METHOD_PERCENTAGE , 'text' => 'Percentage of Interval' }, { 'value' => Variant::SUBSCRIPTION_PRORATE_METHOD_CUSTOM , 'text' => 'Custom' } ] when 'subscription-start-day' = (1..31).collect{ |i| { 'value' => i, 'text' => i }} when 'subscription-start-month' = (1..12).collect{ |i| { 'value' => i, 'text' => Date.new(2000, i, 1).strftime('%B') }} when 'status' = [ { 'value' => Variant::STATUS_ACTIVE , 'text' => 'Active' }, { 'value' => Variant::STATUS_INACTIVE , 'text' => 'Inactive' }, { 'value' => Variant::STATUS_DELETED , 'text' => 'Deleted' } ] end render :json => end |
#admin_remove_variants ⇒ Object
487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'app/controllers/caboose/variants_controller.rb', line 487 def admin_remove_variants params[:variant_ids].each do |variant_id| variant = Variant.find(variant_id) # variant.update_attribute(:status, 'deleted') # variant.product.update_attribute(:status, 'deleted') if variant.product.variants.where('status != ?', 'deleted').count == 0 end # Remove passed variants # redirect_to "/admin/products/#{params[:id]}/variants/group" render :json => true end |
#admin_unattach_from_image ⇒ Object
179 180 181 182 183 184 185 186 |
# File 'app/controllers/caboose/variants_controller.rb', line 179 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_update ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'app/controllers/caboose/variants_controller.rb', line 251 def admin_update return unless user_is_allowed_to 'edit', 'sites' resp = Caboose::StdClass.new variants = params[:id] == 'bulk' ? params[:model_ids].collect{ |variant_id| Variant.find(variant_id) } : [Variant.find(params[:id])] save = true params.each do |k,value| case k when 'alternate_id' then variants.each { |v| v.alternate_id = value } when 'sku' then variants.each { |v| v.sku = value } when 'barcode' then variants.each { |v| v. = value } when 'cost' then variants.each { |v| v.cost = value } when 'price' then variants.each { |v| v.price = value } when 'quantity_in_stock' then variants.each { |v| v.quantity_in_stock = value } when 'ignore_quantity' then variants.each { |v| v.ignore_quantity = value } when 'allow_backorder' then variants.each { |v| v.allow_backorder = value } when 'clearance' then variants.each { |v| v.clearance = value } when 'clearance_price' then variants.each { |v| v.clearance_price = value } when 'status' then variants.each { |v| v.status = value } when 'weight' then variants.each { |v| v.weight = value } when 'length' then variants.each { |v| v.length = value } when 'width' then variants.each { |v| v.width = value } when 'height' then variants.each { |v| v.height = value } when 'option1' then variants.each { |v| v.option1 = (value.blank? ? nil : value) } when 'option2' then variants.each { |v| v.option2 = (value.blank? ? nil : value) } when 'option3' then variants.each { |v| v.option3 = (value.blank? ? nil : value) } when 'option1_media_id' then variants.each { |v| v.option1_media_id = value } when 'option2_media_id' then variants.each { |v| v.option2_media_id = value } when 'option3_media_id' then variants.each { |v| v.option3_media_id = value } when 'taxable' then variants.each { |v| v.taxable = value } when 'requires_shipping' then value = value == true || value == 1 || value == '1' ? true : false variants.each { |v| v.requires_shipping = value v.downloadable = false if value } when 'downloadable' then value = value == true || value == 1 || value == '1' ? true : false variants.each { |v| v.downloadable = value v.requires_shipping = false if value } when 'download_path' then variants.each { |v| v.download_path = value } when 'is_subscription' then variants.each { |v| v.is_subscription = value } when 'subscription_interval' then variants.each { |v| v.subscription_interval = value } when 'subscription_prorate' then variants.each { |v| v.subscription_prorate = value } when 'subscription_prorate_method' then variants.each { |v| v.subscription_prorate_method = value } when 'subscription_prorate_flat_amount' then variants.each { |v| v.subscription_prorate_flat_amount = value } when 'subscription_prorate_function' then variants.each { |v| v.subscription_prorate_function = value } when 'subscription_start_on_day' then variants.each { |v| v.subscription_start_on_day = value } when 'subscription_start_day' then variants.each { |v| v.subscription_start_day = value } when 'subscription_start_month' then variants.each { |v| v.subscription_start_month = value } when 'is_bundle' then variants.each { |v| v.is_bundle = value } when 'flat_rate_shipping' then variants.each { |v| v.flat_rate_shipping = value } when 'flat_rate_shipping_single' then variants.each { |v| v.flat_rate_shipping_single = value } when 'flat_rate_shipping_combined' then variants.each { |v| v.flat_rate_shipping_combined = value } when 'flat_rate_shipping_package_id' then variants.each { |v| v.flat_rate_shipping_package_id = value } when 'flat_rate_shipping_method_id' then variants.each { |v| v.flat_rate_shipping_method_id = value } when 'flat_rate_shipping_package_method_id' then arr = value.split('_') variants.each do |v| v.flat_rate_shipping_package_id = arr[0].to_i v.flat_rate_shipping_method_id = arr[1].to_i end when 'sale_price' variants.each_with_index do |v, i| v.sale_price = value v.product.delay(:run_at => 3.seconds.from_now, :queue => 'caboose_store').update_on_sale if i == 0 end when 'date_sale_starts' variants.each_with_index do |v, i| v.date_sale_starts = ModelBinder.update_date(v.date_sale_starts, value, @logged_in_user.timezone) if i == 0 v.product.delay(:run_at => v.date_sale_starts, :queue => 'caboose_store').update_on_sale v.product.delay(:run_at => 3.seconds.from_now, :queue => 'caboose_store').update_on_sale end end when 'time_sale_starts' variants.each_with_index do |v, i| v.date_sale_starts = ModelBinder.update_time(v.date_sale_starts, value, @logged_in_user.timezone) if i == 0 v.product.delay(:run_at => v.date_sale_starts, :queue => 'caboose_store').update_on_sale v.product.delay(:run_at => 3.seconds.from_now, :queue => 'caboose_store').update_on_sale end end when 'date_sale_ends' variants.each_with_index do |v, i| v.date_sale_ends = ModelBinder.update_date(v.date_sale_ends, value, @logged_in_user.timezone) if i == 0 v.product.delay(:run_at => v.date_sale_ends , :queue => 'caboose_store').update_on_sale v.product.delay(:run_at => 3.seconds.from_now, :queue => 'caboose_store').update_on_sale end end when 'time_sale_ends' variants.each_with_index do |v, i| v.date_sale_ends = ModelBinder.update_time(v.date_sale_ends, value, @logged_in_user.timezone) if i == 0 v.product.delay(:run_at => v.date_sale_ends , :queue => 'caboose_store').update_on_sale v.product.delay(:run_at => 3.seconds.from_now, :queue => 'caboose_store').update_on_sale end end end end variants.each{ |v| v.save } resp.success = true render :json => resp end |
#admin_update_option1_sort_order ⇒ Object
214 215 216 217 218 219 220 221 222 223 |
# File 'app/controllers/caboose/variants_controller.rb', line 214 def admin_update_option1_sort_order product_id = params[:product_id] params[:values].each_with_index do |value, i| val = value.gsub('AMPERSAND','&') Variant.where(:product_id => product_id, :option1 => val).all.each do |v| v.update_attribute(:option1_sort_order, i) end end render :json => { :success => true } end |
#admin_update_option2_sort_order ⇒ Object
226 227 228 229 230 231 232 233 234 235 |
# File 'app/controllers/caboose/variants_controller.rb', line 226 def admin_update_option2_sort_order product_id = params[:product_id] params[:values].each_with_index do |value, i| val = value.gsub('AMPERSAND','&') Variant.where(:product_id => product_id, :option2 => val).all.each do |v| v.update_attribute(:option2_sort_order, i) end end render :json => { :success => true } end |
#admin_update_option3_sort_order ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'app/controllers/caboose/variants_controller.rb', line 238 def admin_update_option3_sort_order product_id = params[:product_id] params[:values].each_with_index do |value, i| val = value.gsub('AMPERSAND','&') Variant.where(:product_id => product_id, :option3 => val).all.each do |v| v.update_attribute(:option3_sort_order, i) end end render :json => { :success => true } end |
#display_image ⇒ Object
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_options ⇒ Object
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 the variant based on the product ID and options variant = Variant.(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, | Variant.(customization_id, [:option1], [:option2], [:option3]) end else Array.new end render :json => { :variant => variant, :customizations => customizations } end |