Class: ObservationMatrixRow

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::Citations, Shared::Identifiers, Shared::IsData, Shared::Notes, Shared::ObservationIndex, Shared::Tags, SoftValidation
Defined in:
app/models/observation_matrix_row.rb

Overview

A ObservationMatrixRow is a row in an Observation matrix representing an Otu or a CollectionObject

Constant Summary

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from 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 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::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

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

transaction_with_retry

Instance Attribute Details

#cached_observation_matrix_row_item_idInteger

Returns if the column item is derived from a ::Single::<FOO> subclass, the id of that instance.

Returns:

  • (Integer)

    if the column item is derived from a ::Single::<FOO> subclass, the id of that instance



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

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::Identifiers
  include Shared::Tags
  include Shared::Notes
  include SoftValidation
  include Shared::IsData

  include Shared::ObservationIndex

  acts_as_list scope: [:observation_matrix_id, :project_id]

  attr_accessor :observation_object_global_id

  belongs_to :observation_object, polymorphic: true
  belongs_to :observation_matrix, inverse_of: :observation_matrix_rows

  validates_presence_of :observation_matrix, :observation_object
  validate :observation_object_is_unique_in_matrix

  # This is strictly a sanity check, as these records
  # are not never created directly
  def observation_object_is_unique_in_matrix
    if ObservationMatrixRow.where(
        observation_matrix_id:,
        observation_object:).where.not(id:).any?
      errors.add(:observation_object, 'already exists in this matrix')
    end
  end

  after_initialize :set_reference_count

  def observations
    observation_object.observations
      .joins(descriptor: [:observation_matrix_columns])
      .where(observation_matrix_columns: {observation_matrix_id:})
      .order('observation_matrix_columns.position ASC')
  end

  def observation_matrix_columns
    ObservationMatrixColumn.where(observation_matrix_id:)
  end

  soft_validate(
    :sv_cannot_be_separated,
    set: :cannot_be_separated,
    name: 'Cannot be separated',
    description: 'Observation matrix row cannot be separated from other rows in the matrix' )

  # @param array [Array]
  # @return true
  #   incrementally sort the supplied ids
  def self.sort(array)
    array.each_with_index do |id, index|
      ObservationMatrixRow.where(id:).update_all(position: index + 1)
    end
    true
  end

  def set_reference_count
    self.reference_count ||= 0
  end

  def observation_object_global_id=(value)
    self.observation_object = GlobalID::Locator.locate(value)
    @observation_object_global_id = value
  end

  def current_taxon_name
    case observation_object_type
    when 'Otu'
      observation_object.taxon_name
    when 'CollectionObject'
      observation_object.current_taxon_name
    end
  end

  # TODO: A little bit suspect here, likely belongs in
  # helper or other lib context
  def current_otu
    case observation_object_type
    when 'Otu'
      observation_object
    when 'CollectionObject'
      observation_object.current_otu
    end
  end

  # TODO: belongs in helpers
  def next_row
    observation_matrix.observation_matrix_rows.where('position > ?', position).order(:position).first
  end

  def previous_row
    observation_matrix.observation_matrix_rows.where('position < ?', position).order('position DESC').first
  end

  private

  def sv_cannot_be_separated
    description = Tools::Description::FromObservationMatrix.new(
      observation_matrix_row_id: self.id,
      project_id: self.project_id
      )
    if description && description.generated_description.blank?
      soft_validations.add(:base, 'No observations!')
    elsif description && description.generated_diagnosis == 'Cannot be separated from other rows in the matrix!'
      str = description.generated_diagnosis.to_s + ' Similar rows:'
      s = description.similar_objects.first[:similarities]
      description.similar_objects.each do |i|
        break if i[:similarities] != s
        if i[:otu_id]
          str += ' ' + Otu.find(i[:otu_id]).otu_name + '.'
        else
          str += ' Collection object #' + i[:collection_object_id].to_s + '.'
        end
      end
      soft_validations.add(:base, str)
    end
  end
end

#nameString?

