Class: Auctify::Sale::Auction

Inherits:
Base show all
Includes:
AASM, AuctionCallbacks
Defined in:
app/models/auctify/sale/auction.rb

Constant Summary collapse

ATTRIBUTES_UNMUTABLE_AT_SOME_STATE =
i[ends_at offered_price]
DEPENDENT_ATTRIBUTES =
{
  ends_at: i[currently_ends_at],
  offered_price: i[current_price]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#auctioneer_commision_from_buyer, #auctioneer_commision_from_seller, #initialize, #item=, latest_published_sales_by_item_subtable, #publish!

Methods included from Behavior::Base

#auctify_id, #object_from_auctify_id

Constructor Details

This class inherits a constructor from Auctify::Sale::Base

Instance Attribute Details

#winning_bidObject

Returns the value of attribute winning_bid.



15
16
17
# File 'app/models/auctify/sale/auction.rb', line 15

def winning_bid
  @winning_bid
end

Instance Method Details

#allows_new_bidder_registrations?Boolean

Returns:

  • (Boolean)


254
255
256
# File 'app/models/auctify/sale/auction.rb', line 254

def allows_new_bidder_registrations?
  @allows_new_bidder_registrations ||= (in_sale? || accepted?)
end

#auction_prolonging_limit_in_secondsObject



267
268
269
# File 'app/models/auctify/sale/auction.rb', line 267

def auction_prolonging_limit_in_seconds
  pack&.auction_prolonging_limit_in_seconds || Auctify.configuration.auction_prolonging_limit_in_seconds
end

#bid!(bid) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/models/auctify/sale/auction.rb', line 158

def bid!(bid)
  ensure_registration(bid)

  ActiveRecord::Base.transaction do
    bid.created_at ||= Time.current

    bap = Auctify::BidsAppender.call(auction: self, bid: bid)

    if bap.success?
      after_bid_appended(bap)

      Yabeda.auctify.bids_count.increment({}, by: 1)
      times = ordered_applied_bids.limit(2).pluck(:created_at)
      Yabeda.auctify.time_between_bids.set({ auction_slug: slug }, (times.size == 1 ? 0 : times.first - times.second))
    else
      after_bid_not_appended(bap)
    end

    bap.success?
  end
end

#biddersObject



125
126
127
# File 'app/models/auctify/sale/auction.rb', line 125

def bidders
  @bidders ||= bidder_registrations.collect { |br| br.bidder }.sort_by(&:name)
end

#bidding_allowed_for?(bidder) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
261
# File 'app/models/auctify/sale/auction.rb', line 258

def bidding_allowed_for?(bidder)
  babm = bidding_allowed_by_method_for?(bidder)
  babm.nil? ? true : babm # if no method defined => allow
end

#bidding_resultObject



206
207
208
# File 'app/models/auctify/sale/auction.rb', line 206

def bidding_result
  Auctify::BidsAppender.call(auction: self, bid: nil).result
end

#current_max_price_for(bidder, bids_array: nil) ⇒ Object



238
239
240
241
242
243
244
# File 'app/models/auctify/sale/auction.rb', line 238

def current_max_price_for(bidder, bids_array: nil)
  bids_array ||= ordered_applied_bids.with_limit

  last_bidder_mx_bid = bids_array.detect { |bid| !bid.max_price.nil? && bid.bidder == bidder }

  last_bidder_mx_bid.blank? ? 0 : last_bidder_mx_bid.max_price
end

#current_minimal_bidObject



226
227
228
# File 'app/models/auctify/sale/auction.rb', line 226

def current_minimal_bid
  bidding_result.current_minimal_bid
end

#current_winnerObject



210
211
212
# File 'app/models/auctify/sale/auction.rb', line 210

def current_winner
  bidding_result.winner
end

#current_winning_bidObject



214
215
216
# File 'app/models/auctify/sale/auction.rb', line 214

def current_winning_bid
  bidding_result.winning_bid
end

#ends_at=(value) ⇒ Object



138
139
140
141
142
# File 'app/models/auctify/sale/auction.rb', line 138

def ends_at=(value)
  super

  self.currently_ends_at = value if currently_ends_at.present?
end

#locked_for_modifications?Boolean

Returns:

  • (Boolean)


263
264
265
# File 'app/models/auctify/sale/auction.rb', line 263

def locked_for_modifications?
  applied_bids_count.positive?
end

#minimal_bid_increase_amount_at(price, respect_first_bid: true) ⇒ Object



230
231
232
233
234
235
236
# File 'app/models/auctify/sale/auction.rb', line 230

def minimal_bid_increase_amount_at(price, respect_first_bid: true)
  return 0 if respect_first_bid && ordered_applied_bids.blank? # first bid can equal opening price
  return Auctify.configuration.require_bids_to_be_rounded_to if bid_steps_ladder.blank?

  _range, increase_step = bid_steps_ladder.detect { |range, step| range.cover?(price) }
  increase_step
end

#offered_price=(value) ⇒ Object



144
145
146
147
148
# File 'app/models/auctify/sale/auction.rb', line 144

def offered_price=(value)
  super

  self.current_price = value if current_price.present?
end

#open_for_bids?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'app/models/auctify/sale/auction.rb', line 246

def open_for_bids?
  in_sale? && Time.current <= currently_ends_at
end

#opening_priceObject



250
251
252
# File 'app/models/auctify/sale/auction.rb', line 250

def opening_price
  offered_price
end

#previous_winning_bid(relative_to_bid = nil) ⇒ Object



218
219
220
221
222
223
224
# File 'app/models/auctify/sale/auction.rb', line 218

def previous_winning_bid(relative_to_bid = nil)
  return nil if bids.empty?

  relative_to_bid ||= current_winning_bid
  considered_bids = bids.ordered.drop_while { |b| b != relative_to_bid }
  considered_bids.second
end

#published=(value) ⇒ Object



129
130
131
132
133
134
135
136
# File 'app/models/auctify/sale/auction.rb', line 129

def published=(value)
  super

  if published?
    accept_offer if offered?
    start_sale if accepted?
  end
end

#recalculate_bidding!Object



192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/models/auctify/sale/auction.rb', line 192

def recalculate_bidding!
  self.applied_bids_count = ordered_applied_bids.size

  if applied_bids_count.zero?
    self.current_price = offered_price
  else
    winning_price = bidding_result.winning_bid.price
    self.current_price = winning_price if current_price > winning_price
  end

  save!
end

#succesfull_bid!(price:, winner:, time:) ⇒ Object

callback from bid_appender



181
182
183
184
185
186
187
188
189
190
# File 'app/models/auctify/sale/auction.rb', line 181

def succesfull_bid!(price:, winner:, time:)
  return false if price < current_price || time.blank?

  self.current_price = price
  self.current_winner = winner
  self.applied_bids_count = ordered_applied_bids.size
  extend_end_time(time)

  save!
end

#success?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
# File 'app/models/auctify/sale/auction.rb', line 150

def success?
  return nil if offered? || accepted? || refused? || cancelled? || in_sale? # or raise error?
  return true if auctioned_successfully? || sold?
  return false if auctioned_unsuccessfully? || not_sold?

  applied_bids_count.positive? && ((reserve_price || 0) <= current_price)
end