Class: LoanItem
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- LoanItem
- Defined in:
- app/models/loan_item.rb
Overview
A loan item is a CollectionObject, Container, or historical reference to something that has been loaned via (Otu)
Thanks to neanderslob.com/2015/11/03/polymorphic-associations-the-smart-way-using-global-ids/ for global_entity.
Constant Summary collapse
- STATUS =
['Destroyed', 'Donated', 'Loaned on', 'Lost', 'Retained', 'Returned'].freeze
Instance Attribute Summary collapse
-
#date_returned ⇒ DateTime
The date the item was returned.
-
#date_returned_jquery ⇒ Object
Returns the value of attribute date_returned_jquery.
-
#disposition ⇒ String
An evolving controlled vocabulary used to differentiate loan object status when it differs from that of the overal loan, see LoanItem::STATUS.
-
#loan_id ⇒ Integer
Id of the loan.
-
#loan_object_id ⇒ Integer
Polymorphic, the id of the Container, CollectionObject or Otu.
-
#loan_object_type ⇒ String
Polymorphic- one of Container, CollectionObject, or Otu.
-
#position ⇒ Integer
Sorts the items in relation to the loan.
-
#project_id ⇒ Integer
the project ID.
-
#total ⇒ Integer
When type is OTU an arbitrary total can be provided.
Class Method Summary collapse
-
.batch_create(params) ⇒ Object
TODO: param handling is currently all kinds of “meh”.
- .batch_create_from_pinboard(loan_id, project_id, user_id, klass) ⇒ Object
- .batch_create_from_tags(keyword_id, klass, loan_id) ⇒ Object
- .batch_determine_loan_items(ids: [], params: {}) ⇒ Object
Instance Method Summary collapse
-
#available_for_loan ⇒ Object
protected
Is not already in a loan item if CollectionObject/Container.
-
#determinable_objects ⇒ Array
All objects that can have a taxon determination applied to them for htis loan item.
- #global_entity ⇒ Object
- #global_entity=(entity) ⇒ Object
-
#loan_object_is_loanable ⇒ Object
protected
Code, not out-on-loan check.
- #object_loanable_check ⇒ Object protected
- #returned? ⇒ Boolean
-
#total_items ⇒ Integer?
TODO: this does not factor in nested items in a container.
- #total_provided_only_when_otu ⇒ Object protected
Methods included from Shared::IsData
#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar
Methods included from Shared::Tags
#reject_tags, #tag_with, #tagged?, #tagged_with?
Methods included from Shared::Notes
#concatenated_notes_string, #reject_notes
Methods included from Shared::DataAttributes
#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes
Methods included from Housekeeping
#has_polymorphic_relationship?
Methods inherited from ApplicationRecord
Instance Attribute Details
#date_returned ⇒ DateTime
The date the item was returned.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/models/loan_item.rb', line 38 class LoanItem < ApplicationRecord acts_as_list scope: [:loan, :project_id] include Housekeeping include Shared::DataAttributes include Shared::Notes include Shared::Tags include Shared::IsData attr_accessor :date_returned_jquery STATUS = ['Destroyed', 'Donated', 'Loaned on', 'Lost', 'Retained', 'Returned'].freeze belongs_to :loan, inverse_of: :loan_items belongs_to :loan_item_object, polymorphic: true validates_presence_of :loan_item_object validates :loan, presence: true # validates_uniqueness_of :loan, scope: [:loan_item_object_type, :loan_item_object_id] validate :total_provided_only_when_otu validate :loan_object_is_loanable validate :available_for_loan validates_inclusion_of :disposition, in: STATUS, if: -> {!disposition.blank?} def global_entity self.loan_item_object.to_global_id if self.loan_item_object.present? end def global_entity=(entity) self.loan_item_object = GlobalID::Locator.locate entity end def date_returned_jquery=(date) self.date_returned = date.gsub(/(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3') end def date_returned_jquery self.date_returned end def returned? !date_returned.blank? end # @return [Integer, nil] # the total items this loan line item represent # TODO: this does not factor in nested items in a container def total_items case loan_item_object_type when 'Otu' total ? total : nil when 'Container' loan_item_object.container_items.try(:count) when 'CollectionObject' loan_item_object.total.to_i else nil end end # @return [Array] # all objects that can have a taxon determination applied to them for htis loan item def determinable_objects # this loan item which may be a container, an OTU, or a collection object case loan_item_object_type when /contain/i # if this item is a container, dig into the container for the collection objects themselves loan_item_object.collection_objects when /object/i # if this item is a collection object, just add the object [loan_item_object] when /otu/i # not strictly needed, but helps keep track of what the loan_item is. [] # can't use an OTU as a determination object. end end # @params :ids -> an ID of a loan_item def self.batch_determine_loan_items(ids: [], params: {}) return false if ids.empty? # these objects will be created/persisted to be used for each of the loan items identified by the input ids td = TaxonDetermination.new(params) # build a td from the input data begin LoanItem.transaction do item_list = [] # Array of objects that can have a taxon determination LoanItem.where(id: ids).each do |li| item_list.push li.determinable_objects end item_list.flatten! first = item_list.pop td.biological_collection_object = first td.save! # create and save the first one so we can dup it in the next step item_list.each do |item| n = td.dup n.determiners << td.determiners n.biological_collection_object = item n.save n.move_to_top end end rescue ActiveRecord::RecordInvalid return false end true end # TODO: param handling is currently all kinds of "meh" def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:loan_id]) when 'pinboard' batch_create_from_pinboard(params[:loan_id], params[:project_id], params[:user_id], params[:klass]) end end def self.(keyword_id, klass, loan_id) created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else Tag.where(keyword_id: keyword_id).where(tag_object_type: ['Container', 'Otu', 'CollectionObject']).distinct.all.each do |o| created.push LoanItem.create!(loan_item_object: o.tag_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end def self.batch_create_from_pinboard(loan_id, project_id, user_id, klass) return false if loan_id.blank? || project_id.blank? || user_id.blank? created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id, pinned_object_type: klass}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: ['Container', 'Otu', 'CollectionObject']).all.each do |o| created.push LoanItem.create!(loan_item_object: o.pinned_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end protected def object_loanable_check loan_item_object && loan_item_object.respond_to?(:is_loanable?) end def total_provided_only_when_otu errors.add(:total, 'only providable when item is an OTU.') if total && loan_item_object_type != 'Otu' end # Code, not out-on-loan check def loan_object_is_loanable if !persisted? # if it is, then this check should not be necessary if !object_loanable_check errors.add(:loan_item_object, 'is not loanble') end end end # Is not already in a loan item if CollectionObject/Container def available_for_loan if !persisted? # if it is, then this check should not be necessary if object_loanable_check if loan_item_object_type == 'Otu' true else if loan_item_object.on_loan? #loan_item_object.loan_items.where.not(id: id).any? errors.add(:loan_item_object, 'is already on loan') end end end end end end |
#date_returned_jquery ⇒ Object
Returns the value of attribute date_returned_jquery.
47 48 49 |
# File 'app/models/loan_item.rb', line 47 def date_returned_jquery @date_returned_jquery end |
#disposition ⇒ String
Returns an evolving controlled vocabulary used to differentiate loan object status when it differs from that of the overal loan, see LoanItem::STATUS.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/models/loan_item.rb', line 38 class LoanItem < ApplicationRecord acts_as_list scope: [:loan, :project_id] include Housekeeping include Shared::DataAttributes include Shared::Notes include Shared::Tags include Shared::IsData attr_accessor :date_returned_jquery STATUS = ['Destroyed', 'Donated', 'Loaned on', 'Lost', 'Retained', 'Returned'].freeze belongs_to :loan, inverse_of: :loan_items belongs_to :loan_item_object, polymorphic: true validates_presence_of :loan_item_object validates :loan, presence: true # validates_uniqueness_of :loan, scope: [:loan_item_object_type, :loan_item_object_id] validate :total_provided_only_when_otu validate :loan_object_is_loanable validate :available_for_loan validates_inclusion_of :disposition, in: STATUS, if: -> {!disposition.blank?} def global_entity self.loan_item_object.to_global_id if self.loan_item_object.present? end def global_entity=(entity) self.loan_item_object = GlobalID::Locator.locate entity end def date_returned_jquery=(date) self.date_returned = date.gsub(/(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3') end def date_returned_jquery self.date_returned end def returned? !date_returned.blank? end # @return [Integer, nil] # the total items this loan line item represent # TODO: this does not factor in nested items in a container def total_items case loan_item_object_type when 'Otu' total ? total : nil when 'Container' loan_item_object.container_items.try(:count) when 'CollectionObject' loan_item_object.total.to_i else nil end end # @return [Array] # all objects that can have a taxon determination applied to them for htis loan item def determinable_objects # this loan item which may be a container, an OTU, or a collection object case loan_item_object_type when /contain/i # if this item is a container, dig into the container for the collection objects themselves loan_item_object.collection_objects when /object/i # if this item is a collection object, just add the object [loan_item_object] when /otu/i # not strictly needed, but helps keep track of what the loan_item is. [] # can't use an OTU as a determination object. end end # @params :ids -> an ID of a loan_item def self.batch_determine_loan_items(ids: [], params: {}) return false if ids.empty? # these objects will be created/persisted to be used for each of the loan items identified by the input ids td = TaxonDetermination.new(params) # build a td from the input data begin LoanItem.transaction do item_list = [] # Array of objects that can have a taxon determination LoanItem.where(id: ids).each do |li| item_list.push li.determinable_objects end item_list.flatten! first = item_list.pop td.biological_collection_object = first td.save! # create and save the first one so we can dup it in the next step item_list.each do |item| n = td.dup n.determiners << td.determiners n.biological_collection_object = item n.save n.move_to_top end end rescue ActiveRecord::RecordInvalid return false end true end # TODO: param handling is currently all kinds of "meh" def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:loan_id]) when 'pinboard' batch_create_from_pinboard(params[:loan_id], params[:project_id], params[:user_id], params[:klass]) end end def self.(keyword_id, klass, loan_id) created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else Tag.where(keyword_id: keyword_id).where(tag_object_type: ['Container', 'Otu', 'CollectionObject']).distinct.all.each do |o| created.push LoanItem.create!(loan_item_object: o.tag_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end def self.batch_create_from_pinboard(loan_id, project_id, user_id, klass) return false if loan_id.blank? || project_id.blank? || user_id.blank? created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id, pinned_object_type: klass}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: ['Container', 'Otu', 'CollectionObject']).all.each do |o| created.push LoanItem.create!(loan_item_object: o.pinned_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end protected def object_loanable_check loan_item_object && loan_item_object.respond_to?(:is_loanable?) end def total_provided_only_when_otu errors.add(:total, 'only providable when item is an OTU.') if total && loan_item_object_type != 'Otu' end # Code, not out-on-loan check def loan_object_is_loanable if !persisted? # if it is, then this check should not be necessary if !object_loanable_check errors.add(:loan_item_object, 'is not loanble') end end end # Is not already in a loan item if CollectionObject/Container def available_for_loan if !persisted? # if it is, then this check should not be necessary if object_loanable_check if loan_item_object_type == 'Otu' true else if loan_item_object.on_loan? #loan_item_object.loan_items.where.not(id: id).any? errors.add(:loan_item_object, 'is already on loan') end end end end end end |
#loan_id ⇒ Integer
Id of the loan
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/models/loan_item.rb', line 38 class LoanItem < ApplicationRecord acts_as_list scope: [:loan, :project_id] include Housekeeping include Shared::DataAttributes include Shared::Notes include Shared::Tags include Shared::IsData attr_accessor :date_returned_jquery STATUS = ['Destroyed', 'Donated', 'Loaned on', 'Lost', 'Retained', 'Returned'].freeze belongs_to :loan, inverse_of: :loan_items belongs_to :loan_item_object, polymorphic: true validates_presence_of :loan_item_object validates :loan, presence: true # validates_uniqueness_of :loan, scope: [:loan_item_object_type, :loan_item_object_id] validate :total_provided_only_when_otu validate :loan_object_is_loanable validate :available_for_loan validates_inclusion_of :disposition, in: STATUS, if: -> {!disposition.blank?} def global_entity self.loan_item_object.to_global_id if self.loan_item_object.present? end def global_entity=(entity) self.loan_item_object = GlobalID::Locator.locate entity end def date_returned_jquery=(date) self.date_returned = date.gsub(/(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3') end def date_returned_jquery self.date_returned end def returned? !date_returned.blank? end # @return [Integer, nil] # the total items this loan line item represent # TODO: this does not factor in nested items in a container def total_items case loan_item_object_type when 'Otu' total ? total : nil when 'Container' loan_item_object.container_items.try(:count) when 'CollectionObject' loan_item_object.total.to_i else nil end end # @return [Array] # all objects that can have a taxon determination applied to them for htis loan item def determinable_objects # this loan item which may be a container, an OTU, or a collection object case loan_item_object_type when /contain/i # if this item is a container, dig into the container for the collection objects themselves loan_item_object.collection_objects when /object/i # if this item is a collection object, just add the object [loan_item_object] when /otu/i # not strictly needed, but helps keep track of what the loan_item is. [] # can't use an OTU as a determination object. end end # @params :ids -> an ID of a loan_item def self.batch_determine_loan_items(ids: [], params: {}) return false if ids.empty? # these objects will be created/persisted to be used for each of the loan items identified by the input ids td = TaxonDetermination.new(params) # build a td from the input data begin LoanItem.transaction do item_list = [] # Array of objects that can have a taxon determination LoanItem.where(id: ids).each do |li| item_list.push li.determinable_objects end item_list.flatten! first = item_list.pop td.biological_collection_object = first td.save! # create and save the first one so we can dup it in the next step item_list.each do |item| n = td.dup n.determiners << td.determiners n.biological_collection_object = item n.save n.move_to_top end end rescue ActiveRecord::RecordInvalid return false end true end # TODO: param handling is currently all kinds of "meh" def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:loan_id]) when 'pinboard' batch_create_from_pinboard(params[:loan_id], params[:project_id], params[:user_id], params[:klass]) end end def self.(keyword_id, klass, loan_id) created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else Tag.where(keyword_id: keyword_id).where(tag_object_type: ['Container', 'Otu', 'CollectionObject']).distinct.all.each do |o| created.push LoanItem.create!(loan_item_object: o.tag_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end def self.batch_create_from_pinboard(loan_id, project_id, user_id, klass) return false if loan_id.blank? || project_id.blank? || user_id.blank? created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id, pinned_object_type: klass}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: ['Container', 'Otu', 'CollectionObject']).all.each do |o| created.push LoanItem.create!(loan_item_object: o.pinned_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end protected def object_loanable_check loan_item_object && loan_item_object.respond_to?(:is_loanable?) end def total_provided_only_when_otu errors.add(:total, 'only providable when item is an OTU.') if total && loan_item_object_type != 'Otu' end # Code, not out-on-loan check def loan_object_is_loanable if !persisted? # if it is, then this check should not be necessary if !object_loanable_check errors.add(:loan_item_object, 'is not loanble') end end end # Is not already in a loan item if CollectionObject/Container def available_for_loan if !persisted? # if it is, then this check should not be necessary if object_loanable_check if loan_item_object_type == 'Otu' true else if loan_item_object.on_loan? #loan_item_object.loan_items.where.not(id: id).any? errors.add(:loan_item_object, 'is already on loan') end end end end end end |
#loan_object_id ⇒ Integer
Polymorphic, the id of the Container, CollectionObject or Otu
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/models/loan_item.rb', line 38 class LoanItem < ApplicationRecord acts_as_list scope: [:loan, :project_id] include Housekeeping include Shared::DataAttributes include Shared::Notes include Shared::Tags include Shared::IsData attr_accessor :date_returned_jquery STATUS = ['Destroyed', 'Donated', 'Loaned on', 'Lost', 'Retained', 'Returned'].freeze belongs_to :loan, inverse_of: :loan_items belongs_to :loan_item_object, polymorphic: true validates_presence_of :loan_item_object validates :loan, presence: true # validates_uniqueness_of :loan, scope: [:loan_item_object_type, :loan_item_object_id] validate :total_provided_only_when_otu validate :loan_object_is_loanable validate :available_for_loan validates_inclusion_of :disposition, in: STATUS, if: -> {!disposition.blank?} def global_entity self.loan_item_object.to_global_id if self.loan_item_object.present? end def global_entity=(entity) self.loan_item_object = GlobalID::Locator.locate entity end def date_returned_jquery=(date) self.date_returned = date.gsub(/(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3') end def date_returned_jquery self.date_returned end def returned? !date_returned.blank? end # @return [Integer, nil] # the total items this loan line item represent # TODO: this does not factor in nested items in a container def total_items case loan_item_object_type when 'Otu' total ? total : nil when 'Container' loan_item_object.container_items.try(:count) when 'CollectionObject' loan_item_object.total.to_i else nil end end # @return [Array] # all objects that can have a taxon determination applied to them for htis loan item def determinable_objects # this loan item which may be a container, an OTU, or a collection object case loan_item_object_type when /contain/i # if this item is a container, dig into the container for the collection objects themselves loan_item_object.collection_objects when /object/i # if this item is a collection object, just add the object [loan_item_object] when /otu/i # not strictly needed, but helps keep track of what the loan_item is. [] # can't use an OTU as a determination object. end end # @params :ids -> an ID of a loan_item def self.batch_determine_loan_items(ids: [], params: {}) return false if ids.empty? # these objects will be created/persisted to be used for each of the loan items identified by the input ids td = TaxonDetermination.new(params) # build a td from the input data begin LoanItem.transaction do item_list = [] # Array of objects that can have a taxon determination LoanItem.where(id: ids).each do |li| item_list.push li.determinable_objects end item_list.flatten! first = item_list.pop td.biological_collection_object = first td.save! # create and save the first one so we can dup it in the next step item_list.each do |item| n = td.dup n.determiners << td.determiners n.biological_collection_object = item n.save n.move_to_top end end rescue ActiveRecord::RecordInvalid return false end true end # TODO: param handling is currently all kinds of "meh" def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:loan_id]) when 'pinboard' batch_create_from_pinboard(params[:loan_id], params[:project_id], params[:user_id], params[:klass]) end end def self.(keyword_id, klass, loan_id) created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else Tag.where(keyword_id: keyword_id).where(tag_object_type: ['Container', 'Otu', 'CollectionObject']).distinct.all.each do |o| created.push LoanItem.create!(loan_item_object: o.tag_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end def self.batch_create_from_pinboard(loan_id, project_id, user_id, klass) return false if loan_id.blank? || project_id.blank? || user_id.blank? created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id, pinned_object_type: klass}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: ['Container', 'Otu', 'CollectionObject']).all.each do |o| created.push LoanItem.create!(loan_item_object: o.pinned_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end protected def object_loanable_check loan_item_object && loan_item_object.respond_to?(:is_loanable?) end def total_provided_only_when_otu errors.add(:total, 'only providable when item is an OTU.') if total && loan_item_object_type != 'Otu' end # Code, not out-on-loan check def loan_object_is_loanable if !persisted? # if it is, then this check should not be necessary if !object_loanable_check errors.add(:loan_item_object, 'is not loanble') end end end # Is not already in a loan item if CollectionObject/Container def available_for_loan if !persisted? # if it is, then this check should not be necessary if object_loanable_check if loan_item_object_type == 'Otu' true else if loan_item_object.on_loan? #loan_item_object.loan_items.where.not(id: id).any? errors.add(:loan_item_object, 'is already on loan') end end end end end end |
#loan_object_type ⇒ String
Polymorphic- one of Container, CollectionObject, or Otu
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/models/loan_item.rb', line 38 class LoanItem < ApplicationRecord acts_as_list scope: [:loan, :project_id] include Housekeeping include Shared::DataAttributes include Shared::Notes include Shared::Tags include Shared::IsData attr_accessor :date_returned_jquery STATUS = ['Destroyed', 'Donated', 'Loaned on', 'Lost', 'Retained', 'Returned'].freeze belongs_to :loan, inverse_of: :loan_items belongs_to :loan_item_object, polymorphic: true validates_presence_of :loan_item_object validates :loan, presence: true # validates_uniqueness_of :loan, scope: [:loan_item_object_type, :loan_item_object_id] validate :total_provided_only_when_otu validate :loan_object_is_loanable validate :available_for_loan validates_inclusion_of :disposition, in: STATUS, if: -> {!disposition.blank?} def global_entity self.loan_item_object.to_global_id if self.loan_item_object.present? end def global_entity=(entity) self.loan_item_object = GlobalID::Locator.locate entity end def date_returned_jquery=(date) self.date_returned = date.gsub(/(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3') end def date_returned_jquery self.date_returned end def returned? !date_returned.blank? end # @return [Integer, nil] # the total items this loan line item represent # TODO: this does not factor in nested items in a container def total_items case loan_item_object_type when 'Otu' total ? total : nil when 'Container' loan_item_object.container_items.try(:count) when 'CollectionObject' loan_item_object.total.to_i else nil end end # @return [Array] # all objects that can have a taxon determination applied to them for htis loan item def determinable_objects # this loan item which may be a container, an OTU, or a collection object case loan_item_object_type when /contain/i # if this item is a container, dig into the container for the collection objects themselves loan_item_object.collection_objects when /object/i # if this item is a collection object, just add the object [loan_item_object] when /otu/i # not strictly needed, but helps keep track of what the loan_item is. [] # can't use an OTU as a determination object. end end # @params :ids -> an ID of a loan_item def self.batch_determine_loan_items(ids: [], params: {}) return false if ids.empty? # these objects will be created/persisted to be used for each of the loan items identified by the input ids td = TaxonDetermination.new(params) # build a td from the input data begin LoanItem.transaction do item_list = [] # Array of objects that can have a taxon determination LoanItem.where(id: ids).each do |li| item_list.push li.determinable_objects end item_list.flatten! first = item_list.pop td.biological_collection_object = first td.save! # create and save the first one so we can dup it in the next step item_list.each do |item| n = td.dup n.determiners << td.determiners n.biological_collection_object = item n.save n.move_to_top end end rescue ActiveRecord::RecordInvalid return false end true end # TODO: param handling is currently all kinds of "meh" def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:loan_id]) when 'pinboard' batch_create_from_pinboard(params[:loan_id], params[:project_id], params[:user_id], params[:klass]) end end def self.(keyword_id, klass, loan_id) created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else Tag.where(keyword_id: keyword_id).where(tag_object_type: ['Container', 'Otu', 'CollectionObject']).distinct.all.each do |o| created.push LoanItem.create!(loan_item_object: o.tag_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end def self.batch_create_from_pinboard(loan_id, project_id, user_id, klass) return false if loan_id.blank? || project_id.blank? || user_id.blank? created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id, pinned_object_type: klass}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: ['Container', 'Otu', 'CollectionObject']).all.each do |o| created.push LoanItem.create!(loan_item_object: o.pinned_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end protected def object_loanable_check loan_item_object && loan_item_object.respond_to?(:is_loanable?) end def total_provided_only_when_otu errors.add(:total, 'only providable when item is an OTU.') if total && loan_item_object_type != 'Otu' end # Code, not out-on-loan check def loan_object_is_loanable if !persisted? # if it is, then this check should not be necessary if !object_loanable_check errors.add(:loan_item_object, 'is not loanble') end end end # Is not already in a loan item if CollectionObject/Container def available_for_loan if !persisted? # if it is, then this check should not be necessary if object_loanable_check if loan_item_object_type == 'Otu' true else if loan_item_object.on_loan? #loan_item_object.loan_items.where.not(id: id).any? errors.add(:loan_item_object, 'is already on loan') end end end end end end |
#position ⇒ Integer
Returns Sorts the items in relation to the loan.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/models/loan_item.rb', line 38 class LoanItem < ApplicationRecord acts_as_list scope: [:loan, :project_id] include Housekeeping include Shared::DataAttributes include Shared::Notes include Shared::Tags include Shared::IsData attr_accessor :date_returned_jquery STATUS = ['Destroyed', 'Donated', 'Loaned on', 'Lost', 'Retained', 'Returned'].freeze belongs_to :loan, inverse_of: :loan_items belongs_to :loan_item_object, polymorphic: true validates_presence_of :loan_item_object validates :loan, presence: true # validates_uniqueness_of :loan, scope: [:loan_item_object_type, :loan_item_object_id] validate :total_provided_only_when_otu validate :loan_object_is_loanable validate :available_for_loan validates_inclusion_of :disposition, in: STATUS, if: -> {!disposition.blank?} def global_entity self.loan_item_object.to_global_id if self.loan_item_object.present? end def global_entity=(entity) self.loan_item_object = GlobalID::Locator.locate entity end def date_returned_jquery=(date) self.date_returned = date.gsub(/(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3') end def date_returned_jquery self.date_returned end def returned? !date_returned.blank? end # @return [Integer, nil] # the total items this loan line item represent # TODO: this does not factor in nested items in a container def total_items case loan_item_object_type when 'Otu' total ? total : nil when 'Container' loan_item_object.container_items.try(:count) when 'CollectionObject' loan_item_object.total.to_i else nil end end # @return [Array] # all objects that can have a taxon determination applied to them for htis loan item def determinable_objects # this loan item which may be a container, an OTU, or a collection object case loan_item_object_type when /contain/i # if this item is a container, dig into the container for the collection objects themselves loan_item_object.collection_objects when /object/i # if this item is a collection object, just add the object [loan_item_object] when /otu/i # not strictly needed, but helps keep track of what the loan_item is. [] # can't use an OTU as a determination object. end end # @params :ids -> an ID of a loan_item def self.batch_determine_loan_items(ids: [], params: {}) return false if ids.empty? # these objects will be created/persisted to be used for each of the loan items identified by the input ids td = TaxonDetermination.new(params) # build a td from the input data begin LoanItem.transaction do item_list = [] # Array of objects that can have a taxon determination LoanItem.where(id: ids).each do |li| item_list.push li.determinable_objects end item_list.flatten! first = item_list.pop td.biological_collection_object = first td.save! # create and save the first one so we can dup it in the next step item_list.each do |item| n = td.dup n.determiners << td.determiners n.biological_collection_object = item n.save n.move_to_top end end rescue ActiveRecord::RecordInvalid return false end true end # TODO: param handling is currently all kinds of "meh" def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:loan_id]) when 'pinboard' batch_create_from_pinboard(params[:loan_id], params[:project_id], params[:user_id], params[:klass]) end end def self.(keyword_id, klass, loan_id) created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else Tag.where(keyword_id: keyword_id).where(tag_object_type: ['Container', 'Otu', 'CollectionObject']).distinct.all.each do |o| created.push LoanItem.create!(loan_item_object: o.tag_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end def self.batch_create_from_pinboard(loan_id, project_id, user_id, klass) return false if loan_id.blank? || project_id.blank? || user_id.blank? created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id, pinned_object_type: klass}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: ['Container', 'Otu', 'CollectionObject']).all.each do |o| created.push LoanItem.create!(loan_item_object: o.pinned_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end protected def object_loanable_check loan_item_object && loan_item_object.respond_to?(:is_loanable?) end def total_provided_only_when_otu errors.add(:total, 'only providable when item is an OTU.') if total && loan_item_object_type != 'Otu' end # Code, not out-on-loan check def loan_object_is_loanable if !persisted? # if it is, then this check should not be necessary if !object_loanable_check errors.add(:loan_item_object, 'is not loanble') end end end # Is not already in a loan item if CollectionObject/Container def available_for_loan if !persisted? # if it is, then this check should not be necessary if object_loanable_check if loan_item_object_type == 'Otu' true else if loan_item_object.on_loan? #loan_item_object.loan_items.where.not(id: id).any? errors.add(:loan_item_object, 'is already on loan') end end end end end end |
#project_id ⇒ Integer
the project ID
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/models/loan_item.rb', line 38 class LoanItem < ApplicationRecord acts_as_list scope: [:loan, :project_id] include Housekeeping include Shared::DataAttributes include Shared::Notes include Shared::Tags include Shared::IsData attr_accessor :date_returned_jquery STATUS = ['Destroyed', 'Donated', 'Loaned on', 'Lost', 'Retained', 'Returned'].freeze belongs_to :loan, inverse_of: :loan_items belongs_to :loan_item_object, polymorphic: true validates_presence_of :loan_item_object validates :loan, presence: true # validates_uniqueness_of :loan, scope: [:loan_item_object_type, :loan_item_object_id] validate :total_provided_only_when_otu validate :loan_object_is_loanable validate :available_for_loan validates_inclusion_of :disposition, in: STATUS, if: -> {!disposition.blank?} def global_entity self.loan_item_object.to_global_id if self.loan_item_object.present? end def global_entity=(entity) self.loan_item_object = GlobalID::Locator.locate entity end def date_returned_jquery=(date) self.date_returned = date.gsub(/(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3') end def date_returned_jquery self.date_returned end def returned? !date_returned.blank? end # @return [Integer, nil] # the total items this loan line item represent # TODO: this does not factor in nested items in a container def total_items case loan_item_object_type when 'Otu' total ? total : nil when 'Container' loan_item_object.container_items.try(:count) when 'CollectionObject' loan_item_object.total.to_i else nil end end # @return [Array] # all objects that can have a taxon determination applied to them for htis loan item def determinable_objects # this loan item which may be a container, an OTU, or a collection object case loan_item_object_type when /contain/i # if this item is a container, dig into the container for the collection objects themselves loan_item_object.collection_objects when /object/i # if this item is a collection object, just add the object [loan_item_object] when /otu/i # not strictly needed, but helps keep track of what the loan_item is. [] # can't use an OTU as a determination object. end end # @params :ids -> an ID of a loan_item def self.batch_determine_loan_items(ids: [], params: {}) return false if ids.empty? # these objects will be created/persisted to be used for each of the loan items identified by the input ids td = TaxonDetermination.new(params) # build a td from the input data begin LoanItem.transaction do item_list = [] # Array of objects that can have a taxon determination LoanItem.where(id: ids).each do |li| item_list.push li.determinable_objects end item_list.flatten! first = item_list.pop td.biological_collection_object = first td.save! # create and save the first one so we can dup it in the next step item_list.each do |item| n = td.dup n.determiners << td.determiners n.biological_collection_object = item n.save n.move_to_top end end rescue ActiveRecord::RecordInvalid return false end true end # TODO: param handling is currently all kinds of "meh" def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:loan_id]) when 'pinboard' batch_create_from_pinboard(params[:loan_id], params[:project_id], params[:user_id], params[:klass]) end end def self.(keyword_id, klass, loan_id) created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else Tag.where(keyword_id: keyword_id).where(tag_object_type: ['Container', 'Otu', 'CollectionObject']).distinct.all.each do |o| created.push LoanItem.create!(loan_item_object: o.tag_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end def self.batch_create_from_pinboard(loan_id, project_id, user_id, klass) return false if loan_id.blank? || project_id.blank? || user_id.blank? created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id, pinned_object_type: klass}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: ['Container', 'Otu', 'CollectionObject']).all.each do |o| created.push LoanItem.create!(loan_item_object: o.pinned_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end protected def object_loanable_check loan_item_object && loan_item_object.respond_to?(:is_loanable?) end def total_provided_only_when_otu errors.add(:total, 'only providable when item is an OTU.') if total && loan_item_object_type != 'Otu' end # Code, not out-on-loan check def loan_object_is_loanable if !persisted? # if it is, then this check should not be necessary if !object_loanable_check errors.add(:loan_item_object, 'is not loanble') end end end # Is not already in a loan item if CollectionObject/Container def available_for_loan if !persisted? # if it is, then this check should not be necessary if object_loanable_check if loan_item_object_type == 'Otu' true else if loan_item_object.on_loan? #loan_item_object.loan_items.where.not(id: id).any? errors.add(:loan_item_object, 'is already on loan') end end end end end end |
#total ⇒ Integer
Returns when type is OTU an arbitrary total can be provided.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/models/loan_item.rb', line 38 class LoanItem < ApplicationRecord acts_as_list scope: [:loan, :project_id] include Housekeeping include Shared::DataAttributes include Shared::Notes include Shared::Tags include Shared::IsData attr_accessor :date_returned_jquery STATUS = ['Destroyed', 'Donated', 'Loaned on', 'Lost', 'Retained', 'Returned'].freeze belongs_to :loan, inverse_of: :loan_items belongs_to :loan_item_object, polymorphic: true validates_presence_of :loan_item_object validates :loan, presence: true # validates_uniqueness_of :loan, scope: [:loan_item_object_type, :loan_item_object_id] validate :total_provided_only_when_otu validate :loan_object_is_loanable validate :available_for_loan validates_inclusion_of :disposition, in: STATUS, if: -> {!disposition.blank?} def global_entity self.loan_item_object.to_global_id if self.loan_item_object.present? end def global_entity=(entity) self.loan_item_object = GlobalID::Locator.locate entity end def date_returned_jquery=(date) self.date_returned = date.gsub(/(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3') end def date_returned_jquery self.date_returned end def returned? !date_returned.blank? end # @return [Integer, nil] # the total items this loan line item represent # TODO: this does not factor in nested items in a container def total_items case loan_item_object_type when 'Otu' total ? total : nil when 'Container' loan_item_object.container_items.try(:count) when 'CollectionObject' loan_item_object.total.to_i else nil end end # @return [Array] # all objects that can have a taxon determination applied to them for htis loan item def determinable_objects # this loan item which may be a container, an OTU, or a collection object case loan_item_object_type when /contain/i # if this item is a container, dig into the container for the collection objects themselves loan_item_object.collection_objects when /object/i # if this item is a collection object, just add the object [loan_item_object] when /otu/i # not strictly needed, but helps keep track of what the loan_item is. [] # can't use an OTU as a determination object. end end # @params :ids -> an ID of a loan_item def self.batch_determine_loan_items(ids: [], params: {}) return false if ids.empty? # these objects will be created/persisted to be used for each of the loan items identified by the input ids td = TaxonDetermination.new(params) # build a td from the input data begin LoanItem.transaction do item_list = [] # Array of objects that can have a taxon determination LoanItem.where(id: ids).each do |li| item_list.push li.determinable_objects end item_list.flatten! first = item_list.pop td.biological_collection_object = first td.save! # create and save the first one so we can dup it in the next step item_list.each do |item| n = td.dup n.determiners << td.determiners n.biological_collection_object = item n.save n.move_to_top end end rescue ActiveRecord::RecordInvalid return false end true end # TODO: param handling is currently all kinds of "meh" def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:loan_id]) when 'pinboard' batch_create_from_pinboard(params[:loan_id], params[:project_id], params[:user_id], params[:klass]) end end def self.(keyword_id, klass, loan_id) created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else Tag.where(keyword_id: keyword_id).where(tag_object_type: ['Container', 'Otu', 'CollectionObject']).distinct.all.each do |o| created.push LoanItem.create!(loan_item_object: o.tag_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end def self.batch_create_from_pinboard(loan_id, project_id, user_id, klass) return false if loan_id.blank? || project_id.blank? || user_id.blank? created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id, pinned_object_type: klass}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: ['Container', 'Otu', 'CollectionObject']).all.each do |o| created.push LoanItem.create!(loan_item_object: o.pinned_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end protected def object_loanable_check loan_item_object && loan_item_object.respond_to?(:is_loanable?) end def total_provided_only_when_otu errors.add(:total, 'only providable when item is an OTU.') if total && loan_item_object_type != 'Otu' end # Code, not out-on-loan check def loan_object_is_loanable if !persisted? # if it is, then this check should not be necessary if !object_loanable_check errors.add(:loan_item_object, 'is not loanble') end end end # Is not already in a loan item if CollectionObject/Container def available_for_loan if !persisted? # if it is, then this check should not be necessary if object_loanable_check if loan_item_object_type == 'Otu' true else if loan_item_object.on_loan? #loan_item_object.loan_items.where.not(id: id).any? errors.add(:loan_item_object, 'is already on loan') end end end end end end |
Class Method Details
.batch_create(params) ⇒ Object
TODO: param handling is currently all kinds of “meh”
152 153 154 155 156 157 158 159 |
# File 'app/models/loan_item.rb', line 152 def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:loan_id]) when 'pinboard' batch_create_from_pinboard(params[:loan_id], params[:project_id], params[:user_id], params[:klass]) end end |
.batch_create_from_pinboard(loan_id, project_id, user_id, klass) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'app/models/loan_item.rb', line 182 def self.batch_create_from_pinboard(loan_id, project_id, user_id, klass) return false if loan_id.blank? || project_id.blank? || user_id.blank? created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id, pinned_object_type: klass}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: ['Container', 'Otu', 'CollectionObject']).all.each do |o| created.push LoanItem.create!(loan_item_object: o.pinned_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end |
.batch_create_from_tags(keyword_id, klass, loan_id) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/models/loan_item.rb', line 161 def self.(keyword_id, klass, loan_id) created = [] LoanItem.transaction do begin if klass klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id}).each do |o| created.push LoanItem.create!(loan_item_object: o, loan_id: loan_id) end else Tag.where(keyword_id: keyword_id).where(tag_object_type: ['Container', 'Otu', 'CollectionObject']).distinct.all.each do |o| created.push LoanItem.create!(loan_item_object: o.tag_object, loan_id: loan_id) end end rescue ActiveRecord::RecordInvalid return false end end return created end |
.batch_determine_loan_items(ids: [], params: {}) ⇒ Object
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 |
# File 'app/models/loan_item.rb', line 119 def self.batch_determine_loan_items(ids: [], params: {}) return false if ids.empty? # these objects will be created/persisted to be used for each of the loan items identified by the input ids td = TaxonDetermination.new(params) # build a td from the input data begin LoanItem.transaction do item_list = [] # Array of objects that can have a taxon determination LoanItem.where(id: ids).each do |li| item_list.push li.determinable_objects end item_list.flatten! first = item_list.pop td.biological_collection_object = first td.save! # create and save the first one so we can dup it in the next step item_list.each do |item| n = td.dup n.determiners << td.determiners n.biological_collection_object = item n.save n.move_to_top end end rescue ActiveRecord::RecordInvalid return false end true end |
Instance Method Details
#available_for_loan ⇒ Object (protected)
Is not already in a loan item if CollectionObject/Container
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'app/models/loan_item.rb', line 223 def available_for_loan if !persisted? # if it is, then this check should not be necessary if object_loanable_check if loan_item_object_type == 'Otu' true else if loan_item_object.on_loan? #loan_item_object.loan_items.where.not(id: id).any? errors.add(:loan_item_object, 'is already on loan') end end end end end |
#determinable_objects ⇒ Array
Returns all objects that can have a taxon determination applied to them for htis loan item.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/models/loan_item.rb', line 106 def determinable_objects # this loan item which may be a container, an OTU, or a collection object case loan_item_object_type when /contain/i # if this item is a container, dig into the container for the collection objects themselves loan_item_object.collection_objects when /object/i # if this item is a collection object, just add the object [loan_item_object] when /otu/i # not strictly needed, but helps keep track of what the loan_item is. [] # can't use an OTU as a determination object. end end |
#global_entity ⇒ Object
68 69 70 |
# File 'app/models/loan_item.rb', line 68 def global_entity self.loan_item_object.to_global_id if self.loan_item_object.present? end |
#global_entity=(entity) ⇒ Object
72 73 74 |
# File 'app/models/loan_item.rb', line 72 def global_entity=(entity) self.loan_item_object = GlobalID::Locator.locate entity end |
#loan_object_is_loanable ⇒ Object (protected)
Code, not out-on-loan check
214 215 216 217 218 219 220 |
# File 'app/models/loan_item.rb', line 214 def loan_object_is_loanable if !persisted? # if it is, then this check should not be necessary if !object_loanable_check errors.add(:loan_item_object, 'is not loanble') end end end |
#object_loanable_check ⇒ Object (protected)
205 206 207 |
# File 'app/models/loan_item.rb', line 205 def object_loanable_check loan_item_object && loan_item_object.respond_to?(:is_loanable?) end |
#returned? ⇒ Boolean
84 85 86 |
# File 'app/models/loan_item.rb', line 84 def returned? !date_returned.blank? end |
#total_items ⇒ Integer?
TODO: this does not factor in nested items in a container
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/loan_item.rb', line 91 def total_items case loan_item_object_type when 'Otu' total ? total : nil when 'Container' loan_item_object.container_items.try(:count) when 'CollectionObject' loan_item_object.total.to_i else nil end end |
#total_provided_only_when_otu ⇒ Object (protected)
209 210 211 |
# File 'app/models/loan_item.rb', line 209 def total_provided_only_when_otu errors.add(:total, 'only providable when item is an OTU.') if total && loan_item_object_type != 'Otu' end |