Returns TEMPORARY value. Allows for a temporary generated/custom name for the row, useful for example when generating labels for phylogenetic trees. This value is NOT persisted and NOT intended for provenance purposes, it is strictly utilitarian. Consider using custom OTUs to track provenance.

Returns:

  • (String, nil)

    TEMPORARY value. Allows for a temporary generated/custom name for the row, useful for example when generating labels for phylogenetic trees. This value is NOT persisted and NOT intended for provenance purposes, it is strictly utilitarian. Consider using custom OTUs to track provenance.



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

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::Identifiers
  include Shared::Tags
  include Shared::Notes
  include SoftValidation
  include Shared::IsData

  include Shared::ObservationIndex

  acts_as_list scope: [:observation_matrix_id, :project_id]

  attr_accessor :observation_object_global_id

  belongs_to :observation_object, polymorphic: true
  belongs_to :observation_matrix, inverse_of: :observation_matrix_rows

  validates_presence_of :observation_matrix, :observation_object
  validate :observation_object_is_unique_in_matrix

  # This is strictly a sanity check, as these records
  # are not never created directly
  def observation_object_is_unique_in_matrix
    if ObservationMatrixRow.where(
        observation_matrix_id:,
        observation_object:).where.not(id:).any?
      errors.add(:observation_object, 'already exists in this matrix')
    end
  end

  after_initialize :set_reference_count

  def observations
    observation_object.observations
      .joins(descriptor: [:observation_matrix_columns])
      .where(observation_matrix_columns: {observation_matrix_id:})
      .order('observation_matrix_columns.position ASC')
  end

  def observation_matrix_columns
    ObservationMatrixColumn.where(observation_matrix_id:)
  end

  soft_validate(
    :sv_cannot_be_separated,
    set: :cannot_be_separated,
    name: 'Cannot be separated',
    description: 'Observation matrix row cannot be separated from other rows in the matrix' )

  # @param array [Array]
  # @return true
  #   incrementally sort the supplied ids
  def self.sort(array)
    array.each_with_index do |id, index|
      ObservationMatrixRow.where(id:).update_all(position: index + 1)
    end
    true
  end

  def set_reference_count
    self.reference_count ||= 0
  end

  def observation_object_global_id=(value)
    self.observation_object = GlobalID::Locator.locate(value)
    @observation_object_global_id = value
  end

  def current_taxon_name
    case observation_object_type
    when 'Otu'
      observation_object.taxon_name
    when 'CollectionObject'
      observation_object.current_taxon_name
    end
  end

  # TODO: A little bit suspect here, likely belongs in
  # helper or other lib context
  def current_otu
    case observation_object_type
    when 'Otu'
      observation_object
    when 'CollectionObject'
      observation_object.current_otu
    end
  end

  # TODO: belongs in helpers
  def next_row
    observation_matrix.observation_matrix_rows.where('position > ?', position).order(:position).first
  end

  def previous_row
    observation_matrix.observation_matrix_rows.where('position < ?', position).order('position DESC').first
  end

  private

  def sv_cannot_be_separated
    description = Tools::Description::FromObservationMatrix.new(
      observation_matrix_row_id: self.id,
      project_id: self.project_id
      )
    if description && description.generated_description.blank?
      soft_validations.add(:base, 'No observations!')
    elsif description && description.generated_diagnosis == 'Cannot be separated from other rows in the matrix!'
      str = description.generated_diagnosis.to_s + ' Similar rows:'
      s = description.similar_objects.first[:similarities]
      description.similar_objects.each do |i|
        break if i[:similarities] != s
        if i[:otu_id]
          str += ' ' + Otu.find(i[:otu_id]).otu_name + '.'
        else
          str += ' Collection object #' + i[:collection_object_id].to_s + '.'
        end
      end
      soft_validations.add(:base, str)
    end
  end
end

#observation_matrix_idInteger

Returns id of the matrix the row is in.

