-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathusers_controller.rb
More file actions
171 lines (153 loc) · 4.58 KB
/
users_controller.rb
File metadata and controls
171 lines (153 loc) · 4.58 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
167
168
169
170
171
require 'net/http'
require 'date'
class UsersController < ApplicationController
include ApplicationHelper
# GET /users
def index
@users = User.order(active: :desc).order("name COLLATE nocase")
# index.html.haml
end
# GET /users/1
def show
@user = User.find(params[:id])
@drinks = Drink.order(active: :desc).order("name COLLATE nocase")
# 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, 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_attributes(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[:notice] = "Deleted all your logs."
end
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, 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.new(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
# POST /users/1/print_label
# POST /users/1/print_label.json
def print_label
@user = User.find(params[:id])
text = "#{@user.name} #{Date.today.iso8601}"
uri = URI('http://labello/print')
res = Net::HTTP.post_form(
uri,
'text' => text,
'font' => 'lettergothic',
'align' => 'left',
'fontSize' => '42'
)
result = res.body
respond_to do |format|
format.html do
flash[:info] = "Labello responded with '#{result}'."
redirect_to @user
end
format.json { render json: {"result" => result } }
end
end
# GET /users/1/pay?amount=1.5
def payment
@user = User.find(params[:id])
@user.payment(BigDecimal.new(params[:amount]))
flash[:success] = "You just bought a drink and your new balance is #{show_amount(@user.balance)}. Thank you."
if (@user.balance < 0) then
flash[:warning] = "Your balance is below zero. Remember to compensate as soon as possible."
end
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
unless @drink.active?
@drink.active = true
@drink.save!
flash[:info] = "The drink you just bought has been set to 'available'."
end
@user.buy(@drink)
flash[:success] = "You just bought a drink and your new balance is #{show_amount(@user.balance)}. Thank you."
if (@user.balance < 0) then
flash[:warning] = "Your balance is below zero. Remember to compensate as soon as possible."
end
warn_user_if_audit
no_resp_redir @user.redirect ? users_url : @user
end
def user_params
params.require(:user).permit(:name, :email, :balance, :active, :audit, :redirect)
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
end