-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathapplication_controller.rb
92 lines (76 loc) · 3.11 KB
/
application_controller.rb
1
2
3
4
5
6
7
8
9
10
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :load_settings, :set_default_mailer_host
after_filter :store_location
def load_settings
@settings = Settings.find_by_id(1)
if !@settings
@settings = Settings.create
end
end
def set_default_mailer_host
ActionMailer::Base.default_url_options = {:host => request.host_with_port}
end
def store_location
# store last url as long as it isn't an /account path
session[:previous_url] = request.fullpath unless request.fullpath =~ /\/account/
end
def after_sign_in_path_for(resource)
session[:previous_url] || root_path
end
def verify_admin
if !current_user.admin?
redirect_to root_url, :flash => { :notice => "You must be an admin to access that page" }
end
end
def check_init
if [email protected]_flag
if current_user
# Create the Admin User
current_user.update_attribute :admin, true
# Set intiatilized flag to true
@settings.update_attribute :initialized_flag, true
# Set default reply_to_email to Admin User email
@settings.update_attribute :reply_to_email, current_user.email
# Create the Crowdtilt API Users
begin
Crowdtilt.sandbox
sandbox_guest = {
firstname: 'Crowdhoster',
lastname: (Rails.configuration.crowdhoster_app_name + '-guest'),
email: (Rails.configuration.crowdhoster_app_name + '[email protected]')
}
sandbox_guest = Crowdtilt.post('/users', {user: sandbox_guest})
sandbox_admin = {
firstname: 'Crowdhoster',
lastname: (Rails.configuration.crowdhoster_app_name + '-admin'),
email: (Rails.configuration.crowdhoster_app_name + '[email protected]')
}
sandbox_admin = Crowdtilt.post('/users', {user: sandbox_admin})
rescue => exception
@settings.update_attribute :initialized_flag, false
sign_out current_user
if(exception.message == "Invalid credentials" && Rails.env.development?)
errorMsg = 'Invalid credentials, check Crowdtilt API Key and API Secret'
else
errorMsg = exception.message
end
redirect_to new_user_registration_url, :flash => { :error => "An error occurred, please contact [email protected]: #{errorMsg}" }
return
else
@settings.update_attribute :ct_sandbox_guest_id, sandbox_guest['user']['id']
@settings.update_attribute :ct_sandbox_admin_id, sandbox_admin['user']['id']
end
# Put user back on admin area
redirect_to admin_website_url, :flash => { :success => "Nice! Your app is now initialized." }
else
redirect_to new_user_registration_url, :flash => { :error => "Please create an account below to initialize the app." }
end
end
end
def calculate_processing_fee(amount_cents)
amount_cents *= Rails.configuration.processing_fee_percentage.to_f / 100
amount_cents += Rails.configuration.processing_fee_flat_cents
return amount_cents.ceil
end
end