Class: Caboose::VariantChildrenController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_edit, #admin_index, #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_addObject


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/caboose/variant_children_controller.rb', line 37

def admin_add      
  resp = Caboose::StdClass.new

  if params[:parent_id] == params[:variant_id]
    resp.error = "Can't add the same variant as a child."
  else
    vc = VariantChild.create(
      :parent_id  => params[:parent_id],
      :variant_id => params[:variant_id],
      :quantity   => params[:quantity] ? params[:quantity] : 1
    )
    resp.new_id = vc.id
    resp.success = true
  end
  render :json => resp
end

#admin_deleteObject


74
75
76
77
78
79
80
81
# File 'app/controllers/caboose/variant_children_controller.rb', line 74

def admin_delete
  return if !user_is_allowed('variants', 'delete')      
  vc_ids = params[:id] == 'bulk' ? params[:model_ids] : [params[:id]]
  vc_ids.each do |vc_id|
    VariantChild.where(:id => vc_id).destroy_all
  end
  render :json => { :success => true }      
end

#admin_jsonObject


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/caboose/variant_children_controller.rb', line 9

def admin_json
  return if !user_is_allowed('products', 'view')
  
  pager = Caboose::PageBarGenerator.new(params, {
    'parent_id'      => params[:variant_id]        
  }, {
    'model'          => 'Caboose::VariantChild',
    'sort'           => 'parent_id',
    'desc'           => false,
    'base_url'       => "/admin/products/#{params[:product_id]}/variants/#{params[:variant_id]}/children",
    'items_per_page' => 100,
    'use_url_params' => false        
  })
  render :json => {
    :pager => pager,
    :models => pager.items.as_json(:include => { :variant => { :methods => :full_title }})
  }      
end

#admin_json_singleObject


29
30
31
32
33
34
# File 'app/controllers/caboose/variant_children_controller.rb', line 29

def admin_json_single
  return if !user_is_allowed('products', 'view')
  
  vc = VariantChild.find(params[:id])      
  render :json => vc.as_json(:include => { :variant => { :methods => :full_title }})      
end

#admin_updateObject


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/caboose/variant_children_controller.rb', line 55

def admin_update
  return if !user_is_allowed('variants', 'edit')
  
  resp = Caboose::StdClass.new
  vcs = params[:id] == 'bulk' ? params[:model_ids].collect{ |model_id| VariantChild.find(model_id) } : [VariantChild.find(params[:id])]    
            
  params.each do |name,value|
    case name        
      when 'parent_id'  then vcs.each{ |vc| vc.parent_id   = value }
      when 'variant_id' then vcs.each{ |vc| vc.variant_id  = value }
      when 'quantity'   then vcs.each{ |vc| vc.quantity    = value }          
    end
  end
  vcs.each{ |vc| vc.save }
  resp.success = true
  render :json => resp
end