Class: ObservationMatrixRowItem
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ObservationMatrixRowItem
- Includes:
- Housekeeping, Shared::Citations, Shared::Identifiers, Shared::IsData, Shared::Notes, Shared::ObservationIndex, Shared::Tags, SoftValidation
- Defined in:
- app/models/observation_matrix_row_item.rb
Overview
Each ObservationMatrixRowItem determines a set of ObservationMatrixRows (1 if type is Single, 0 or more if type is Dynamic). Thus ObservationMatrixRowItems generate ObservationMatrixRows (not the other way around).
Defined Under Namespace
Constant Summary
Constants included from SoftValidation
SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS
Instance Attribute Summary collapse
-
#controlled_vocabulary_term_id ⇒ Object
Tag id if this row item is a ObservationMatrixRowItem::Dynamic::Tag.
-
#observation_matrix_id ⇒ Integer
Id of the matrix.
-
#observation_object_id ⇒ Object
Id of the object being observed.
-
#observation_object_type ⇒ Object
Type of the object being observed.
-
#position ⇒ Integer
A sort order; determines the order in which the ObservationMatrixRows of ObservationMatrixRowItems are listed in the matrix.
-
#taxon_name_id ⇒ Object
Taxon name id if this row item is a ObservationMatrixRowItem::Dynamic::TaxonName.
-
#type ⇒ Object
Rails STI type.
Class Method Summary collapse
-
.batch_create(params) ⇒ Array
Of ObservationMatrixRowItems.
- .batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) ⇒ Array
- .batch_create_from_tags(keyword_id, klass, observation_matrix_id) ⇒ Array
-
.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) ⇒ Array
Create observation matrix row items for all scope items.
- .create_for_tags(tag_scope, observation_matrix_id) ⇒ Array
- .human_name ⇒ Object
-
.subclass_attributes ⇒ Array
override.
Instance Method Summary collapse
- #cleanup_matrix_rows ⇒ Object
-
#cleanup_single_matrix_row(object) ⇒ Object
Not named "destroy_" because it doesn't always delete row.
-
#cleanup_single_matrix_row_for(observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id) ⇒ Object
As cleanup_single_matrix_row, for an object that needn't be loaded.
- #decrement_matrix_row_reference_count(mr) ⇒ Object private
- #find_or_build_row(object) ⇒ Object
-
#increment_matrix_row_reference_count(mr) ⇒ Object
private
TODO: Should change behaviour of cached_ to only populate with id when reference count == 1 that way we could delete rows.
-
#matrix_row_item_object ⇒ Object
override.
-
#move_matrix_row ⇒ Object
The item now covers another object (e.g. unify moved it to the kept object) and/or is in another matrix: decrement the previous observation_object's row (in the previous matrix), increment the new one.
- #object_is?(object_type) ⇒ matrix_row_item_object?
-
#observation_objects ⇒ Array
required/defined in subclasses.
- #update_matrix_rows ⇒ Object
- #update_single_matrix_row(object) ⇒ Object
Methods included from SoftValidation
#clear_soft_validations, #fix_for, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations, #soft_validators
Methods included from Shared::IsData
#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_in_use?, #similar
Methods included from Shared::Notes
#concatenated_notes_string, #reject_notes
Methods included from Shared::Tags
#reject_tags, #tag_with, #tagged?, #tagged_with?
Methods included from Shared::Identifiers
#dwc_occurrence_id, #identified?, #next_by_identifier, #previous_by_identifier, #reject_identifiers, #uri, #uuid, #visible_identifiers_for
Methods included from Shared::Citations
#cited?, #mark_citations_for_destruction, #nomenclature_date, #origin_citation_source_id, #reject_citations, #requires_citation?, #sources_by_topic_id
Methods included from Housekeeping
#has_polymorphic_relationship?
Methods inherited from ApplicationRecord
Instance Attribute Details
#controlled_vocabulary_term_id ⇒ Object
Returns tag id if this row item is a ObservationMatrixRowItem::Dynamic::Tag.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/models/observation_matrix_row_item.rb', line 24 class ObservationMatrixRowItem < ApplicationRecord include Housekeeping include Shared::Citations include Shared::Identifiers include Shared::Tags include Shared::Notes include Shared::IsData include SoftValidation include Shared::ObservationIndex acts_as_list scope: [:observation_matrix_id, :project_id] belongs_to :observation_matrix, inverse_of: :observation_matrix_row_items belongs_to :observation_object, polymorphic: true, inverse_of: :observation_matrix_row_items validates_presence_of :observation_matrix, :observation_object after_create :update_matrix_rows after_update :move_matrix_row, if: -> { saved_change_to_observation_object_id? || saved_change_to_observation_object_type? || saved_change_to_observation_matrix_id? } after_destroy :cleanup_matrix_rows # @return [Array] # of all objects this row references # required/defined in subclasses def observation_objects [] end def update_matrix_rows observation_objects.each do |o| update_single_matrix_row o end end # The item now covers another object (e.g. unify moved it to the kept # object) and/or is in another matrix: decrement the previous # observation_object's row (in the previous matrix), increment the new one def move_matrix_row if observation_object_id_before_last_save # One object replaced by another: repoint a row that would otherwise be # deleted, so it keeps its position if observation_matrix_id_before_last_save == observation_matrix_id mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type: observation_object_type_before_last_save, observation_object_id: observation_object_id_before_last_save ).first if mr&.reference_count == 1 && !ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).exists? mr.update_columns(observation_object_type:, observation_object_id:) return end end cleanup_single_matrix_row_for( observation_object_type_before_last_save, observation_object_id_before_last_save, observation_matrix_id_before_last_save ) end update_matrix_rows end def cleanup_matrix_rows return true if observation_objects.count == 0 ObservationMatrixRow.where(observation_matrix:, observation_object: observation_objects).each do |mr| decrement_matrix_row_reference_count(mr) end true end def find_or_build_row(object) ObservationMatrixRow.find_or_initialize_by(observation_matrix:, observation_object: object) end def update_single_matrix_row(object) mr = find_or_build_row(object) mr.save! if !mr.persisted? increment_matrix_row_reference_count(mr) end # Not named "destroy_" because it doesn't always delete row def cleanup_single_matrix_row(object) cleanup_single_matrix_row_for(object.class.base_class.name, object.id) end # As cleanup_single_matrix_row, for an object that needn't be loaded def cleanup_single_matrix_row_for( observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id ) mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).first decrement_matrix_row_reference_count(mr) if !mr.nil? end def self.human_name self.name.demodulize.humanize end # @return [Array] # the required attributes for this subclass # override def self.subclass_attributes [] end # @return [Object] # the object used to define the set of matrix rows # override def matrix_row_item_object nil end # @return [matrix_row_item_object, nil] def object_is?(object_type) matrix_row_item_object.class.name == object_type ? matrix_row_item_object : nil end # @return [Array] of ObservationMatrixRowItems # @raise [TaxonWorks::Error] def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:observation_matrix_id]) when 'pinboard' batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass]) end end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.(keyword_id, klass, observation_matrix_id) created = [] ObservationMatrixRowItem.transaction do begin if klass klass.safe_constantize.joins(:tags).where(tags: {keyword_id:} ).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += ( Tag.where(keyword_id:, tag_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) if observation_matrix_id.blank? || project_id.blank? || user_id.blank? raise TaxonWorks::Error, 'Internal error, missing required batch create data' end created = [] ObservationMatrixRow.transaction do begin if klass klass.safe_constantize.joins(:pinboard_items).where(pinboard_items: {user_id:, project_id:}).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += create_for_pinboard_items( PinboardItem.where(project_id:, user_id:, pinned_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end private # @return [Array] def self.(tag_scope, observation_matrix_id) a = [] tag_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.tag_object) end a end # @param pinboard_item_scope [PinboardItem Scope] # @return [Array] # create observation matrix row items for all scope items def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) a = [] pinboard_item_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.pinned_object) end a end def decrement_matrix_row_reference_count(mr) current = mr.reference_count - 1 if current == 0 mr.delete else mr.update_columns(reference_count: current) mr.update_columns(cached_observation_matrix_row_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged end end # TODO: Should change behaviour of cached_ # to only populate with id when reference count == 1 # that way we could delete rows def increment_matrix_row_reference_count(mr) mr.update_columns(reference_count: (mr.reference_count || 0) + 1) mr.update_columns(cached_observation_matrix_row_item_id: id) if type =~ /Single/ end end |
#observation_matrix_id ⇒ Integer
Returns id of the matrix.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/models/observation_matrix_row_item.rb', line 24 class ObservationMatrixRowItem < ApplicationRecord include Housekeeping include Shared::Citations include Shared::Identifiers include Shared::Tags include Shared::Notes include Shared::IsData include SoftValidation include Shared::ObservationIndex acts_as_list scope: [:observation_matrix_id, :project_id] belongs_to :observation_matrix, inverse_of: :observation_matrix_row_items belongs_to :observation_object, polymorphic: true, inverse_of: :observation_matrix_row_items validates_presence_of :observation_matrix, :observation_object after_create :update_matrix_rows after_update :move_matrix_row, if: -> { saved_change_to_observation_object_id? || saved_change_to_observation_object_type? || saved_change_to_observation_matrix_id? } after_destroy :cleanup_matrix_rows # @return [Array] # of all objects this row references # required/defined in subclasses def observation_objects [] end def update_matrix_rows observation_objects.each do |o| update_single_matrix_row o end end # The item now covers another object (e.g. unify moved it to the kept # object) and/or is in another matrix: decrement the previous # observation_object's row (in the previous matrix), increment the new one def move_matrix_row if observation_object_id_before_last_save # One object replaced by another: repoint a row that would otherwise be # deleted, so it keeps its position if observation_matrix_id_before_last_save == observation_matrix_id mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type: observation_object_type_before_last_save, observation_object_id: observation_object_id_before_last_save ).first if mr&.reference_count == 1 && !ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).exists? mr.update_columns(observation_object_type:, observation_object_id:) return end end cleanup_single_matrix_row_for( observation_object_type_before_last_save, observation_object_id_before_last_save, observation_matrix_id_before_last_save ) end update_matrix_rows end def cleanup_matrix_rows return true if observation_objects.count == 0 ObservationMatrixRow.where(observation_matrix:, observation_object: observation_objects).each do |mr| decrement_matrix_row_reference_count(mr) end true end def find_or_build_row(object) ObservationMatrixRow.find_or_initialize_by(observation_matrix:, observation_object: object) end def update_single_matrix_row(object) mr = find_or_build_row(object) mr.save! if !mr.persisted? increment_matrix_row_reference_count(mr) end # Not named "destroy_" because it doesn't always delete row def cleanup_single_matrix_row(object) cleanup_single_matrix_row_for(object.class.base_class.name, object.id) end # As cleanup_single_matrix_row, for an object that needn't be loaded def cleanup_single_matrix_row_for( observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id ) mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).first decrement_matrix_row_reference_count(mr) if !mr.nil? end def self.human_name self.name.demodulize.humanize end # @return [Array] # the required attributes for this subclass # override def self.subclass_attributes [] end # @return [Object] # the object used to define the set of matrix rows # override def matrix_row_item_object nil end # @return [matrix_row_item_object, nil] def object_is?(object_type) matrix_row_item_object.class.name == object_type ? matrix_row_item_object : nil end # @return [Array] of ObservationMatrixRowItems # @raise [TaxonWorks::Error] def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:observation_matrix_id]) when 'pinboard' batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass]) end end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.(keyword_id, klass, observation_matrix_id) created = [] ObservationMatrixRowItem.transaction do begin if klass klass.safe_constantize.joins(:tags).where(tags: {keyword_id:} ).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += ( Tag.where(keyword_id:, tag_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) if observation_matrix_id.blank? || project_id.blank? || user_id.blank? raise TaxonWorks::Error, 'Internal error, missing required batch create data' end created = [] ObservationMatrixRow.transaction do begin if klass klass.safe_constantize.joins(:pinboard_items).where(pinboard_items: {user_id:, project_id:}).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += create_for_pinboard_items( PinboardItem.where(project_id:, user_id:, pinned_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end private # @return [Array] def self.(tag_scope, observation_matrix_id) a = [] tag_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.tag_object) end a end # @param pinboard_item_scope [PinboardItem Scope] # @return [Array] # create observation matrix row items for all scope items def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) a = [] pinboard_item_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.pinned_object) end a end def decrement_matrix_row_reference_count(mr) current = mr.reference_count - 1 if current == 0 mr.delete else mr.update_columns(reference_count: current) mr.update_columns(cached_observation_matrix_row_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged end end # TODO: Should change behaviour of cached_ # to only populate with id when reference count == 1 # that way we could delete rows def increment_matrix_row_reference_count(mr) mr.update_columns(reference_count: (mr.reference_count || 0) + 1) mr.update_columns(cached_observation_matrix_row_item_id: id) if type =~ /Single/ end end |
#observation_object_id ⇒ Object
Returns id of the object being observed.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/models/observation_matrix_row_item.rb', line 24 class ObservationMatrixRowItem < ApplicationRecord include Housekeeping include Shared::Citations include Shared::Identifiers include Shared::Tags include Shared::Notes include Shared::IsData include SoftValidation include Shared::ObservationIndex acts_as_list scope: [:observation_matrix_id, :project_id] belongs_to :observation_matrix, inverse_of: :observation_matrix_row_items belongs_to :observation_object, polymorphic: true, inverse_of: :observation_matrix_row_items validates_presence_of :observation_matrix, :observation_object after_create :update_matrix_rows after_update :move_matrix_row, if: -> { saved_change_to_observation_object_id? || saved_change_to_observation_object_type? || saved_change_to_observation_matrix_id? } after_destroy :cleanup_matrix_rows # @return [Array] # of all objects this row references # required/defined in subclasses def observation_objects [] end def update_matrix_rows observation_objects.each do |o| update_single_matrix_row o end end # The item now covers another object (e.g. unify moved it to the kept # object) and/or is in another matrix: decrement the previous # observation_object's row (in the previous matrix), increment the new one def move_matrix_row if observation_object_id_before_last_save # One object replaced by another: repoint a row that would otherwise be # deleted, so it keeps its position if observation_matrix_id_before_last_save == observation_matrix_id mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type: observation_object_type_before_last_save, observation_object_id: observation_object_id_before_last_save ).first if mr&.reference_count == 1 && !ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).exists? mr.update_columns(observation_object_type:, observation_object_id:) return end end cleanup_single_matrix_row_for( observation_object_type_before_last_save, observation_object_id_before_last_save, observation_matrix_id_before_last_save ) end update_matrix_rows end def cleanup_matrix_rows return true if observation_objects.count == 0 ObservationMatrixRow.where(observation_matrix:, observation_object: observation_objects).each do |mr| decrement_matrix_row_reference_count(mr) end true end def find_or_build_row(object) ObservationMatrixRow.find_or_initialize_by(observation_matrix:, observation_object: object) end def update_single_matrix_row(object) mr = find_or_build_row(object) mr.save! if !mr.persisted? increment_matrix_row_reference_count(mr) end # Not named "destroy_" because it doesn't always delete row def cleanup_single_matrix_row(object) cleanup_single_matrix_row_for(object.class.base_class.name, object.id) end # As cleanup_single_matrix_row, for an object that needn't be loaded def cleanup_single_matrix_row_for( observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id ) mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).first decrement_matrix_row_reference_count(mr) if !mr.nil? end def self.human_name self.name.demodulize.humanize end # @return [Array] # the required attributes for this subclass # override def self.subclass_attributes [] end # @return [Object] # the object used to define the set of matrix rows # override def matrix_row_item_object nil end # @return [matrix_row_item_object, nil] def object_is?(object_type) matrix_row_item_object.class.name == object_type ? matrix_row_item_object : nil end # @return [Array] of ObservationMatrixRowItems # @raise [TaxonWorks::Error] def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:observation_matrix_id]) when 'pinboard' batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass]) end end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.(keyword_id, klass, observation_matrix_id) created = [] ObservationMatrixRowItem.transaction do begin if klass klass.safe_constantize.joins(:tags).where(tags: {keyword_id:} ).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += ( Tag.where(keyword_id:, tag_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) if observation_matrix_id.blank? || project_id.blank? || user_id.blank? raise TaxonWorks::Error, 'Internal error, missing required batch create data' end created = [] ObservationMatrixRow.transaction do begin if klass klass.safe_constantize.joins(:pinboard_items).where(pinboard_items: {user_id:, project_id:}).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += create_for_pinboard_items( PinboardItem.where(project_id:, user_id:, pinned_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end private # @return [Array] def self.(tag_scope, observation_matrix_id) a = [] tag_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.tag_object) end a end # @param pinboard_item_scope [PinboardItem Scope] # @return [Array] # create observation matrix row items for all scope items def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) a = [] pinboard_item_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.pinned_object) end a end def decrement_matrix_row_reference_count(mr) current = mr.reference_count - 1 if current == 0 mr.delete else mr.update_columns(reference_count: current) mr.update_columns(cached_observation_matrix_row_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged end end # TODO: Should change behaviour of cached_ # to only populate with id when reference count == 1 # that way we could delete rows def increment_matrix_row_reference_count(mr) mr.update_columns(reference_count: (mr.reference_count || 0) + 1) mr.update_columns(cached_observation_matrix_row_item_id: id) if type =~ /Single/ end end |
#observation_object_type ⇒ Object
Returns type of the object being observed.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/models/observation_matrix_row_item.rb', line 24 class ObservationMatrixRowItem < ApplicationRecord include Housekeeping include Shared::Citations include Shared::Identifiers include Shared::Tags include Shared::Notes include Shared::IsData include SoftValidation include Shared::ObservationIndex acts_as_list scope: [:observation_matrix_id, :project_id] belongs_to :observation_matrix, inverse_of: :observation_matrix_row_items belongs_to :observation_object, polymorphic: true, inverse_of: :observation_matrix_row_items validates_presence_of :observation_matrix, :observation_object after_create :update_matrix_rows after_update :move_matrix_row, if: -> { saved_change_to_observation_object_id? || saved_change_to_observation_object_type? || saved_change_to_observation_matrix_id? } after_destroy :cleanup_matrix_rows # @return [Array] # of all objects this row references # required/defined in subclasses def observation_objects [] end def update_matrix_rows observation_objects.each do |o| update_single_matrix_row o end end # The item now covers another object (e.g. unify moved it to the kept # object) and/or is in another matrix: decrement the previous # observation_object's row (in the previous matrix), increment the new one def move_matrix_row if observation_object_id_before_last_save # One object replaced by another: repoint a row that would otherwise be # deleted, so it keeps its position if observation_matrix_id_before_last_save == observation_matrix_id mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type: observation_object_type_before_last_save, observation_object_id: observation_object_id_before_last_save ).first if mr&.reference_count == 1 && !ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).exists? mr.update_columns(observation_object_type:, observation_object_id:) return end end cleanup_single_matrix_row_for( observation_object_type_before_last_save, observation_object_id_before_last_save, observation_matrix_id_before_last_save ) end update_matrix_rows end def cleanup_matrix_rows return true if observation_objects.count == 0 ObservationMatrixRow.where(observation_matrix:, observation_object: observation_objects).each do |mr| decrement_matrix_row_reference_count(mr) end true end def find_or_build_row(object) ObservationMatrixRow.find_or_initialize_by(observation_matrix:, observation_object: object) end def update_single_matrix_row(object) mr = find_or_build_row(object) mr.save! if !mr.persisted? increment_matrix_row_reference_count(mr) end # Not named "destroy_" because it doesn't always delete row def cleanup_single_matrix_row(object) cleanup_single_matrix_row_for(object.class.base_class.name, object.id) end # As cleanup_single_matrix_row, for an object that needn't be loaded def cleanup_single_matrix_row_for( observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id ) mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).first decrement_matrix_row_reference_count(mr) if !mr.nil? end def self.human_name self.name.demodulize.humanize end # @return [Array] # the required attributes for this subclass # override def self.subclass_attributes [] end # @return [Object] # the object used to define the set of matrix rows # override def matrix_row_item_object nil end # @return [matrix_row_item_object, nil] def object_is?(object_type) matrix_row_item_object.class.name == object_type ? matrix_row_item_object : nil end # @return [Array] of ObservationMatrixRowItems # @raise [TaxonWorks::Error] def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:observation_matrix_id]) when 'pinboard' batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass]) end end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.(keyword_id, klass, observation_matrix_id) created = [] ObservationMatrixRowItem.transaction do begin if klass klass.safe_constantize.joins(:tags).where(tags: {keyword_id:} ).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += ( Tag.where(keyword_id:, tag_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) if observation_matrix_id.blank? || project_id.blank? || user_id.blank? raise TaxonWorks::Error, 'Internal error, missing required batch create data' end created = [] ObservationMatrixRow.transaction do begin if klass klass.safe_constantize.joins(:pinboard_items).where(pinboard_items: {user_id:, project_id:}).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += create_for_pinboard_items( PinboardItem.where(project_id:, user_id:, pinned_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end private # @return [Array] def self.(tag_scope, observation_matrix_id) a = [] tag_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.tag_object) end a end # @param pinboard_item_scope [PinboardItem Scope] # @return [Array] # create observation matrix row items for all scope items def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) a = [] pinboard_item_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.pinned_object) end a end def decrement_matrix_row_reference_count(mr) current = mr.reference_count - 1 if current == 0 mr.delete else mr.update_columns(reference_count: current) mr.update_columns(cached_observation_matrix_row_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged end end # TODO: Should change behaviour of cached_ # to only populate with id when reference count == 1 # that way we could delete rows def increment_matrix_row_reference_count(mr) mr.update_columns(reference_count: (mr.reference_count || 0) + 1) mr.update_columns(cached_observation_matrix_row_item_id: id) if type =~ /Single/ end end |
#position ⇒ Integer
Returns a sort order; determines the order in which the ObservationMatrixRows of ObservationMatrixRowItems are listed in the matrix.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/models/observation_matrix_row_item.rb', line 24 class ObservationMatrixRowItem < ApplicationRecord include Housekeeping include Shared::Citations include Shared::Identifiers include Shared::Tags include Shared::Notes include Shared::IsData include SoftValidation include Shared::ObservationIndex acts_as_list scope: [:observation_matrix_id, :project_id] belongs_to :observation_matrix, inverse_of: :observation_matrix_row_items belongs_to :observation_object, polymorphic: true, inverse_of: :observation_matrix_row_items validates_presence_of :observation_matrix, :observation_object after_create :update_matrix_rows after_update :move_matrix_row, if: -> { saved_change_to_observation_object_id? || saved_change_to_observation_object_type? || saved_change_to_observation_matrix_id? } after_destroy :cleanup_matrix_rows # @return [Array] # of all objects this row references # required/defined in subclasses def observation_objects [] end def update_matrix_rows observation_objects.each do |o| update_single_matrix_row o end end # The item now covers another object (e.g. unify moved it to the kept # object) and/or is in another matrix: decrement the previous # observation_object's row (in the previous matrix), increment the new one def move_matrix_row if observation_object_id_before_last_save # One object replaced by another: repoint a row that would otherwise be # deleted, so it keeps its position if observation_matrix_id_before_last_save == observation_matrix_id mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type: observation_object_type_before_last_save, observation_object_id: observation_object_id_before_last_save ).first if mr&.reference_count == 1 && !ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).exists? mr.update_columns(observation_object_type:, observation_object_id:) return end end cleanup_single_matrix_row_for( observation_object_type_before_last_save, observation_object_id_before_last_save, observation_matrix_id_before_last_save ) end update_matrix_rows end def cleanup_matrix_rows return true if observation_objects.count == 0 ObservationMatrixRow.where(observation_matrix:, observation_object: observation_objects).each do |mr| decrement_matrix_row_reference_count(mr) end true end def find_or_build_row(object) ObservationMatrixRow.find_or_initialize_by(observation_matrix:, observation_object: object) end def update_single_matrix_row(object) mr = find_or_build_row(object) mr.save! if !mr.persisted? increment_matrix_row_reference_count(mr) end # Not named "destroy_" because it doesn't always delete row def cleanup_single_matrix_row(object) cleanup_single_matrix_row_for(object.class.base_class.name, object.id) end # As cleanup_single_matrix_row, for an object that needn't be loaded def cleanup_single_matrix_row_for( observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id ) mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).first decrement_matrix_row_reference_count(mr) if !mr.nil? end def self.human_name self.name.demodulize.humanize end # @return [Array] # the required attributes for this subclass # override def self.subclass_attributes [] end # @return [Object] # the object used to define the set of matrix rows # override def matrix_row_item_object nil end # @return [matrix_row_item_object, nil] def object_is?(object_type) matrix_row_item_object.class.name == object_type ? matrix_row_item_object : nil end # @return [Array] of ObservationMatrixRowItems # @raise [TaxonWorks::Error] def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:observation_matrix_id]) when 'pinboard' batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass]) end end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.(keyword_id, klass, observation_matrix_id) created = [] ObservationMatrixRowItem.transaction do begin if klass klass.safe_constantize.joins(:tags).where(tags: {keyword_id:} ).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += ( Tag.where(keyword_id:, tag_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) if observation_matrix_id.blank? || project_id.blank? || user_id.blank? raise TaxonWorks::Error, 'Internal error, missing required batch create data' end created = [] ObservationMatrixRow.transaction do begin if klass klass.safe_constantize.joins(:pinboard_items).where(pinboard_items: {user_id:, project_id:}).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += create_for_pinboard_items( PinboardItem.where(project_id:, user_id:, pinned_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end private # @return [Array] def self.(tag_scope, observation_matrix_id) a = [] tag_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.tag_object) end a end # @param pinboard_item_scope [PinboardItem Scope] # @return [Array] # create observation matrix row items for all scope items def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) a = [] pinboard_item_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.pinned_object) end a end def decrement_matrix_row_reference_count(mr) current = mr.reference_count - 1 if current == 0 mr.delete else mr.update_columns(reference_count: current) mr.update_columns(cached_observation_matrix_row_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged end end # TODO: Should change behaviour of cached_ # to only populate with id when reference count == 1 # that way we could delete rows def increment_matrix_row_reference_count(mr) mr.update_columns(reference_count: (mr.reference_count || 0) + 1) mr.update_columns(cached_observation_matrix_row_item_id: id) if type =~ /Single/ end end |
#taxon_name_id ⇒ Object
Returns taxon name id if this row item is a ObservationMatrixRowItem::Dynamic::TaxonName.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/models/observation_matrix_row_item.rb', line 24 class ObservationMatrixRowItem < ApplicationRecord include Housekeeping include Shared::Citations include Shared::Identifiers include Shared::Tags include Shared::Notes include Shared::IsData include SoftValidation include Shared::ObservationIndex acts_as_list scope: [:observation_matrix_id, :project_id] belongs_to :observation_matrix, inverse_of: :observation_matrix_row_items belongs_to :observation_object, polymorphic: true, inverse_of: :observation_matrix_row_items validates_presence_of :observation_matrix, :observation_object after_create :update_matrix_rows after_update :move_matrix_row, if: -> { saved_change_to_observation_object_id? || saved_change_to_observation_object_type? || saved_change_to_observation_matrix_id? } after_destroy :cleanup_matrix_rows # @return [Array] # of all objects this row references # required/defined in subclasses def observation_objects [] end def update_matrix_rows observation_objects.each do |o| update_single_matrix_row o end end # The item now covers another object (e.g. unify moved it to the kept # object) and/or is in another matrix: decrement the previous # observation_object's row (in the previous matrix), increment the new one def move_matrix_row if observation_object_id_before_last_save # One object replaced by another: repoint a row that would otherwise be # deleted, so it keeps its position if observation_matrix_id_before_last_save == observation_matrix_id mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type: observation_object_type_before_last_save, observation_object_id: observation_object_id_before_last_save ).first if mr&.reference_count == 1 && !ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).exists? mr.update_columns(observation_object_type:, observation_object_id:) return end end cleanup_single_matrix_row_for( observation_object_type_before_last_save, observation_object_id_before_last_save, observation_matrix_id_before_last_save ) end update_matrix_rows end def cleanup_matrix_rows return true if observation_objects.count == 0 ObservationMatrixRow.where(observation_matrix:, observation_object: observation_objects).each do |mr| decrement_matrix_row_reference_count(mr) end true end def find_or_build_row(object) ObservationMatrixRow.find_or_initialize_by(observation_matrix:, observation_object: object) end def update_single_matrix_row(object) mr = find_or_build_row(object) mr.save! if !mr.persisted? increment_matrix_row_reference_count(mr) end # Not named "destroy_" because it doesn't always delete row def cleanup_single_matrix_row(object) cleanup_single_matrix_row_for(object.class.base_class.name, object.id) end # As cleanup_single_matrix_row, for an object that needn't be loaded def cleanup_single_matrix_row_for( observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id ) mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).first decrement_matrix_row_reference_count(mr) if !mr.nil? end def self.human_name self.name.demodulize.humanize end # @return [Array] # the required attributes for this subclass # override def self.subclass_attributes [] end # @return [Object] # the object used to define the set of matrix rows # override def matrix_row_item_object nil end # @return [matrix_row_item_object, nil] def object_is?(object_type) matrix_row_item_object.class.name == object_type ? matrix_row_item_object : nil end # @return [Array] of ObservationMatrixRowItems # @raise [TaxonWorks::Error] def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:observation_matrix_id]) when 'pinboard' batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass]) end end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.(keyword_id, klass, observation_matrix_id) created = [] ObservationMatrixRowItem.transaction do begin if klass klass.safe_constantize.joins(:tags).where(tags: {keyword_id:} ).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += ( Tag.where(keyword_id:, tag_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) if observation_matrix_id.blank? || project_id.blank? || user_id.blank? raise TaxonWorks::Error, 'Internal error, missing required batch create data' end created = [] ObservationMatrixRow.transaction do begin if klass klass.safe_constantize.joins(:pinboard_items).where(pinboard_items: {user_id:, project_id:}).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += create_for_pinboard_items( PinboardItem.where(project_id:, user_id:, pinned_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end private # @return [Array] def self.(tag_scope, observation_matrix_id) a = [] tag_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.tag_object) end a end # @param pinboard_item_scope [PinboardItem Scope] # @return [Array] # create observation matrix row items for all scope items def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) a = [] pinboard_item_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.pinned_object) end a end def decrement_matrix_row_reference_count(mr) current = mr.reference_count - 1 if current == 0 mr.delete else mr.update_columns(reference_count: current) mr.update_columns(cached_observation_matrix_row_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged end end # TODO: Should change behaviour of cached_ # to only populate with id when reference count == 1 # that way we could delete rows def increment_matrix_row_reference_count(mr) mr.update_columns(reference_count: (mr.reference_count || 0) + 1) mr.update_columns(cached_observation_matrix_row_item_id: id) if type =~ /Single/ end end |
#type ⇒ Object
Returns rails STI type.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/models/observation_matrix_row_item.rb', line 24 class ObservationMatrixRowItem < ApplicationRecord include Housekeeping include Shared::Citations include Shared::Identifiers include Shared::Tags include Shared::Notes include Shared::IsData include SoftValidation include Shared::ObservationIndex acts_as_list scope: [:observation_matrix_id, :project_id] belongs_to :observation_matrix, inverse_of: :observation_matrix_row_items belongs_to :observation_object, polymorphic: true, inverse_of: :observation_matrix_row_items validates_presence_of :observation_matrix, :observation_object after_create :update_matrix_rows after_update :move_matrix_row, if: -> { saved_change_to_observation_object_id? || saved_change_to_observation_object_type? || saved_change_to_observation_matrix_id? } after_destroy :cleanup_matrix_rows # @return [Array] # of all objects this row references # required/defined in subclasses def observation_objects [] end def update_matrix_rows observation_objects.each do |o| update_single_matrix_row o end end # The item now covers another object (e.g. unify moved it to the kept # object) and/or is in another matrix: decrement the previous # observation_object's row (in the previous matrix), increment the new one def move_matrix_row if observation_object_id_before_last_save # One object replaced by another: repoint a row that would otherwise be # deleted, so it keeps its position if observation_matrix_id_before_last_save == observation_matrix_id mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type: observation_object_type_before_last_save, observation_object_id: observation_object_id_before_last_save ).first if mr&.reference_count == 1 && !ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).exists? mr.update_columns(observation_object_type:, observation_object_id:) return end end cleanup_single_matrix_row_for( observation_object_type_before_last_save, observation_object_id_before_last_save, observation_matrix_id_before_last_save ) end update_matrix_rows end def cleanup_matrix_rows return true if observation_objects.count == 0 ObservationMatrixRow.where(observation_matrix:, observation_object: observation_objects).each do |mr| decrement_matrix_row_reference_count(mr) end true end def find_or_build_row(object) ObservationMatrixRow.find_or_initialize_by(observation_matrix:, observation_object: object) end def update_single_matrix_row(object) mr = find_or_build_row(object) mr.save! if !mr.persisted? increment_matrix_row_reference_count(mr) end # Not named "destroy_" because it doesn't always delete row def cleanup_single_matrix_row(object) cleanup_single_matrix_row_for(object.class.base_class.name, object.id) end # As cleanup_single_matrix_row, for an object that needn't be loaded def cleanup_single_matrix_row_for( observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id ) mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).first decrement_matrix_row_reference_count(mr) if !mr.nil? end def self.human_name self.name.demodulize.humanize end # @return [Array] # the required attributes for this subclass # override def self.subclass_attributes [] end # @return [Object] # the object used to define the set of matrix rows # override def matrix_row_item_object nil end # @return [matrix_row_item_object, nil] def object_is?(object_type) matrix_row_item_object.class.name == object_type ? matrix_row_item_object : nil end # @return [Array] of ObservationMatrixRowItems # @raise [TaxonWorks::Error] def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:observation_matrix_id]) when 'pinboard' batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass]) end end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.(keyword_id, klass, observation_matrix_id) created = [] ObservationMatrixRowItem.transaction do begin if klass klass.safe_constantize.joins(:tags).where(tags: {keyword_id:} ).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += ( Tag.where(keyword_id:, tag_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end # @param klass [String] the class name like `Otu` or `CollectionObject` # @return [Array] # @raise [TaxonWorks::Error] def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) if observation_matrix_id.blank? || project_id.blank? || user_id.blank? raise TaxonWorks::Error, 'Internal error, missing required batch create data' end created = [] ObservationMatrixRow.transaction do begin if klass klass.safe_constantize.joins(:pinboard_items).where(pinboard_items: {user_id:, project_id:}).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += create_for_pinboard_items( PinboardItem.where(project_id:, user_id:, pinned_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end private # @return [Array] def self.(tag_scope, observation_matrix_id) a = [] tag_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.tag_object) end a end # @param pinboard_item_scope [PinboardItem Scope] # @return [Array] # create observation matrix row items for all scope items def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) a = [] pinboard_item_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.pinned_object) end a end def decrement_matrix_row_reference_count(mr) current = mr.reference_count - 1 if current == 0 mr.delete else mr.update_columns(reference_count: current) mr.update_columns(cached_observation_matrix_row_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged end end # TODO: Should change behaviour of cached_ # to only populate with id when reference count == 1 # that way we could delete rows def increment_matrix_row_reference_count(mr) mr.update_columns(reference_count: (mr.reference_count || 0) + 1) mr.update_columns(cached_observation_matrix_row_item_id: id) if type =~ /Single/ end end |
Class Method Details
.batch_create(params) ⇒ Array
Returns of ObservationMatrixRowItems.
155 156 157 158 159 160 161 162 |
# File 'app/models/observation_matrix_row_item.rb', line 155 def self.batch_create(params) case params[:batch_type] when 'tags' (params[:keyword_id], params[:klass], params[:observation_matrix_id]) when 'pinboard' batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass]) end end |
.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) ⇒ Array
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'app/models/observation_matrix_row_item.rb', line 191 def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass) if observation_matrix_id.blank? || project_id.blank? || user_id.blank? raise TaxonWorks::Error, 'Internal error, missing required batch create data' end created = [] ObservationMatrixRow.transaction do begin if klass klass.safe_constantize.joins(:pinboard_items).where(pinboard_items: {user_id:, project_id:}).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += create_for_pinboard_items( PinboardItem.where(project_id:, user_id:, pinned_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end |
.batch_create_from_tags(keyword_id, klass, observation_matrix_id) ⇒ Array
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/models/observation_matrix_row_item.rb', line 167 def self.(keyword_id, klass, observation_matrix_id) created = [] ObservationMatrixRowItem.transaction do begin if klass klass.safe_constantize.joins(:tags).where(tags: {keyword_id:} ).each do |o| created.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o) end else created += ( Tag.where(keyword_id:, tag_object_type: OBSERVABLE_TYPES).all, observation_matrix_id ) end rescue ActiveRecord::RecordInvalid => e raise TaxonWorks::Error, e.to_s, e.backtrace end end return created end |
.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) ⇒ Array
Returns create observation matrix row items for all scope items.
230 231 232 233 234 235 236 |
# File 'app/models/observation_matrix_row_item.rb', line 230 def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) a = [] pinboard_item_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.pinned_object) end a end |
.create_for_tags(tag_scope, observation_matrix_id) ⇒ Array
219 220 221 222 223 224 225 |
# File 'app/models/observation_matrix_row_item.rb', line 219 def self.(tag_scope, observation_matrix_id) a = [] tag_scope.each do |o| a.push ObservationMatrixRowItem::Single.create!(observation_matrix_id:, observation_object: o.tag_object) end a end |
.human_name ⇒ Object
129 130 131 |
# File 'app/models/observation_matrix_row_item.rb', line 129 def self.human_name self.name.demodulize.humanize end |
.subclass_attributes ⇒ Array
override
136 137 138 |
# File 'app/models/observation_matrix_row_item.rb', line 136 def self.subclass_attributes [] end |
Instance Method Details
#cleanup_matrix_rows ⇒ Object
95 96 97 98 99 100 101 |
# File 'app/models/observation_matrix_row_item.rb', line 95 def cleanup_matrix_rows return true if observation_objects.count == 0 ObservationMatrixRow.where(observation_matrix:, observation_object: observation_objects).each do |mr| decrement_matrix_row_reference_count(mr) end true end |
#cleanup_single_matrix_row(object) ⇒ Object
Not named "destroy_" because it doesn't always delete row
114 115 116 |
# File 'app/models/observation_matrix_row_item.rb', line 114 def cleanup_single_matrix_row(object) cleanup_single_matrix_row_for(object.class.base_class.name, object.id) end |
#cleanup_single_matrix_row_for(observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id) ⇒ Object
As cleanup_single_matrix_row, for an object that needn't be loaded
119 120 121 122 123 124 125 126 127 |
# File 'app/models/observation_matrix_row_item.rb', line 119 def cleanup_single_matrix_row_for( observation_object_type, observation_object_id, observation_matrix_id = self.observation_matrix_id ) mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).first decrement_matrix_row_reference_count(mr) if !mr.nil? end |
#decrement_matrix_row_reference_count(mr) ⇒ Object (private)
238 239 240 241 242 243 244 245 246 247 |
# File 'app/models/observation_matrix_row_item.rb', line 238 def decrement_matrix_row_reference_count(mr) current = mr.reference_count - 1 if current == 0 mr.delete else mr.update_columns(reference_count: current) mr.update_columns(cached_observation_matrix_row_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged end end |
#find_or_build_row(object) ⇒ Object
103 104 105 |
# File 'app/models/observation_matrix_row_item.rb', line 103 def find_or_build_row(object) ObservationMatrixRow.find_or_initialize_by(observation_matrix:, observation_object: object) end |
#increment_matrix_row_reference_count(mr) ⇒ Object (private)
TODO: Should change behaviour of cached_ to only populate with id when reference count == 1 that way we could delete rows
252 253 254 255 |
# File 'app/models/observation_matrix_row_item.rb', line 252 def increment_matrix_row_reference_count(mr) mr.update_columns(reference_count: (mr.reference_count || 0) + 1) mr.update_columns(cached_observation_matrix_row_item_id: id) if type =~ /Single/ end |
#matrix_row_item_object ⇒ Object
override
143 144 145 |
# File 'app/models/observation_matrix_row_item.rb', line 143 def matrix_row_item_object nil end |
#move_matrix_row ⇒ Object
The item now covers another object (e.g. unify moved it to the kept object) and/or is in another matrix: decrement the previous observation_object's row (in the previous matrix), increment the new one
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 |
# File 'app/models/observation_matrix_row_item.rb', line 65 def move_matrix_row if observation_object_id_before_last_save # One object replaced by another: repoint a row that would otherwise be # deleted, so it keeps its position if observation_matrix_id_before_last_save == observation_matrix_id mr = ObservationMatrixRow.where( observation_matrix_id:, observation_object_type: observation_object_type_before_last_save, observation_object_id: observation_object_id_before_last_save ).first if mr&.reference_count == 1 && !ObservationMatrixRow.where( observation_matrix_id:, observation_object_type:, observation_object_id: ).exists? mr.update_columns(observation_object_type:, observation_object_id:) return end end cleanup_single_matrix_row_for( observation_object_type_before_last_save, observation_object_id_before_last_save, observation_matrix_id_before_last_save ) end update_matrix_rows end |
#object_is?(object_type) ⇒ matrix_row_item_object?
148 149 150 |
# File 'app/models/observation_matrix_row_item.rb', line 148 def object_is?(object_type) matrix_row_item_object.class.name == object_type ? matrix_row_item_object : nil end |
#observation_objects ⇒ Array
required/defined in subclasses
52 53 54 |
# File 'app/models/observation_matrix_row_item.rb', line 52 def observation_objects [] end |
#update_matrix_rows ⇒ Object
56 57 58 59 60 |
# File 'app/models/observation_matrix_row_item.rb', line 56 def update_matrix_rows observation_objects.each do |o| update_single_matrix_row o end end |
#update_single_matrix_row(object) ⇒ Object
107 108 109 110 111 |
# File 'app/models/observation_matrix_row_item.rb', line 107 def update_single_matrix_row(object) mr = find_or_build_row(object) mr.save! if !mr.persisted? increment_matrix_row_reference_count(mr) end |