forked from chaosdorf/mete
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.rb
More file actions
64 lines (52 loc) · 1.17 KB
/
user.rb
File metadata and controls
64 lines (52 loc) · 1.17 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
# frozen_string_literal: true
class User < ApplicationRecord
validates :name, presence: true
validates :barcode, uniqueness: { conditions: -> { where.not(barcode: nil) } }
enum :avatar_provider, [ :gravatar, :webfinger ]
scope :order_by_name_asc, -> {
order(arel_table['name'].lower.asc)
}
before_save do |user|
if user.barcode == ''
user.barcode = nil
end
end
after_save do |user|
Audit.create! difference: user.balance - user.balance_before_last_save, drink: @purchased_drink, user: user.audit? ? user : nil
end
def deposit(amount)
with_lock do
self.balance += amount
save!
end
end
def buy(drink)
with_lock do
self.balance -= drink.price
@purchased_drink = drink
save!
end
end
def payment(amount)
with_lock do
self.balance -= amount
save!
end
end
def initial
first = name[0, 1].downcase
return first if first =~ /[a-z]/
'0'
end
def email
case avatar_provider
when 'gravatar'
avatar
when 'webfinger'
''
end
end
def as_json(options={})
super(except: [ :avatar_provider, :avatar ], methods: :email)
end
end