Class: ObservationMatrixRow

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::IsData, Shared::ObservationIndex, 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 Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#cached_observation_matrix_row_item_idInteger

Returns if this row is derived from a ::Single row_item subclass, the id of that instance. (Does not imply this row isn’t also referenced by a Dynamic row item.).

Returns:

  • (Integer)

    if this row is derived from a ::Single row_item subclass, the id of that instance. (Does not imply this row isn’t also referenced by a Dynamic row item.)



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

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping

  # If requested and implemented downstream then
  # .unify() will need to be addrssed with either:
  # 1) simply allowing the annotations to be destroyed without notice
  # 2) adding custom handlers
  # The core issue is that we don't have (and can't have) a relation
  # between observation_matrix_row_item and observation_matrix_row, that
  # relation his handled by a function/callback.
  #
  # 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
150
151
152
153
154
155
156
157
158
159
# File 'app/models/observation_matrix_row.rb', line 28

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping

  # If requested and implemented downstream then
  # .unify() will need to be addrssed with either:
  # 1) simply allowing the annotations to be destroyed without notice
  # 2) adding custom handlers
  # The core issue is that we don't have (and can't have) a relation
  # between observation_matrix_row_item and observation_matrix_row, that
  # relation his handled by a function/callback.
  #
  # 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
150
151
152
153
154
155
156
157
158
159
# File 'app/models/observation_matrix_row.rb', line 28

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping

  # If requested and implemented downstream then
  # .unify() will need to be addrssed with either:
  # 1) simply allowing the annotations to be destroyed without notice
  # 2) adding custom handlers
  # The core issue is that we don't have (and can't have) a relation
  # between observation_matrix_row_item and observation_matrix_row, that
  # relation his handled by a function/callback.
  #
  # 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.



51
52
53
# File 'app/models/observation_matrix_row.rb', line 51

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
150
151
152
153
154
155
156
157
158
159
# File 'app/models/observation_matrix_row.rb', line 28

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping

  # If requested and implemented downstream then
  # .unify() will need to be addrssed with either:
  # 1) simply allowing the annotations to be destroyed without notice
  # 2) adding custom handlers
  # The core issue is that we don't have (and can't have) a relation
  # between observation_matrix_row_item and observation_matrix_row, that
  # relation his handled by a function/callback.
  #
  # 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
150
151
152
153
154
155
156
157
158
159
# File 'app/models/observation_matrix_row.rb', line 28

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping

  # If requested and implemented downstream then
  # .unify() will need to be addrssed with either:
  # 1) simply allowing the annotations to be destroyed without notice
  # 2) adding custom handlers
  # The core issue is that we don't have (and can't have) a relation
  # between observation_matrix_row_item and observation_matrix_row, that
  # relation his handled by a function/callback.
  #
  # 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
150
151
152
153
154
155
156
157
158
159
# File 'app/models/observation_matrix_row.rb', line 28

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping

  # If requested and implemented downstream then
  # .unify() will need to be addrssed with either:
  # 1) simply allowing the annotations to be destroyed without notice
  # 2) adding custom handlers
  # The core issue is that we don't have (and can't have) a relation
  # between observation_matrix_row_item and observation_matrix_row, that
  # relation his handled by a function/callback.
  #
  # 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 referenced 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
150
151
152
153
154
155
156
157
158
159
# File 'app/models/observation_matrix_row.rb', line 28

class ObservationMatrixRow < ApplicationRecord
  include Housekeeping

  # If requested and implemented downstream then
  # .unify() will need to be addrssed with either:
  # 1) simply allowing the annotations to be destroyed without notice
  # 2) adding custom handlers
  # The core issue is that we don't have (and can't have) a relation
  # between observation_matrix_row_item and observation_matrix_row, that
  # relation his handled by a function/callback.
  #
  # 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



91
92
93
94
95
96
# File 'app/models/observation_matrix_row.rb', line 91

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



118
119
120
121
122
123
124
125
# File 'app/models/observation_matrix_row.rb', line 118

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

#current_taxon_nameObject



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

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



128
129
130
# File 'app/models/observation_matrix_row.rb', line 128

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

#observation_matrix_columnsObject



78
79
80
# File 'app/models/observation_matrix_row.rb', line 78

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



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

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



71
72
73
74
75
76
# File 'app/models/observation_matrix_row.rb', line 71

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



132
133
134
# File 'app/models/observation_matrix_row.rb', line 132

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

#set_reference_countObject



98
99
100
# File 'app/models/observation_matrix_row.rb', line 98

def set_reference_count
  self.reference_count ||= 0
end

#sv_cannot_be_separatedObject (private)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/models/observation_matrix_row.rb', line 138

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