forked from chicago-tool-library/circulate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhold.rb
32 lines (26 loc) · 722 Bytes
/
hold.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
class Hold < ApplicationRecord
belongs_to :member
belongs_to :item, counter_cache: true
belongs_to :creator, class_name: "User"
belongs_to :loan, required: false
scope :active, -> { where("ended_at IS NULL") }
scope :ended, -> { where("ended_at IS NOT NULL") }
def self.active_hold_count_for_item(item)
active.where(item: item).count
end
def lend(loan, now: Time.current)
update!(
loan: loan,
ended_at: now
)
end
def active?
ended_at.blank?
end
def previous_active_holds
Hold.active.where("created_at < ?", created_at).where(item: item).where.not(member: member).order(:ended_at).to_a
end
def ready_for_pickup?
previous_active_holds.empty?
end
end