forked from chaosdorf/mete
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers_controller.rb
More file actions
166 lines (147 loc) · 4.39 KB
/
users_controller.rb
File metadata and controls
166 lines (147 loc) · 4.39 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class UsersController < ApplicationController
include ApplicationHelper
include UsersHelper
# GET /users
def index
if params[:barcode] != nil
@user = User.find_by(barcode: params[:barcode])
if @user
redirect_to @user
else
redirect_to users_url, :flash => { :error => "no user with that barcode found" }
end
end
@users = User.order(active: :desc).order_by_name_asc
# index.html.haml
end
# GET /users/1
def show
@user = User.find(params[:id])
@drinks = Drink.order(active: :desc).order(caffeine: :asc).order_by_name_asc
@wrapped = nil
if @user.audit
if Date.today.month == 12
@wrapped = Date.today.year
end
if Date.today.month == 01
@wrapped = Date.today.year - 1
end
end
# show.html.haml
end
# GET /users/new
def new
@user = User.new
# new.html.haml
end
# GET /users/1/edit
def edit
@user = User.find(params[:id])
# edit.html.haml
end
# POST /users
def create
@user = User.new(user_params)
if @user.save
redirect_to @user, :flash => { :success => 'User was successfully created.' }
else
render action: "new", error: "Couldn't create the user. Error: #{@user.errors} Status: #{:unprocessable_entity}"
end
end
# PATCH /users/1
def update
@user = User.find(params[:id])
@old_audit_status = @user.audit
if @user.update(user_params)
if @user.audit != @old_audit_status
unless @user.audit
@user_audits = Audit.where(:user => @user.id)
@user_audits.each do |audit|
audit.user = nil
audit.save!
end
flash[:info] = "Deleted all your logs."
end
end
if @user.avatar_provider = "webfinger"
Rails.cache.delete "fetch_avatar_url_from_webfinger_or_activitypub #{@user.avatar}"
end
flash[:success] = "User was successfully updated."
no_resp_redir @user
else
render action: "edit", error: "Couldn't update the user. Error: #{@user.errors} Status: #{:unprocessable_entity}"
end
end
# DELETE /users/1
def destroy
@user = User.find(params[:id])
if @user.destroy
flash[:success] = "User was successfully deleted."
no_resp_redir users_url
else
redirect_to users_url, :flash => { :error => "Couldn't delete the user. Error: #{@user.errors} Status: #{:unprocessable_entity}" }
end
end
# GET /users/1/deposit?amount=100
def deposit
@user = User.find(params[:id])
@user.deposit(BigDecimal(params[:amount]))
flash[:success] = "You just deposited some money and your new balance is #{show_amount(@user.balance)}. Thank you."
warn_user_if_audit
no_resp_redir @user
end
# GET /users/1/buy?drink=5
def buy
@user = User.find(params[:id])
@drink = Drink.find(params[:drink])
buy_drink
end
# POST /users/1/buy_barcode
def buy_barcode
@user = User.find(params[:id])
unless Barcode.where(id: params[:barcode]).exists?
flash[:danger] = "No drink found with this barcode."
redirect_to @user
else
@drink = Drink.find(Barcode.find(params[:barcode]).drink)
buy_drink
end
end
# GET /users/1/pay?amount=1.5
def payment
@user = User.find(params[:id])
@user.payment(BigDecimal(params[:amount]))
print_balance
warn_user_if_audit
no_resp_redir @user
end
# GET /users/stats
def stats
@user_count = User.count
@balance_sum = User.sum(:balance)
# stats.html.haml
end
private
def buy_drink
@user.buy(@drink)
print_balance
warn_user_if_audit
no_resp_redir @user.redirect ? redirect_path(@user) : @user
end
def user_params
params.require(:user).permit(:name, :avatar_provider, :avatar, :balance, :active, :audit, :redirect, :barcode)
end
def warn_user_if_audit
if (@user.audit) then
flash[:info] = "This transaction has been logged, because you set up your account that way. #{view_context.link_to 'Change?', edit_user_url(@user)}".html_safe
end
end
def print_balance
flash[:success] = "You just bought a drink and your new balance is #{show_amount(@user.balance)}. Thank you."
if (@user.balance < -50) then
flash[:danger] = "Your balance is way below zero. Remember to compensate as soon as possible."
elsif (@user.balance < 0) then
flash[:warning] = "Your balance is below zero. Please remember to compensate."
end
end
end