Returns:

  • (Integer)

    id of the matrix the row is in



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

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::Identifiers
  include Shared::Tags
  include Shared::Notes
  include SoftValidation
  include Shared::IsData

  include Shared::ObservationIndex

  acts_as_list scope: [:observation_matrix_id, :project_id]

  attr_accessor :observation_object_global_id

  belongs_to :observation_object, polymorphic: true
  belongs_to :observation_matrix, inverse_of: :observation_matrix_rows

  validates_presence_of :observation_matrix, :observation_object
  validate :observation_object_is_unique_in_matrix

  # This is strictly a sanity check, as these records
  # are not never created directly
  def observation_object_is_unique_in_matrix
    if ObservationMatrixRow.where(
        observation_matrix_id:,
        observation_object:).where.not(id:).any?
      errors.add(:observation_object, 'already exists in this matrix')
    end
  end

  after_initialize :set_reference_count

  def observations
    observation_object.observations
      .joins(descriptor: [:observation_matrix_columns])
      .where(observation_matrix_columns: {observation_matrix_id:})
      .order('observation_matrix_columns.position ASC')
  end

  def observation_matrix_columns
    ObservationMatrixColumn.where(observation_matrix_id:)
  end

  soft_validate(
    :sv_cannot_be_separated,
    set: :cannot_be_separated,
    name: 'Cannot be separated',
    description: 'Observation matrix row cannot be separated from other rows in the matrix' )

  # @param array [Array]
  # @return true
  #   incrementally sort the supplied ids
  def self.sort(array)
    array.each_with_index do |id, index|
      ObservationMatrixRow.where(id:).update_all(position: index + 1)
    end
    true
  end

  def set_reference_count
    self.reference_count ||= 0
  end

  def observation_object_global_id=(value)
    self.observation_object = GlobalID::Locator.locate(value)
    @observation_object_global_id = value
  end

  def current_taxon_name
    case observation_object_type
    when 'Otu'
      observation_object.taxon_name
    when 'CollectionObject'
      observation_object.current_taxon_name
    end
  end

  # TODO: A little bit suspect here, likely belongs in
  # helper or other lib context
  def current_otu
    case observation_object_type
    when 'Otu'
      observation_object
    when 'CollectionObject'
      observation_object.current_otu
    end
  end

  # TODO: belongs in helpers
  def next_row
    observation_matrix.observation_matrix_rows.where('position > ?', position).order(:position).first
  end

  def previous_row
    observation_matrix.observation_matrix_rows.where('position < ?', position).order('position DESC').first
  end

  private

  def sv_cannot_be_separated
    description = Tools::Description::FromObservationMatrix.new(
      observation_matrix_row_id: self.id,
      project_id: self.project_id
      )
    if description && description.generated_description.blank?
      soft_validations.add(:base, 'No observations!')
    elsif description && description.generated_diagnosis == 'Cannot be separated from other rows in the matrix!'
      str = description.generated_diagnosis.to_s + ' Similar rows:'
      s = description.similar_objects.first[:similarities]
      description.similar_objects.each do |i|
        break if i[:similarities] != s
        if i[:otu_id]
          str += ' ' + Otu.find(i[:otu_id]).otu_name + '.'
        else
          str += ' Collection object #' + i[:collection_object_id].to_s + '.'
        end
      end
      soft_validations.add(:base, str)
    end
  end
end

#observation_object_global_idObject

Returns the value of attribute observation_object_global_id.



41
42
43
# File 'app/models/observation_matrix_row.rb', line 41

def observation_object_global_id
  @observation_object_global_id
end

#observation_object_idInteger?

