Class: Iugu::AccountController

Inherits:
AccountSettingsController
  • Object
show all
Defined in:
app/controllers/iugu/account_controller.rb

Instance Method Summary collapse

Instance Method Details

#activateObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/iugu/account_controller.rb', line 118

def activate
  
  if @account.subscription.try :activate 
    if @account.subscription.suspended
      notice = I18n.t("iugu.notices.account_will_be_activated")
    else
      notice = I18n.t("iugu.notices.account_activated")
    end
    @account.clear_cached_subscription_active
  else
    notice = I18n.t("iugu.notices.error_activating_account")
  end
  redirect_to (params[:account_id]), :notice => notice
end

#cancel_destructionObject



42
43
44
45
46
# File 'app/controllers/iugu/account_controller.rb', line 42

def cancel_destruction
  current_user.accounts.find(params[:id]).cancel_destruction
  notice = I18n.t("iugu.account_destruction_undone")
  redirect_to(, :notice => notice)
end

#createObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/iugu/account_controller.rb', line 64

def create
  create_parameters = {}
  if IuguSDK::enable_subscription_features
    plan_identifier = params[:plan] || IuguSDK::default_subscription_name
    currency = locale_to_currency I18n.locale
    create_parameters = {plan_identifier: plan_identifier, currency: currency, email: current_user.email}
  end
  acc = Account.create(create_parameters)
  current_user.accounts << acc
  #redirect_to account_view_path(acc)
  (current_user, acc.id.to_param)
  redirect_to IuguSDK::app_main_url
end

#destroyObject



32
33
34
35
36
37
38
39
40
# File 'app/controllers/iugu/account_controller.rb', line 32

def destroy
  if IuguSDK::
     = current_user.accounts.find(params[:id])
    .destroy
    redirect_to(, :notice => I18n.t("iugu.account_destruction_in") + .destruction_job.run_at.to_s)
  else
    raise ActionController::RoutingError.new('Not found')
  end
end

#generate_new_tokenObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/iugu/account_controller.rb', line 78

def generate_new_token
  if IuguSDK::
    @account = current_user.accounts.find(params[:account_id])
    token = @account.tokens.create(description: params[:description], api_type: params[:api_type])
    if token.new_record?
      notice = token.errors.full_messages
    else
      notice = I18n.t("iugu.notices.new_token_generated")
    end
    flash[:group] = :api_token
    redirect_to (params[:account_id]), :notice => notice
  else
    raise ActionController::RoutingError.new('Not found')
  end
end

#indexObject



6
7
8
9
# File 'app/controllers/iugu/account_controller.rb', line 6

def index
  @accounts = current_user.accounts.order(:created_at)
  render 'iugu/settings/accounts'
end

#payment_historyObject



110
111
112
113
114
115
116
# File 'app/controllers/iugu/account_controller.rb', line 110

def payment_history
  
  subscription = Iugu::Api::Subscription.find @account.subscription_id.to_uuid.to_s
  customer = Iugu::Api::Customer.find subscription.customer_id
  @invoices = Iugu::Api::Invoice.find :all, params: {hl: current_user.locale, customer_id: customer.id.to_param, status_filter: ["pending", "paid"], limit: 10}
  render 'iugu/account/payment_history'
end

#remove_tokenObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/iugu/account_controller.rb', line 94

def remove_token
  if IuguSDK::
     = current_user.accounts.find params[:account_id]
    token = .tokens.find params[:token] 
    if token.destroy
      notice = I18n.t("iugu.notices.token_removed")
    else
      notice = token.errors.full_messages
    end
    flash[:group] = :api_token
    redirect_to (params[:account_id]), :notice => notice
  else
    raise ActionController::RoutingError.new('Not found')
  end
end

#selectObject



48
49
50
51
52
53
54
# File 'app/controllers/iugu/account_controller.rb', line 48

def select
  (current_user, params[:id])
  #redirect_to :back
  redirect_to IuguSDK::app_main_url
rescue ActionController::RedirectBackError
  redirect_to(, :notice => "Account selected")
end

#suspendObject



133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/iugu/account_controller.rb', line 133

def suspend
  
  if @account.subscription.try :suspend 
    notice = I18n.t("iugu.notices.account_suspended")
    @account.clear_cached_subscription_active
  else
    notice = I18n.t("iugu.notices.error_suspending_account")
  end
  redirect_to (params[:account_id]), :notice => notice
end

#updateObject



57
58
59
60
61
62
# File 'app/controllers/iugu/account_controller.rb', line 57

def update
  @account = Account.find(params[:id])
  @account.update_attributes(params[:account])
  flash[:group] = :account_update
  redirect_to (params[:id]), :notice => I18n.t("iugu.notices.account_updated")
end

#viewObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/iugu/account_controller.rb', line 11

def view
  if params[:id]
    @account = current_user.accounts.find(params[:id])
  else
    @account = .
  end

  @has_subscription = false

  if IuguSDK::enable_subscription_features
    unless @account.subscription_id.blank?
      @has_subscription = true
      @subscription = @account.subscription
      plan = Iugu::Api::Plan.find_by_identifier @subscription.plan_identifier
      @plan_name = plan.try :name
    end
  end
  @primary_domain = @account..where(:primary => true).first if @account
  render 'iugu/settings/account'
end