Class: Caboose::SubscriptionsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/caboose/subscriptions_controller.rb', line 59

def admin_add
  return unless user_is_allowed('subscriptions', 'add')

  resp = Caboose::StdClass.new            
  name = params[:name]      

  if name.nil? || name.strip.length == 0
    resp.error = "A name is required."
  elsif Subscription.where(:site_id => @site.id, :name => params[:name]).exists?                 
    resp.error = "A subscription with that name already exists."
  else
    s = Subscription.create(
      :site_id             => @site.id,
      :name                => params[:name].strip,                
      :interval            => Subscription::INTERVAL_MONTHLY,
      :prorate             => false,        
      :start_on_day        => false
    )                                    
    resp.redirect = "/admin/subscriptions/#{s.id}"
    resp.success = true
  end                  
  render :json => resp
end

#admin_deleteObject



111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/caboose/subscriptions_controller.rb', line 111

def admin_delete
  return unless user_is_allowed('subscriptions', 'delete')
  
  model_ids = params[:id] == 'bulk' ? params[:model_ids] : [params[:id]]      
  model_ids.each do |model_id|
    UserSubscription.where(:subscription_id => model_id).destroy_all
    Subscription.where(:id => model_id).destroy_all
  end
  
  render :json => { :sucess => true }
end

#admin_editObject



45
46
47
48
49
# File 'app/controllers/caboose/subscriptions_controller.rb', line 45

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

#admin_indexObject



10
11
12
13
# File 'app/controllers/caboose/subscriptions_controller.rb', line 10

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

#admin_jsonObject



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
42
# File 'app/controllers/caboose/subscriptions_controller.rb', line 16

def admin_json
  return if !user_is_allowed('subscriptions', 'view')
  
  pager = PageBarGenerator.new(params, {
      'site_id'             => @site.id,          
      'name_like'           => '',
      'description_like'    => '',
      'variant_id'          => '',
      'interval'            => '',
      'prorate'             => '',
      'prorate_method'      => '',
      'prorate_flat_amount' => '',
      'start_on_day'        => '',
      'start_day'           => '',
      'start_month'         => ''                
		},{
		  'model'          => 'Caboose::Subscription',
	    'sort'			     => 'name',
		  'desc'			     => false,
		  'base_url'		   => "/admin/subscriptions",
		  'use_url_params' => false
	})
	render :json => {
	  :pager => pager,
	  :models => pager.items
	}
end

#admin_json_singleObject



52
53
54
55
56
# File 'app/controllers/caboose/subscriptions_controller.rb', line 52

def admin_json_single
  return if !user_is_allowed('subscriptions', 'view')    
  s = Subscription.find(params[:id])      
  render :json => s
end

#admin_optionsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/caboose/subscriptions_controller.rb', line 125

def admin_options
  if !user_is_allowed('subscriptions', 'edit')
    render :json => false
    return
  end
  
  options = []
  case params[:field]                
    when 'interval'
      options = [
        { 'value' => Subscription::INTERVAL_MONTHLY , 'text' => 'Monthly' },
        { 'value' => Subscription::INTERVAL_YEARLY  , 'text' => 'Yearly'  }
      ]                      
    when 'prorate-method'
      options = [
        { 'value' => Subscription::PRORATE_METHOD_FLAT       , 'text' => 'Flat Amount'            },
        { 'value' => Subscription::PRORATE_METHOD_PERCENTAGE , 'text' => 'Percentage of Interval' },
        { 'value' => Subscription::PRORATE_METHOD_CUSTOM     , 'text' => 'Custom'                 }
      ]
    when 'start-day'
      options = (1..31).collect{ |i| { 'value' => i, 'text' => i }}            
    when 'start-month'    
      options = (1..12).collect{ |i| { 'value' => i, 'text' => Date.new(2000, i, 1).strftime('%B') }}
  end              
  render :json => options 		
end

#admin_updateObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/caboose/subscriptions_controller.rb', line 84

def admin_update
  return unless user_is_allowed('subscriptions', 'edit')
  
  resp = StdClass.new
  models = params[:id] == 'bulk' ? params[:model_ids].collect{ |model_id| Subscription.find(model_id) } : [Subscription.find(params[:id])]
        
  params.each do |k, v|        
    case k
      when 'name'                then models.each{ |s| s.name                = v }
      when 'description'         then models.each{ |s| s.description         = v }
      when 'variant_id'          then models.each{ |s| s.variant_id          = v }
      when 'interval'            then models.each{ |s| s.interval            = v }
      when 'prorate'             then models.each{ |s| s.prorate             = v }
      when 'prorate_method'      then models.each{ |s| s.prorate_method      = v }
      when 'prorate_flat_amount' then models.each{ |s| s.prorate_flat_amount = v }
      when 'prorate_function'    then models.each{ |s| s.prorate_function    = v }
      when 'start_on_day'        then models.each{ |s| s.start_on_day        = v }
      when 'start_day'           then models.each{ |s| s.start_day           = v }
      when 'start_month'         then models.each{ |s| s.start_month         = v }
    end
  end
  models.each{ |s| s.save }
  resp.success = true      
  render :json => resp
end