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


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/caboose/subscriptions_controller.rb', line 66

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

  resp = Caboose::StdClass.new            
  v = params[:variant_id] ? Variant.where(:id => params[:variant_id]).first : nil      

  if params[:variant_id].nil? || v.nil? || v.product.site_id != @site.id || !v.is_subscription 
    resp.error = "A valid subscription variant is required."      
  else
    s = Subscription.create(
      :variant_id        => v.id,
      :user_id           => params[:user_id],
      :date_started      => Date.today,
      :date_started_full => Date.today,
      :status            => UserSubcription::STATUS_ACTIVE
    )                                    
    resp.redirect = "/admin/users/#{params[:user_id]}/subscriptions/#{s.id}"
    resp.success = true
  end                  
  render :json => resp
end

#admin_create_invoicesObject


110
111
112
113
114
115
# File 'app/controllers/caboose/subscriptions_controller.rb', line 110

def admin_create_invoices
  return if !user_is_allowed('subscriptions', 'edit')    
  s = Subscription.find(params[:id])
  s.create_invoices
  render :json => { :success => true }      
end

#admin_deleteObject


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

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|
    Subscription.where(:id => model_id).destroy_all        
  end
  
  render :json => { :sucess => true }
end

#admin_editObject


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

def admin_edit
  return if !user_is_allowed('subscriptions', 'edit')    
  @subscription = Subscription.find(params[:id])      
  @edituser = @subscription.user
  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


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

def admin_json
  return if !user_is_allowed('subscriptions', 'view')
  
  pager = PageBarGenerator.new(params, {
      'site_id'               => @site.id,
      'variant_id'            => '',
      'user_id'               => params[:user_id] ? params[:user_id] : '',
      'date_started_gte'      => '',
      'date_started_lte'      => '',
      'date_started_full_gte' => '',
      'date_started_full_lte' => '',
      'status'                => ''                                
		},{
		  'model'          => 'Caboose::Subscription',
	    'sort'			     => 'date_started',
		  'desc'			     => false,
		  'base_url'		   => params[:user_id] ? "/admin/users/#{params[:user_id]}/subscriptions" : '/admin/subscriptions',
		  'use_url_params' => false
	})
	render :json => {
	  :pager => pager,
	  :models => pager.items.as_json(:include => [:user, { :variant => { :include => :product }}])
	}
end

#admin_json_singleObject


59
60
61
62
63
# File 'app/controllers/caboose/subscriptions_controller.rb', line 59

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

#admin_optionsObject


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/controllers/caboose/subscriptions_controller.rb', line 132

def admin_options
  if !user_is_allowed('subscriptions', 'edit')
    render :json => false
    return
  end
  
  options = []
  case params[:field]                
    when 'variant'
      arr = Variant.joins(:product).where("store_products.site_id = ? and is_subscription = ?", @site.id, true).reorder("store_products.title").all          
      options = arr.collect{ |v| { 'value' => v.id, 'text' => "#{v.product.title} - #{v.alternate_id}" }}
    when 'status'
      options = [
        { 'value' => Subscription::STATUS_ACTIVE   , 'text' => 'Active'   },
        { 'value' => Subscription::STATUS_INACTIVE , 'text' => 'Inactive' }
      ]          
    when 'user'
      arr = User.where(:site_id => @site.id).reorder('last_name, first_name').all          
      options = arr.collect{ |u| { 'value' => u.id, 'text' => "#{u.first_name} #{u.last_name}" }}
  end              
  render :json => options 		
end

#admin_updateObject


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

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 'variant_id'        then models.each{ |s| s.variant_id        = v }
      when 'user_id'           then models.each{ |s| s.user_id           = v }
      when 'date_started'      then models.each{ |s| s.date_started      = v }
      when 'date_started_full' then models.each{ |s| s.date_started_full = v }
      when 'status'            then models.each{ |s| s.status            = v }                
    end
  end
  models.each{ |s| s.save }
  resp.success = true      
  render :json => resp
end

#admin_user_indexObject


16
17
18
19
20
# File 'app/controllers/caboose/subscriptions_controller.rb', line 16

def admin_user_index
  return if !user_is_allowed('subscriptions', 'view')
  @edituser = User.find(params[:user_id])
  render :layout => 'caboose/admin'      
end