Class: Loan

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Housekeeping, Shared::DataAttributes, Shared::Depictions, Shared::HasRoles, Shared::Identifiable, Shared::IsData, Shared::Notable, Shared::Taggable, SoftValidation
Defined in:
app/models/loan.rb

Overview

A Loan is the metadata that wraps/describes an exchange of specimens.

Constant Summary

Constant Summary

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from SoftValidation

#clear_soft_validations, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations

Methods included from Housekeeping

#has_polymorphic_relationship?

Instance Attribute Details

- (DateTime) date_closed

Returns date at which loan has been fully resolved and requires no additional attention

Returns:

  • (DateTime)

    date at which loan has been fully resolved and requires no additional attention



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (DateTime) date_received

Returns date loan was recievied by recipient

Returns:

  • (DateTime)

    date loan was recievied by recipient



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (DateTime) date_requested

Returns date request was recieved by lender

Returns:

  • (DateTime)

    date request was recieved by lender



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (DateTime) date_return_expected

Returns date expected

Returns:

  • (DateTime)

    date expected



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (DateTime) date_sent

Returns date loan was delivered to post

Returns:

  • (DateTime)

    date loan was delivered to post



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (Integer) project_id

the project ID

Returns:

  • (Integer)


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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (String) recipient_address

Returns address loan sent to

Returns:

  • (String)

    address loan sent to



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (String) recipient_country

Returns:

  • (String)


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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (String) recipient_email

Returns email address of recipient

Returns:

  • (String)

    email address of recipient



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (String) recipient_honorarium

Returns as in Prof. Mrs. Dr. M. Mr. etc.

Returns:

  • (String)

    as in Prof. Mrs. Dr. M. Mr. etc.



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (String) recipient_phone

Returns phone number of recipient

Returns:

  • (String)

    phone number of recipient



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (String) request_method

Returns brief not as to how the request was made, not a controlled vocabulary

Returns:

  • (String)

    brief not as to how the request was made, not a controlled vocabulary



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (String) supervisor_email

Returns oe email of utlimately responsible party if recient can not be

Returns:

  • (String)

    oe email of utlimately responsible party if recient can not be



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

- (String) supervisor_phone

Returns phone # of utlimately responsible party if recient can not be

Returns:

  • (String)

    phone # of utlimately responsible party if recient can not be



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
# File 'app/models/loan.rb', line 58

