Class: ObservationMatrixRow

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

Defined Under Namespace

Classes: Autocomplete

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Housekeeping

#has_polymorphic_relationship?

Instance Attribute Details

#row_object_global_idObject

! if row_object changes (it never should, just create/destroy) this memoization is bad



56
57
58
# File 'app/models/observation_matrix_row.rb', line 56

def row_object_global_id
  @row_object_global_id
end

Class Method Details

.sort(array) ⇒ Object

Returns true incrementally sort the supplied ids

Parameters:

  • array (Array)

Returns:

  • true incrementally sort the supplied ids



33
34
35
36
37
38
# File 'app/models/observation_matrix_row.rb', line 33

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

Instance Method Details

#current_taxon_nameObject



67
68
69
70
71
72
73
74
# File 'app/models/observation_matrix_row.rb', line 67

def current_taxon_name
  case row_object_class_name
  when 'Otu'
    row_object.taxon_name
  when 'CollectionObject'
    row_object.current_taxon_name
  end
end

#next_rowObject

TODO: belong in helpers



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

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

#observation_matrix_columnsObject



17
18
19
# File 'app/models/observation_matrix_row.rb', line 17

def observation_matrix_columns
  ObservationMatrixColumn.where(observation_matrix_id: observation_matrix_id)
end

#observationsScope

Returns all the observations in this row, ordered (but not gathered)

Returns:

  • (Scope)

    all the observations in this row, ordered (but not gathered)



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

def observations
  t = Observation.arel_table

  a = t.alias
  b = t.project(a[Arel.star]).from(a)

  c = ObservationMatrixColumn.arel_table

  d = Descriptor.arel_table
  f = d.project(d[Arel.star]).from(d)

  # Descriptors in the matrix
  f = f.join(c, Arel::Nodes::InnerJoin)
    .on(
      d[:id].eq(c[:descriptor_id]).
      and( c[:observation_matrix_id].eq(observation_matrix_id) )
  ).order(c[:position]).as('a1')

  # TODO: when polymorphic this whole method will collapse and
  # not require this fork
  x = nil
  case row_object_class_name
  when  'Otu'
    x = a[:otu_id].eq(otu_id)
  when 'CollectionObject'
    x = a[:collection_object_id].eq(collection_object_id)
  else
    raise
  end

  # Observations from those descriptros
  b = b.join(f, Arel::Nodes::InnerJoin)
    .on(
      a[:descriptor_id].eq(f[:id]).
      and(x)
  ).as('a2')

  ::Observation.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(t['id']))))
end

#otu_and_collection_object_blankObject (private)



129
130
131
132
133
# File 'app/models/observation_matrix_row.rb', line 129

def otu_and_collection_object_blank
  if otu_id.nil? && collection_object_id.nil?
    errors.add(:base, 'Specify otu OR collection object!')
  end
end

#otu_and_collection_object_givenObject (private)



135
136
137
138
139
# File 'app/models/observation_matrix_row.rb', line 135

def otu_and_collection_object_given
  if !otu_id.nil? && !collection_object_id.nil?
    errors.add(:base, 'Specify otu OR collection object, not both!')
  end
end

#previous_rowObject



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

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

#row_objectObject



44
45
46
47
48
49
50
51
52
53
# File 'app/models/observation_matrix_row.rb', line 44

def row_object
  # exit as quickly as possible
  if o = otu
    return o
  end
  if c = collection_object
    return c
  end
  nil
end

#row_object_class_nameObject



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

def row_object_class_name
  return 'Otu' if otu_id
  return 'CollectionObject' if collection_object_id
  raise
end

#set_reference_countObject



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

def set_reference_count
  self.reference_count ||= 0
end