Returns:

  • (Integer, nil)


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

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::Identifiers
  include Shared::Tags
  include Shared::Notes
  include SoftValidation
  include Shared::IsData

  include Shared::ObservationIndex

  acts_as_list scope: [:observation_matrix_id, :project_id]

  attr_accessor :observation_object_global_id

  belongs_to :observation_object, polymorphic: true
  belongs_to :observation_matrix, inverse_of: :observation_matrix_rows

  validates_presence_of :observation_matrix, :observation_object
  validate :observation_object_is_unique_in_matrix

  # This is strictly a sanity check, as these records
  # are not never created directly
  def observation_object_is_unique_in_matrix
    if ObservationMatrixRow.where(
        observation_matrix_id:,
        observation_object:).where.not(id:).any?
      errors.add(:observation_object, 'already exists in this matrix')
    end
  end

  after_initialize :set_reference_count

  def observations
    observation_object.observations
      .joins(descriptor: [:observation_matrix_columns])
      .where(observation_matrix_columns: {observation_matrix_id:})
      .order('observation_matrix_columns.position ASC')
  end

  def observation_matrix_columns
    ObservationMatrixColumn.where(observation_matrix_id:)
  end

  soft_validate(
    :sv_cannot_be_separated,
    set: :cannot_be_separated,
    name: 'Cannot be separated',
    description: 'Observation matrix row cannot be separated from other rows in the matrix' )

  # @param array [Array]
  # @return true
  #   incrementally sort the supplied ids
  def self.sort(array)
    array.each_with_index do |id, index|
      ObservationMatrixRow.where(id:).update_all(position: index + 1)
    end
    true
  end

  def set_reference_count
    self.reference_count ||= 0
  end

  def observation_object_global_id=(value)
    self.observation_object = GlobalID::Locator.locate(value)
    @observation_object_global_id = value
  end

  def current_taxon_name
    case observation_object_type
    when 'Otu'
      observation_object.taxon_name
    when 'CollectionObject'
      observation_object.current_taxon_name
    end
  end

  # TODO: A little bit suspect here, likely belongs in
  # helper or other lib context
  def current_otu
    case observation_object_type
    when 'Otu'
      observation_object
    when 'CollectionObject'
      observation_object.current_otu
    end
  end

  # TODO: belongs in helpers
  def next_row
    observation_matrix.observation_matrix_rows.where('position > ?', position).order(:position).first
  end

  def previous_row
    observation_matrix.observation_matrix_rows.where('position < ?', position).order('position DESC').first
  end

  private

  def sv_cannot_be_separated
    description = Tools::Description::FromObservationMatrix.new(
      observation_matrix_row_id: self.id,
      project_id: self.project_id
      )
    if description && description.generated_description.blank?
      soft_validations.add(:base, 'No observations!')
    elsif description && description.generated_diagnosis == 'Cannot be separated from other rows in the matrix!'
      str = description.generated_diagnosis.to_s + ' Similar rows:'
      s = description.similar_objects.first[:similarities]
      description.similar_objects.each do |i|
        break if i[:similarities] != s
        if i[:otu_id]
          str += ' ' + Otu.find(i[:otu_id]).otu_name + '.'
        else
          str += ' Collection object #' + i[:collection_object_id].to_s + '.'
        end
      end
      soft_validations.add(:base, str)
    end
  end
end

#observation_object_typeString?

