Class: Caboose::GiftCardsController

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



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/gift_cards_controller.rb', line 57

def admin_add
  return if !user_is_allowed('giftcards', 'add')
  
  resp = StdClass.new
  code = params[:code].strip
  
  if code.length == 0
    resp.error = "A valid code is required."
  elsif GiftCard.where(:code => code).exists?
    resp.error = "A gift card with that code already exists."
  else
    gc = GiftCard.new(
      :site_id => @site.id,
      :code    => code,
      :status  => GiftCard::STATUS_INACTIVE              
    )
    resp.success = gc.save
    resp.new_id = gc.id
    resp.redirect = "/admin/gift-cards/#{gc.id}"
  end
  
  render :json => resp        
end

#admin_bulk_addObject

POST /admin/gift-cards/bulk



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
# File 'app/controllers/caboose/gift_cards_controller.rb', line 82

def admin_bulk_add
  return if !user_is_allowed('sites', 'add')
  
  resp = Caboose::StdClass.new
  i = 0
  CSV.parse(params[:csv_data].strip).each do |row|        
    if row[0].nil? || row[0].strip.length == 0        
      resp.error = "Code not defined on row #{i+1}."        
    end
    i = i + 1
  end
  
  if resp.error.nil?
    CSV.parse(params[:csv_data]).each do |row|
      Caboose::GiftCard.create(
        :site_id  => @site.id,
        :code => row[0].strip,            
        :status => GiftCard::STATUS_INACTIVE                        
      )
    end
    resp.success = true
  end
  
  render :json => resp
end

#admin_bulk_deleteObject

DELETE /admin/gift-cards/:id/bulk



179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/caboose/gift_cards_controller.rb', line 179

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

#admin_bulk_updateObject

PUT /admin/gift-cards/bulk



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/caboose/gift_cards_controller.rb', line 142

def admin_bulk_update
  return unless user_is_allowed_to 'edit', 'sites'

  resp = Caboose::StdClass.new    
  gift_cards = params[:model_ids].collect{ |gc_id| GiftCard.find(gc_id) }

  save = true
  params.each do |k,v|
    case k
      when 'site_id'         then gift_cards.each{ |gc| gc.site_id         = v }                 
      when 'name'            then gift_cards.each{ |gc| gc.name            = v }
      when 'code'            then gift_cards.each{ |gc| gc.code            = v }
      when 'card_type'       then gift_cards.each{ |gc| gc.card_type       = v }
      when 'total'           then gift_cards.each{ |gc| gc.total           = v }
      when 'balance'         then gift_cards.each{ |gc| gc.balance         = v }
      when 'min_order_total' then gift_cards.each{ |gc| gc.min_order_total = v }            
      when 'date_available'  then gift_cards.each{ |gc| gc.date_available  = DateTime.strptime(v, '%m/%d/%Y') }
      when 'date_expires'    then gift_cards.each{ |gc| gc.date_expires    = DateTime.strptime(v, '%m/%d/%Y') }
      when 'status'          then gift_cards.each{ |gc| gc.status          = v }                      
    end        
  end
  gift_cards.each{ |gc| gc.save }

  resp.success = true
  render :json => resp
end

#admin_card_type_optionsObject

GET /admin/gift-cards/card-type-options



203
204
205
206
207
208
209
210
211
212
213
# File 'app/controllers/caboose/gift_cards_controller.rb', line 203

def admin_card_type_options
  return if !user_is_allowed('categories', 'view')
  types = [
    GiftCard::CARD_TYPE_AMOUNT,
    GiftCard::CARD_TYPE_PERCENTAGE,
    GiftCard::CARD_TYPE_NO_SHIPPING,
    GiftCard::CARD_TYPE_NO_TAX,
  ]        
  options = types.collect{ |s| { 'text' => s, 'value' => s }}       
  render :json => options            
end

#admin_deleteObject

DELETE /admin/gift-cards/:id



170
171
172
173
174
175
176
# File 'app/controllers/caboose/gift_cards_controller.rb', line 170

