Class: Caboose::VariantLimitsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_update, #admin_delete, #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


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

def admin_add
  return unless (user_is_allowed_to 'edit', 'variantlimits')
  resp = Caboose::StdClass.new
  if VariantLimit.where(:user_id => params[:user_id], :variant_id => params[:variant_id]).exists?
    resp.error = 'That variant is already added to this user.'
  else
    c = VariantLimit.new
    c.user_id            = params[:user_id]
    c.variant_id         = params[:variant_id]
    c.min_quantity_value = 0
    c.max_quantity_value = 0
    c.current_value      = 0
    c.save
    resp.success = true
  end
  render :json => resp
end

#admin_bulk_deleteObject


105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/caboose/variant_limits_controller.rb', line 105

def admin_bulk_delete
  return if !user_is_allowed('variantlimits', 'delete')
  resp = Caboose::StdClass.new
  params[:model_ids].each do |vl_id|
    vl = VariantLimit.find(vl_id)
    vl.destroy
  end
  resp.success = true
  render :json => resp
end

#admin_createObject


54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/caboose/variant_limits_controller.rb', line 54

def admin_create
  resp = Caboose::StdClass.new
  mq = params[:max_quantity_value]
  elo = User.where(:site_id => @site.id, :username => 'elo').first
  vl = VariantLimit.where(:user_id => elo.id, :variant_id => params[:variant_id]).first
  vl = VariantLimit.create(:user_id => elo.id, :variant_id => params[:variant_id]) if vl.nil?
  vl.max_quantity_value = mq.blank? ? nil : mq
  vl.min_quantity_value = 0
  vl.current_value = 0
  resp.success = vl.save
  render :json => resp
end

#admin_editObject


98
99
100
101
102
# File 'app/controllers/caboose/variant_limits_controller.rb', line 98

def admin_edit
  return unless (user_is_allowed_to 'edit', 'users')
  @edituser = Caboose::User.find(params[:user_id])
  @variant_limit = Caboose::VariantLimit.find(params[:id])
end

#admin_jsonObject


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

def admin_json 
  render :json => false and return if !user_is_allowed_to 'view', 'variantlimits'
  pager = Caboose::Pager.new(params, {
    'variant_id' => '',
    'user_id' => params[:user_id]
  },
  {
    'model' => 'Caboose::VariantLimit',
    'sort'  => 'variant_id',
    'desc'  => 'false',
    'base_url' => '/admin/variant-limits',
    'items_per_page' => 100
  })
  render :json => {
    :pager => pager,
    :models => pager.items.as_json(:include => [:variant])
  } 
end

#admin_json_singleObject


28
29
30
31
32
# File 'app/controllers/caboose/variant_limits_controller.rb', line 28

def admin_json_single
  render :json => false and return if !user_is_allowed_to 'edit', 'variantlimits'
  variantlimit = VariantLimit.find(params[:id])
  render :json => variantlimit.as_json(:include => [:variant])
end

#admin_updateObject


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/caboose/variant_limits_controller.rb', line 69

def admin_update
  return unless (user_is_allowed_to 'edit', 'variantlimits')
  resp = Caboose::StdClass.new
  variantlimit = VariantLimit.find(params[:id])
  user = logged_in_user
  if user
    params.each do |k,v|
      case k
        when "user_id"            then variantlimit.user_id            = v
        when "variant_id"         then variantlimit.variant_id         = v
        when "min_quantity_value" then variantlimit.min_quantity_value = v
        when "min_quantity_func"  then variantlimit.min_quantity_func  = v
        when "max_quantity_value" then variantlimit.max_quantity_value = v
        when "max_quantity_func"  then variantlimit.max_quantity_func  = v
        when "current_value"      then variantlimit.current_value      = v
      end
    end
    resp.success = variantlimit.save
  end
  render :json => resp
end

#admin_user_indexObject


92
93
94
95
# File 'app/controllers/caboose/variant_limits_controller.rb', line 92

def admin_user_index
  return unless (user_is_allowed_to 'edit', 'users')
  @edituser = Caboose::User.find(params[:user_id])
end

#admin_variant_optionsObject


118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/caboose/variant_limits_controller.rb', line 118

def admin_variant_options
  return unless user_is_allowed('edit', 'users')
  options = [{ 'value' => nil, 'text' => 'Select a Variant' }]
  products = Caboose::Product.where(:site_id => @site.id).all
  products.each do |p|
    p.variants.each do |v|
      options << { 'value' => v.id, 'text' => v.full_title }
    end
  end
  render :json => options
end