Returns:

  • (String, nil)


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

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::Identifiers
  include Shared::Tags
  include Shared::Notes
  include SoftValidation
  include Shared::IsData

  include Shared::ObservationIndex

  acts_as_list scope: [:observation_matrix_id, :project_id]

  attr_accessor :observation_object_global_id

  belongs_to :observation_object, polymorphic: true
  belongs_to :observation_matrix, inverse_of: :observation_matrix_rows

  validates_presence_of :observation_matrix, :observation_object
  validate :observation_object_is_unique_in_matrix

  # This is strictly a sanity check, as these records
  # are not never created directly
  def observation_object_is_unique_in_matrix
    if ObservationMatrixRow.where(
        observation_matrix_id:,
        observation_object:).where.not(id:).any?
      errors.add(:observation_object, 'already exists in this matrix')
    end
  end

  after_initialize :set_reference_count

  def observations
    observation_object.observations
      .joins(descriptor: [:observation_matrix_columns])
      .where(observation_matrix_columns: {observation_matrix_id:})
      .order('observation_matrix_columns.position ASC')
  end

  def observation_matrix_columns
    ObservationMatrixColumn.where(observation_matrix_id:)
  end

  soft_validate(
    :sv_cannot_be_separated,
    set: :cannot_be_separated,
    name: 'Cannot be separated',
    description: 'Observation matrix row cannot be separated from other rows in the matrix' )

  # @param array [Array]
  # @return true
  #   incrementally sort the supplied ids
  def self.sort(array)
    array.each_with_index do |id, index|
      ObservationMatrixRow.where(id:).update_all(position: index + 1)
    end
    true
  end

  def set_reference_count
    self.reference_count ||= 0
  end

  def observation_object_global_id=(value)
    self.observation_object = GlobalID::Locator.locate(value)
    @observation_object_global_id = value
  end

  def current_taxon_name
    case observation_object_type
    when 'Otu'
      observation_object.taxon_name
    when 'CollectionObject'
      observation_object.current_taxon_name
    end
  end

  # TODO: A little bit suspect here, likely belongs in
  # helper or other lib context
  def current_otu
    case observation_object_type
    when 'Otu'
      observation_object
    when 'CollectionObject'
      observation_object.current_otu
    end
  end

  # TODO: belongs in helpers
  def next_row
    observation_matrix.observation_matrix_rows.where('position > ?', position).order(:position).first
  end

  def previous_row
    observation_matrix.observation_matrix_rows.where('position < ?', position).order('position DESC').first
  end

  private

  def sv_cannot_be_separated
    description = Tools::Description::FromObservationMatrix.new(
      observation_matrix_row_id: self.id,
      project_id: self.project_id
      )
    if description && description.generated_description.blank?
      soft_validations.add(:base, 'No observations!')
    elsif description && description.generated_diagnosis == 'Cannot be separated from other rows in the matrix!'
      str = description.generated_diagnosis.to_s + ' Similar rows:'
      s = description.similar_objects.first[:similarities]
      description.similar_objects.each do |i|
        break if i[:similarities] != s
        if i[:otu_id]
          str += ' ' + Otu.find(i[:otu_id]).otu_name + '.'
        else
          str += ' Collection object #' + i[:collection_object_id].to_s + '.'
        end
      end
      soft_validations.add(:base, str)
    end
  end
end

#positionInteger

Returns from acts as list.

Returns:

  • (Integer)

    from acts as list



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

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::Identifiers
  include Shared::Tags
  include Shared::Notes
  include SoftValidation
  include Shared::IsData

  include Shared::ObservationIndex

  acts_as_list scope: [:observation_matrix_id, :project_id]

  attr_accessor :observation_object_global_id

  belongs_to :observation_object, polymorphic: true
  belongs_to :observation_matrix, inverse_of: :observation_matrix_rows

  validates_presence_of :observation_matrix, :observation_object
  validate :observation_object_is_unique_in_matrix

  # This is strictly a sanity check, as these records
  # are not never created directly
  def observation_object_is_unique_in_matrix
    if ObservationMatrixRow.where(
        observation_matrix_id:,
        observation_object:).where.not(id:).any?
      errors.add(:observation_object, 'already exists in this matrix')
    end
  end

  after_initialize :set_reference_count

  def observations
    observation_object.observations
      .joins(descriptor: [:observation_matrix_columns])
      .where(observation_matrix_columns: {observation_matrix_id:})
      .order('observation_matrix_columns.position ASC')
  end

  def observation_matrix_columns
    ObservationMatrixColumn.where(observation_matrix_id:)
  end

  soft_validate(
    :sv_cannot_be_separated,
    set: :cannot_be_separated,
    name: 'Cannot be separated',
    description: 'Observation matrix row cannot be separated from other rows in the matrix' )

  # @param array [Array]
  # @return true
  #   incrementally sort the supplied ids
  def self.sort(array)
    array.each_with_index do |id, index|
      ObservationMatrixRow.where(id:).update_all(position: index + 1)
    end
    true
  end

  def set_reference_count
    self.reference_count ||= 0
  end

  def observation_object_global_id=(value)
    self.observation_object = GlobalID::Locator.locate(value)
    @observation_object_global_id = value
  end

  def current_taxon_name
    case observation_object_type
    when 'Otu'
      observation_object.taxon_name
    when 'CollectionObject'
      observation_object.current_taxon_name
    end
  end

  # TODO: A little bit suspect here, likely belongs in
  # helper or other lib context
  def current_otu
    case observation_object_type
    when 'Otu'
      observation_object
    when 'CollectionObject'
      observation_object.current_otu
    end
  end

  # TODO: belongs in helpers
  def next_row
    observation_matrix.observation_matrix_rows.where('position > ?', position).order(:position).first
  end

  def previous_row
    observation_matrix.observation_matrix_rows.where('position < ?', position).order('position DESC').first
  end

  private

  def sv_cannot_be_separated
    description = Tools::Description::FromObservationMatrix.new(
      observation_matrix_row_id: self.id,
      project_id: self.project_id
      )
    if description && description.generated_description.blank?
      soft_validations.add(:base, 'No observations!')
    elsif description && description.generated_diagnosis == 'Cannot be separated from other rows in the matrix!'
      str = description.generated_diagnosis.to_s + ' Similar rows:'
      s = description.similar_objects.first[:similarities]
      description.similar_objects.each do |i|
        break if i[:similarities] != s
        if i[:otu_id]
          str += ' ' + Otu.find(i[:otu_id]).otu_name + '.'
        else
          str += ' Collection object #' + i[:collection_object_id].to_s + '.'
        end
      end
      soft_validations.add(:base, str)
    end
  end
