This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
/
auction.rb
190 lines (161 loc) · 3.91 KB
/
auction.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'action_view'
# This is a wrapper around the basic AR model and should be used for
# selecting and rendering raw data from the model object.
module Presenter
class Auction
include ActiveModel::SerializerSupport
include ActionView::Helpers::DateHelper
include ActionView::Helpers::NumberHelper
include Rails.application.routes.url_helpers
default_url_options[:host] = ::Rails.application.routes.default_url_options[:host]
def initialize(auction)
@auction = auction
end
delegate(
:billable_to,
:created_at,
:delivery_deadline,
:delivery_url,
:description,
:end_datetime,
:github_repo,
:id,
:issue_url,
:model_name,
:published,
:read_attribute_for_serialization,
:start_datetime,
:start_price,
:summary,
:title,
:to_key,
:to_model,
:to_param,
:type,
:updated_at,
:lowest_bid,
to: :model
)
delegate(
:amount,
:time,
to: :lowest_bid,
prefix: :lowest_bid
)
delegate(
:bidder_duns_number,
:bidder_name,
to: :lowest_bid,
prefix: :lowest
)
delegate(
:future?,
:expiring?,
:over?,
:available?,
to: :auction_status
)
delegate(
:auction_rules_href,
:formatted_type,
:highlighted_bid,
:highlighted_bid_label,
:max_allowed_bid,
:partial_path,
:show_bids?,
:user_can_bid?,
:veiled_bids,
:winning_bid,
to: :auction_rules
)
def url
"/auctions/#{id.to_s}"
end
def bids?
bid_count > 0
end
def bids
@bids ||= model.bids.to_a
.map {|bid| decorated_bid(bid) }
.sort_by(&:created_at)
.reverse
end
def lowest_bids
model.lowest_bids.map {|b| decorated_bid(b) }
end
def lowest_bid
decorated_bid(model.lowest_bid)
end
def bid_count
bids.size
end
def starts_at
Presenter::DcTime.convert_and_format(model.start_datetime)
end
def ends_at
Presenter::DcTime.convert_and_format(model.end_datetime)
end
def starts_in
time_in_human(model.start_datetime)
end
def ends_in
time_in_human(model.end_datetime)
end
def delivery_deadline_expires_in
time_in_human(model.delivery_deadline)
end
def winning_bidder_id
winning_bid.bidder_id
end
def html_description
return '' if description.blank?
markdown.render(description)
end
def html_summary
return '' if summary.blank?
markdown.render(summary)
end
def human_start_time
if start_datetime < Time.now
# this method comes from the included date helpers
"#{distance_of_time_in_words(Time.now, start_datetime)} ago"
else
"in #{distance_of_time_in_words(Time.now, start_datetime)}"
end
end
private
def auction_rules
@auction_rules ||= RulesFactory.new(self).create
end
def markdown
# FIXME: Do we want the lax_spacing?
@markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML,
no_intra_emphasis: true,
autolink: true,
tables: true,
fenced_code_blocks: true,
lax_spacing: true)
end
def time_in_human(time)
distance = distance_of_time_in_words(Time.now, time)
if time < Time.now
"#{distance} ago"
else
"in #{distance}"
end
end
def auction_status
AuctionStatus.new(model)
end
def decorated_bid(bid)
if bid.present?
Presenter::Bid.new(bid)
else
Presenter::Bid::Null.new
end
end
def model
@auction
end
end
end