-
Notifications
You must be signed in to change notification settings - Fork 9
/
transaction_decorator.rb
92 lines (72 loc) · 2.21 KB
/
transaction_decorator.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
# frozen_string_literal: true
class TransactionDecorator < ApplicationDecorator
decorates_finders
decorates_association :transaction_balance
delegate_all
include Draper::LazyHelpers
def debit
amount.negative? ? number_to_currency(amount.abs) : ' '.html_safe
end
def credit
amount.positive? ? number_to_currency(amount) : ' '.html_safe
end
def amount_decorated
amount.negative? ? number_to_currency(amount.abs) : number_to_currency(amount)
end
def amount_form
amount.present? ? number_with_precision(amount.abs, precision: 2) : nil
end
def amount_color
amount.negative? ? 'has-text-danger' : 'has-text-success'
end
# Used to specify default value of Transaction type on form
def trx_type_credit_form
amount.present? ? amount.positive? : false
end
def trx_type_debit_form
if object.new_record?
true
else
amount.present? ? amount.negative? : false
end
end
###
def memo_decorated
memo? ? memo : '- None -'
end
def filename_size
attachment.attached? ? attachment.filename.to_s + ' (' + number_to_human_size(attachment.byte_size).to_s + ')' : nil
end
def filename_form
attachment.attached? ? filename_size : '- No receipt -'
end
def add_receipt_button_label
attachment.attached? ? 'Change receipt...' : 'Add receipt...'
end
def running_balance_display
number_to_currency(running_balance)
end
def trx_date_decorated
transaction.pending ? 'PENDING' : trx_date_display
end
def trx_date_display
trx_date.in_time_zone(current_user.timezone).strftime('%m/%d/%Y')
end
def trx_date_formatted
# trx_date.in_time_zone(current_user.timezone).strftime('%m/%d/%Y')
trx_date.in_time_zone(current_user.timezone).strftime('%Y-%m-%d')
end
def trx_date_form_value
# trx_date.present? ? trx_date_formatted : Time.current.strftime('%m/%d/%Y')
trx_date.present? ? trx_date_formatted : Time.current.strftime('%Y-%m-%d')
end
def created_at_decorated
created_at.in_time_zone(current_user.timezone).strftime('%b %d, %Y @ %I:%M %p %Z')
end
def name_too_long
description.length > 35
end
def trx_desc_display
name_too_long ? description[0..35] + '...' : description
end
end