end

#reference_countInteger

Indicates the total number of times this row is referened via some row_item

Returns:

  • (Integer)


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

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::Identifiers
  include Shared::Tags
  include Shared::Notes
  include SoftValidation
  include Shared::IsData

  include Shared::ObservationIndex

  acts_as_list scope: [:observation_matrix_id, :project_id]

  attr_accessor :observation_object_global_id

  belongs_to :observation_object, polymorphic: true
  belongs_to :observation_matrix, inverse_of: :observation_matrix_rows

  validates_presence_of :observation_matrix, :observation_object
  validate :observation_object_is_unique_in_matrix

  # This is strictly a sanity check, as these records
  # are not never created directly
  def observation_object_is_unique_in_matrix
    if ObservationMatrixRow.where(
        observation_matrix_id:,
        observation_object:).where.not(id:).any?
      errors.add(:observation_object, 'already exists in this matrix')
    end
  end

  after_initialize :set_reference_count

  def observations
    observation_object.observations
      .joins(descriptor: [:observation_matrix_columns])
      .where(observation_matrix_columns: {observation_matrix_id:})
      .order('observation_matrix_columns.position ASC')
  end

  def observation_matrix_columns
    ObservationMatrixColumn.where(observation_matrix_id:)
  end

  soft_validate(
    :sv_cannot_be_separated,
    set: :cannot_be_separated,
    name: 'Cannot be separated',
    description: 'Observation matrix row cannot be separated from other rows in the matrix' )

  # @param array [Array]
  # @return true
  #   incrementally sort the supplied ids
  def self.sort(array)
    array.each_with_index do |id, index|
      ObservationMatrixRow.where(id:).update_all(position: index + 1)
    end
    true
  end

  def set_reference_count
    self.reference_count ||= 0
  end

  def observation_object_global_id=(value)
    self.observation_object = GlobalID::Locator.locate(value)
    @observation_object_global_id = value
  end

  def current_taxon_name
    case observation_object_type
    when 'Otu'
      observation_object.taxon_name
    when 'CollectionObject'
      observation_object.current_taxon_name
    end
  end

  # TODO: A little bit suspect here, likely belongs in
  # helper or other lib context
  def current_otu
    case observation_object_type
    when 'Otu'
      observation_object
    when 'CollectionObject'
      observation_object.current_otu
    end
  end

  # TODO: belongs in helpers
  def next_row
    observation_matrix.observation_matrix_rows.where('position > ?', position).order(:position).first
  end

  def previous_row
    observation_matrix.observation_matrix_rows.where('position < ?', position).order('position DESC').first
  end

  private

  def sv_cannot_be_separated
    description = Tools::Description::FromObservationMatrix.new(
      observation_matrix_row_id: self.id,
      project_id: self.project_id
      )
    if description && description.generated_description.blank?
      soft_validations.add(:base, 'No observations!')
    elsif description && description.generated_diagnosis == 'Cannot be separated from other rows in the matrix!'
      str = description.generated_diagnosis.to_s + ' Similar rows:'
      s = description.similar_objects.first[:similarities]
      description.similar_objects.each do |i|
        break if i[:similarities] != s
        if i[:otu_id]
          str += ' ' + Otu.find(i[:otu_id]).otu_name + '.'
        else
          str += ' Collection object #' + i[:collection_object_id].to_s + '.'
        end
      end
      soft_validations.add(:base, str)
    end
  end