class Loan < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::DataAttributes
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation
  include Shared::Depictions
  include Shared::HasRoles

  has_paper_trail :on => [:update] 

  has_many :loan_items, dependent: :restrict_with_error

  has_many :loan_recipient_roles, class_name: 'LoanRecipient', as: :role_object
  has_many :loan_supervisor_roles, class_name: 'LoanSupervisor', as: :role_object

  has_many :loan_recipients, through: :loan_recipient_roles, source: :person
  has_many :loan_supervisors, through: :loan_supervisor_roles, source: :person

  validates :supervisor_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!supervisor_email.blank?'
  validates :recipient_email, format: {with: User::VALID_EMAIL_REGEX}, if: '!recipient_email.blank?'

  validates :lender_address, presence: true

  validate :recieved_after_sent
  validate :returned_after_recieved
  validate :return_expected_after_sent

  accepts_nested_attributes_for :loan_items, allow_destroy: true, reject_if: :reject_loan_items
  accepts_nested_attributes_for :loan_supervisors, :loan_supervisor_roles, allow_destroy: true
  accepts_nested_attributes_for :loan_recipients, :loan_recipient_roles, allow_destroy: true

  scope :overdue, -> {where('now() > loans.date_return_expected AND date_closed IS NULL', Time.now.to_date)}

  def self.find_for_autocomplete(params)
    where('recipient_email LIKE ?', "#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a CSV version of the raw Loans table for the given scope
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  # @return [Scope] of CollectionObject
  def collection_objects
    list = collection_object_ids
    if list.empty?
      CollectionObject.where('false')
    else
      CollectionObject.find(list)
    end
  end

  def overdue?
    Time.now.to_date > date_return_expected && !date_closed.present?
  end

  def days_overdue
    (Time.now.to_date - date_return_expected).to_i
  end

  def days_until_due
    (date_return_expected - Time.now.to_date ).to_i
  end

  protected

  def recieved_after_sent
    errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
  end
  
  def returned_after_recieved
    errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
  end

  def return_expected_after_sent
    errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
  end
  
  # @return [Array] collection_object ids
  def collection_object_ids
    # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
    retval = []
    loan_items.pluck(:id).each { |item_id|
      item = LoanItem.find(item_id)
      case item.loan_item_object_type
        when /contain/i # if this item is a container
          retval.push(item.loan_item_object.all_collection_object_ids)
        when /object/i # if this item is a collection object
          retval.push(item.loan_item_object_id)
        else
          # right now (07/13/16), since there are no other models which are 'containable', do nothing
      end
    }
    retval.flatten
  end

  def reject_taxon_determinations(attributed)
    attributed['loan_item_object_type'].blank?
  end

  def reject_loan_items(attributed)
    attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
  end

end

Class Method Details

+ (Object) find_for_autocomplete(params)



94
95
96
# File 'app/models/loan.rb', line 94

def self.find_for_autocomplete(params)
  where('recipient_email LIKE ?', "#{params[:term]}%")
end

+ (CSV) generate_download(scope)

Generate a CSV version of the raw Loans table for the given scope Ripped from railscasts.com/episodes/362-exporting-csv-and-excel

Returns:

  • (CSV)


101
102
103
104
105
106
107
108
109
110
# File 'app/models/loan.rb', line 101

def self.generate_download(scope)
  CSV.generate do |csv|
    csv << column_names
    scope.order(id: :asc).each do |o|
      csv << o.attributes.values_at(*column_names).collect { |i|
        i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
      }
    end
  end
end

Instance Method Details

- (Array) collection_object_ids (protected)

Returns collection_object ids

Returns:

  • (Array)

    collection_object ids



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/loan.rb', line 149

def collection_object_ids
  # pile1 = Loan.joins(:loan_items).where(loan_items: {loan_id: self.id})
  retval = []
  loan_items.pluck(:id).each { |item_id|
    item = LoanItem.find(item_id)
    case item.loan_item_object_type
      when /contain/i # if this item is a container
        retval.push(item.loan_item_object.all_collection_object_ids)
      when /object/i # if this item is a collection object
        retval.push(item.loan_item_object_id)
      else
        # right now (07/13/16), since there are no other models which are 'containable', do nothing
    end
  }
  retval.flatten
end

- (Scope) collection_objects

Returns of CollectionObject

Returns:

  • (Scope)

    of CollectionObject



113
114
115
116
117
118
119
120
# File 'app/models/loan.rb', line 113

def collection_objects
  list = collection_object_ids
  if list.empty?
    CollectionObject.where('false')
  else
    CollectionObject.find(list)
  end
end

- (Object) days_overdue



126
127
128
# File 'app/models/loan.rb', line 126

def days_overdue
  (Time.now.to_date - date_return_expected).to_i
end

- (Object) days_until_due



130
131
132
# File 'app/models/loan.rb', line 130

def days_until_due
  (date_return_expected - Time.now.to_date ).to_i
end

- (Boolean) overdue?

Returns:

  • (Boolean)


122
123
124
# File 'app/models/loan.rb', line 122

def overdue?
  Time.now.to_date > date_return_expected && !date_closed.present?
end

- (Object) recieved_after_sent (protected)



136
137
138
# File 'app/models/loan.rb', line 136

def recieved_after_sent
  errors.add(:date_received, 'must be received on or after sent') if date_received.present? && date_sent.present? && date_received < date_sent 
end

- (Object) reject_loan_items(attributed) (protected)



170
171
172
# File 'app/models/loan.rb', line 170

def reject_loan_items(attributed)
  attributed['global_entity'].blank? && (attributed['loan_item_object_type'].blank? && attributed['loan_item_object_id'].blank?)
end

- (Object) reject_taxon_determinations(attributed) (protected)



166
167
168
# File 'app/models/loan.rb', line 166

def reject_taxon_determinations(attributed)
  attributed['loan_item_object_type'].blank?
end

- (Object) return_expected_after_sent (protected)



144
145
146
# File 'app/models/loan.rb', line 144

def return_expected_after_sent
  errors.add(:date_return_expected, 'must be expected after sent') if date_return_expected.present? && date_sent.present? && date_return_expected < date_sent
end

- (Object) returned_after_recieved (protected)



140
141
142
# File 'app/models/loan.rb', line 140

def returned_after_recieved
  errors.add(:date_closed, 'must be closed on or after received') if date_closed.present? && date_received.present? && date_closed < date_received 
end