Module: LoansHelper

Defined in:
app/helpers/loans_helper.rb

Instance Method Summary collapse

Instance Method Details

#keywords_on_loanable_itemsObject



105
106
107
# File 'app/helpers/loans_helper.rb', line 105

def keywords_on_loanable_items
  Keyword.joins(:tags).where(project_id: sessions_current_project_id).where(tags: {tag_object_type: ['Container', 'Otu', 'CollectionObject']}).distinct.all
end

#label_for_loan(loan) ⇒ Object



13
14
15
16
# File 'app/helpers/loans_helper.rb', line 13

def label_for_loan(loan)
  s = "loan #{loan.id}"
  s << loan.identifiers&.pluck(:cached)&.join(', ')
end

#loan_autocomplete_tag(loan) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/loans_helper.rb', line 24

def loan_autocomplete_tag(loan)
  return nil if loan.nil?
  [
    (:span, (identifier_tag(loan_identifier(loan)) || loan.id), class: [:feedback, 'feedback-thin', 'feedback-primary']),
    loan.loan_recipients.collect{|a| a.name}.join(', '),
    loan.recipient_email,
    loan.date_sent,
    loan.recipient_address,
    loan_status_tag(loan)
  ].delete_if{|b| b.blank? }.join(' - ').gsub(/\n/, '; ').html_safe
end

#loan_due_back_tag(loan) ⇒ Object



99
100
101
102
103
# File 'app/helpers/loans_helper.rb', line 99

def loan_due_back_tag(loan)
  'Due back on ' +
    ( loan.date_return_expected.present? ? loan.date_return_expected.to_s : 'NOT PROVIDED' ) +
    '.'
end

#loan_identifier(loan) ⇒ Object



36
37
38
# File 'app/helpers/loans_helper.rb', line 36

def loan_identifier(loan)
  loan.identifiers.where(type: 'Identifier::Local::LoanCode').first
end


50
51
52
53
# File 'app/helpers/loans_helper.rb', line 50

def loan_link(loan)
  return nil if loan.nil?
  link_to(loan_tag(loan).html_safe, loan)
end

#loan_overdue_tag(loan) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/loans_helper.rb', line 86

def loan_overdue_tag(loan)
  if loan.date_return_expected.present?
    if loan.overdue?
      "#{loan.days_overdue} days overdue."
    else
      "#{loan.days_until_due} days until due."
    end
  else
    (:span, 'Due date NOT PROVIDED.', data: {icon: :warning})
  end

end

#loan_recipients_tag(loan) ⇒ Object



18
19
20
21
22
# File 'app/helpers/loans_helper.rb', line 18

def loan_recipients_tag(loan)
  return nil if loan.nil?
  recipients = loan.loan_recipients.collect{|lr| person_tag(lr)}.join.html_safe
  recipients.blank? ? 'No recipients defined!' : recipients
end

#loan_status_tag(loan) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'app/helpers/loans_helper.rb', line 40

def loan_status_tag(loan)
  return nil if loan.nil?
  return tag.span('Returned/canceled', class: [:feedback, 'feedback-thin', 'feedback-success']) if loan.date_closed.present?
  if loan.date_return_expected.present?
    return tag.span("Due in #{distance_of_time_in_words_to_now(loan.date_return_expected)}", class: [:feedback, 'feedback-thin', 'feedback-information']) if loan.date_return_expected > Time.now
    return tag.span("#{distance_of_time_in_words(Time.now, loan.date_return_expected)} overdue", class: [:feedback, 'feedback-thin', 'feedback-danger']) if loan.date_return_expected < Time.now
  end
  return tag.span("Lost/destroyed", class: [:feedback, 'feedback-thin', 'feedback-warning']) if loan.loan_items.count == loan.loan_items.where(disposition: ['Lost', 'Destroyed']).distinct.count
end

#loan_tag(loan) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'app/helpers/loans_helper.rb', line 3

def loan_tag(loan)
  return nil if loan.nil?
  [
    (:span, (identifier_tag(loan_identifier(loan)) || loan.id), class: [:feedback, 'feedback-thin', 'feedback-primary']),
    loan.loan_recipients.collect{|a| a.name}.join(', '),
    loan.recipient_email,
    loan.date_sent,
    loan.recipient_address].delete_if{|b| b.blank? }.join(' - ').gsub(/\n/, '; ').html_safe
end

#loans_search_formObject



55
56
57
# File 'app/helpers/loans_helper.rb', line 55

def loans_search_form
  render('/loans/quick_search_form')
end

#object_loan_history_tag(object) ⇒ Object



69
70
71
# File 'app/helpers/loans_helper.rb', line 69

def object_loan_history_tag(object)
  (:li, "Loaned #{object.times_loaned} times.")
end

#object_loan_status_tag(object) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'app/helpers/loans_helper.rb', line 59

def object_loan_status_tag(object)
  if object.has_loans? && object.has_been_loaned?
    (:h3, 'Loan status') +
      (:ul) do
        (on_loan_tag(object) +
         object_loan_history_tag(object)).html_safe
      end
  end
end

#on_loan_tag(object) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/loans_helper.rb', line 73

def on_loan_tag(object)
  if object.has_loans? && object.on_loan?
    (:li) do
      ['On ' +  link_to('loan', object.loan) + '.',
       loan_overdue_tag(object.loan),
       loan_due_back_tag(object.loan)
      ].join(' ').html_safe
    end
  else
    ''
  end
end