end

Class Method Details

.sort(array) ⇒ Object

Returns true incrementally sort the supplied ids.

Parameters:

  • array (Array)

Returns:

  • true incrementally sort the supplied ids



81
82
83
84
85
86
# File 'app/models/observation_matrix_row.rb', line 81

def self.sort(array)
  array.each_with_index do |id, index|
    ObservationMatrixRow.where(id:).update_all(position: index + 1)
  end
  true
end

Instance Method Details

#current_otuObject

TODO: A little bit suspect here, likely belongs in helper or other lib context



108
109
110
111
112
113
114
115
# File 'app/models/observation_matrix_row.rb', line 108

def current_otu
  case observation_object_type
  when 'Otu'
    observation_object
  when 'CollectionObject'
    observation_object.current_otu
  end
end

#current_taxon_nameObject



97
98
99
100
101
102
103
104
# File 'app/models/observation_matrix_row.rb', line 97

def current_taxon_name
  case observation_object_type
  when 'Otu'
    observation_object.taxon_name
  when 'CollectionObject'
    observation_object.current_taxon_name
  end
end

#next_rowObject

TODO: belongs in helpers



118
119
120
# File 'app/models/observation_matrix_row.rb', line 118

def next_row
  observation_matrix.observation_matrix_rows.where('position > ?', position).order(:position).first
end

#observation_matrix_columnsObject



68
69
70
# File 'app/models/observation_matrix_row.rb', line 68

def observation_matrix_columns
  ObservationMatrixColumn.where(observation_matrix_id:)
end

#observation_object_is_unique_in_matrixObject

This is strictly a sanity check, as these records are not never created directly



51
52
53
54
55
56
57
# File 'app/models/observation_matrix_row.rb', line 51

def observation_object_is_unique_in_matrix
  if ObservationMatrixRow.where(
      observation_matrix_id:,
      observation_object:).where.not(id:).any?
    errors.add(:observation_object, 'already exists in this matrix')
  end
end

#observationsObject



61
62
63
64
65
66
# File 'app/models/observation_matrix_row.rb', line 61

def observations
  observation_object.observations
    .joins(descriptor: [:observation_matrix_columns])
    .where(observation_matrix_columns: {observation_matrix_id:})
    .order('observation_matrix_columns.position ASC')
end

#previous_rowObject



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

def previous_row
  observation_matrix.observation_matrix_rows.where('position < ?', position).order('position DESC').first
end

#set_reference_countObject



88
89
90
# File 'app/models/observation_matrix_row.rb', line 88

def set_reference_count
  self.reference_count ||= 0
end

#sv_cannot_be_separatedObject (private)



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/observation_matrix_row.rb', line 128

def sv_cannot_be_separated
  description = Tools::Description::FromObservationMatrix.new(
    observation_matrix_row_id: self.id,
    project_id: self.project_id
    )
  if description && description.generated_description.blank?
    soft_validations.add(:base, 'No observations!')
  elsif description && description.generated_diagnosis == 'Cannot be separated from other rows in the matrix!'
    str = description.generated_diagnosis.to_s + ' Similar rows:'
    s = description.similar_objects.first[:similarities]
    description.similar_objects.each do |i|
      break if i[:similarities] != s
      if i[:otu_id]
        str += ' ' + Otu.find(i[:otu_id]).otu_name + '.'
      else
        str += ' Collection object #' + i[:collection_object_id].to_s + '.'
      end
    end
    soft_validations.add(:base, str)
  end
end