Class: Caboose::ModificationValuesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/modification_values_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

POST /admin/products/:product_id/modifications/:mod_id/values



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/caboose/modification_values_controller.rb', line 49

def admin_add
  return if !user_is_allowed('products', 'add')
  
  resp = Caboose::StdClass.new
  value = params[:value]
        
  if value.length == 0
    resp.error = "The value cannot be empty."
  else
    last_mv = ModificationValue.where(:modification_id => params[:mod_id]).reorder("sort_order desc").limit(1).first
    sort_order = last_mv ? last_mv.sort_order + 1 : 0
    mv = ModificationValue.new(:modification_id => params[:mod_id], :value => value, :sort_order => sort_order)                  
    mv.save
    resp.success = true        
  end
  render :json => resp    
end

#admin_deleteObject

DELETE /admin/products/:product_id/modifications/:mod_id/values/:id



68
69
70
71
72
73
74
# File 'app/controllers/caboose/modification_values_controller.rb', line 68

def admin_delete
  return if !user_is_allowed('products', 'delete')
  mv = ModificationValue.find(params[:id]).destroy      
  render :json => Caboose::StdClass.new({
    :success => true
  })
end

#admin_jsonObject

GET /admin/products/:product_id/modifications/:mod_id/values/json



5
6
7
8
9
10
# File 'app/controllers/caboose/modification_values_controller.rb', line 5

def admin_json
  return if !user_is_allowed('products', 'view')
  
  m = Modification.find(params[:mod_id])
  render :json => m.modification_values            
end

#admin_json_singleObject

GET /admin/products/:product_id/modifications/:mod_id/values/:id/json



13
14
15
16
# File 'app/controllers/caboose/modification_values_controller.rb', line 13

def admin_json_single
  mv = ModificationValue.find(params[:id])
  render :json => mv      
end

#admin_updateObject

PUT /admin/products/:product_id/modifications/:mod_id/values/:id



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/caboose/modification_values_controller.rb', line 19

def admin_update
  return if !user_is_allowed('products', 'edit')
  
  resp = Caboose::StdClass.new
  mv = ModificationValue.find(params[:id])    
  
  save = true    
  params.each do |name,value|
    case name
      when 'sort_order'        then mv.sort_order        = value
      when 'value'             then mv.value             = value
      when 'is_default'        then
        if value.to_i == 1
          ModificationValue.where(:modification_id => params[:mod_id]).all.each do |mv2|
            mv2.is_default = false
            mv2.save
          end
        end
        mv.is_default = value
        
      when 'price'             then mv.price             = value
      when 'requires_input'    then mv.requires_input    = value
      when 'input_description' then mv.input_description = value
    end
  end
  resp.success = save && mv.save
  render :json => resp
end

#admin_update_sort_orderObject

PUT /admin/products/:product_id/modifications/:mod_id/values/sort-order



77
78
79
80
81
82
83
84
# File 'app/controllers/caboose/modification_values_controller.rb', line 77

def admin_update_sort_order            
  params[:modification_value_ids].each_with_index do |mv_id, i|
    mv = ModificationValue.where(:id => mv_id).first
    mv.sort_order = i
    mv.save
  end      
  render :json => { :success => true }
end