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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Housekeeping

#has_polymorphic_relationship?

Class Method Details

+ (Object) sort(array)

Returns true incrementally sort the supplied ids

Parameters:

  • array (Array)

Returns:

  • true incrementally sort the supplied ids



31
32
33
34
35
36
# File 'app/models/observation_matrix_row.rb', line 31

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

- (Object) next_row

TODO: belong in helpers



60
61
62
# File 'app/models/observation_matrix_row.rb', line 60

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

- (Object) observation_matrix_columns



15
16
17
# File 'app/models/observation_matrix_row.rb', line 15

def observation_matrix_columns
  ObservationMatrixColumn.where(observation_matrix_id: observation_matrix_id)
end

- (Scope) observations

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

Returns:

  • (Scope)

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



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

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

- (Object) otu_and_collection_object_blank (private)



112
113
114
115
116
# File 'app/models/observation_matrix_row.rb', line 112

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

- (Object) otu_and_collection_object_given (private)



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

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

- (Object) previous_row



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

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

- (Object) row_object



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

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

- (Object) row_object_class_name



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

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

- (Object) set_reference_count



38
39
40
# File 'app/models/observation_matrix_row.rb', line 38

def set_reference_count
  self.reference_count ||= 0
end