def admin_delete
  return if !user_is_allowed('giftcards', 'delete')
  GiftCard.find(params[:id]).destroy
  render :json => Caboose::StdClass.new({
    :redirect => '/admin/gift-cards'
  })
end

#admin_editObject

GET /admin/gift-cards/:id



109
110
111
112
113
# File 'app/controllers/caboose/gift_cards_controller.rb', line 109

def admin_edit
  return if !user_is_allowed('giftcards', 'edit')
  @gift_card = GiftCard.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_indexObject

GET /admin/gift-cards



5
6
7
8
# File 'app/controllers/caboose/gift_cards_controller.rb', line 5

def admin_index
  return if !user_is_allowed('giftcards', 'view')      
  render :layout => 'caboose/admin'
end

#admin_jsonObject

GET /admin/gift-cards/json



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/caboose/gift_cards_controller.rb', line 11

def admin_json
  return if !user_is_allowed('giftcards', 'view')
  
  pager = PageBarGenerator.new(params, {
      'site_id'             => @site.id,          
      'name'                => '',
      'code'                => '',      
      'card_type'           => '',
      'total_lte'           => '',
      'total_gte'           => '',
      'balance_lte'         => '',
      'balance_gte'         => '',
      'min_order_total_lte' => '',
      'min_order_total_gte' => '',
      'date_available_lte'  => '',
      'date_available_gte'  => '',
      'date_expires_lte'    => '',
      'date_expires_gte'    => '',
      'status'              => ''
		},{
		  'model'          => 'Caboose::GiftCard',
	    'sort'			     => 'code',
		  'desc'			     => false,
		  'base_url'		   => "/admin/gift-cards",
		  'use_url_params' => false
	})
	render :json => {
	  :pages => pager,
	  :models => pager.items
	}
end

#admin_json_singleObject

GET /admin/gift-cards/:id/json



44
45
46
47
48
# File 'app/controllers/caboose/gift_cards_controller.rb', line 44

def admin_json_single
  return if !user_is_allowed('giftcards', 'view')    
  gc = GiftCard.find(params[:id])      
  render :json => gc
end

#admin_newObject

GET /admin/gift-cards/new



51
52
53
54
# File 'app/controllers/caboose/gift_cards_controller.rb', line 51

def admin_new
  return if !user_is_allowed('giftcards', 'add')      
  render :layout => 'caboose/admin'
end

#admin_status_optionsObject

GET /admin/gift-cards/status-options



191
192
193
194
195
196
197
198
199
200
# File 'app/controllers/caboose/gift_cards_controller.rb', line 191

def admin_status_options
  return if !user_is_allowed('categories', 'view')
  statuses = [      
    GiftCard::STATUS_INACTIVE,
    GiftCard::STATUS_ACTIVE,
    GiftCard::STATUS_EXPIRED
  ]        
  options = statuses.collect{ |s| { 'text' => s, 'value' => s }}       
  render :json => options
end

#admin_updateObject

PUT /admin/gift-cards/:id



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/caboose/gift_cards_controller.rb', line 116

def admin_update
  return if !user_is_allowed('giftcards', 'edit')
  
  resp = Caboose::StdClass.new
  gc = GiftCard.find(params[:id])    
  
  save = true    
  params.each do |name,value|
    case name
      when 'site_id'         then gc.site_id         = value                 
      when 'name'            then gc.name            = value
      when 'code'            then gc.code            = value
      when 'card_type'       then gc.card_type       = value
      when 'total'           then gc.total           = value
      when 'balance'         then gc.balance         = value
      when 'min_order_total' then gc.min_order_total = value
      when 'date_available'  then gc.date_available  = DateTime.strptime(value, '%m/%d/%Y')
      when 'date_expires'    then gc.date_expires    = DateTime.strptime(value, '%m/%d/%Y')
      when 'status'          then gc.status          = value                            
    end
  end          
  resp.success = save && gc.save
  render :json => resp
end