Module: Shared::Loanable

Extended by:
ActiveSupport::Concern
Included in:
CollectionObject, Container, Otu
Defined in:
app/models/concerns/shared/loanable.rb

Overview

Shared code for data classes that can be loaned (used in LoanItem).

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#all_loan_itemsObject



119
120
121
# File 'app/models/concerns/shared/loanable.rb', line 119

def all_loan_items
  loan_items + container_loan_items
end

#all_loansObject



115
116
117
# File 'app/models/concerns/shared/loanable.rb', line 115

def all_loans
  loans + container_loans
end

#container_loan_itemsObject



127
128
129
130
131
132
133
# File 'app/models/concerns/shared/loanable.rb', line 127

def container_loan_items
  return [] if loan_item&.loan_item_object_type == 'Otu'
  return [] unless contained?
  container_ids = [container.id]
  container_ids += container.container_item.ancestors.pluck(:contained_object_id)
  LoanItem.where(loan_item_object_id: container_ids, loan_item_object_type: 'Container')
end

#container_loaned?Boolean

Returns False, Container.

Returns:

  • (Boolean)

    False, Container



58
59
60
61
62
63
64
65
# File 'app/models/concerns/shared/loanable.rb', line 58

def container_loaned?
  # OTUs are not containable, but they can be "virtual" loan items.
  if c = loaned_in_container
    c
  else
    false
  end
end

#container_loansObject



135
136
137
# File 'app/models/concerns/shared/loanable.rb', line 135

def container_loans
  Loan.joins(:loan_items).where(loan_items: container_loan_items).distinct
end

#container_times_loanedObject

Returns Integer.

Returns:

  • Integer



105
106
107
108
109
110
111
112
113
# File 'app/models/concerns/shared/loanable.rb', line 105

def container_times_loaned
  # OTUs can't be contained, everything else loanable can
  return 0 if loan_item&.loan_item_object_type == 'Otu'
  if container
    total = container_loan_items.count
  else
    0
  end
end

#current_loanObject



74
75
76
# File 'app/models/concerns/shared/loanable.rb', line 74

def current_loan
  current_loan_item&.loan
end

#current_loan_itemObject

Returns Boolean.

Returns:

  • Boolean



79
80
81
82
83
84
85
86
# File 'app/models/concerns/shared/loanable.rb', line 79

def current_loan_item
  if a = loan_item
    return a
  elsif c = container_loaned?
    return c.loan_item
  end
  false
end

#has_been_loaned?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/concerns/shared/loanable.rb', line 123

def has_been_loaned?
  times_loaned > 0
end

#is_loanable?Boolean

Returns True whether this object has this concern, NOT whether it is currently in possesion to be loaned out.

Returns:

  • (Boolean)

    True whether this object has this concern, NOT whether it is currently in possesion to be loaned out



38
39
40
# File 'app/models/concerns/shared/loanable.rb', line 38

def is_loanable?
  true
end

#loan_return_dateObject

Returns date, False.

Returns:

  • date, False



89
90
91
92
93
94
95
96
97
98
# File 'app/models/concerns/shared/loanable.rb', line 89

def loan_return_date
  if loan_item
    loan_item&.loan&.date_return_expected ? loan_item.loan.date_return_expected : false
  elsif c = loaned_in_container
    i = c.loan_item
    i.loan.date_return_expected? || false
  else
    false
  end
end

#loaned_in_containerObject

Returns Container, false the container that was loaned !! Not necessarily the immediate current container.

Returns:

  • Container, false the container that was loaned !! Not necessarily the immediate current container



45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/concerns/shared/loanable.rb', line 45

def loaned_in_container
  if self.class.is_containable?
    if contained?
      return container if container.loan_item # !! Do not use .on_loan? !!
      container.enclosing_containers.each do |c|
        return c if c.loan_item # !! Do not use .on_loan? !!
      end
    end
  end
  false
end

#on_loan?Boolean

Accounts for Containers

Returns:

  • (Boolean)

    Boolean



69
70
71
72
# File 'app/models/concerns/shared/loanable.rb', line 69

def on_loan?
  return true if container_loaned?
  loan_item.present? && !loan_item.returned?
end

#times_loanedObject



100
101
102
# File 'app/models/concerns/shared/loanable.rb', line 100

def times_loaned
  loans.count + container_times_loaned
end