Class: ObservationMatrixRowItem

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Housekeeping, Shared::Citable, Shared::Identifiable, Shared::IsData, Shared::Notable, Shared::Taggable
Defined in:
app/models/observation_matrix_row_item.rb

Overview

A MatrixRowItem defines a set of one OR MORE matrix rows depending on their class.

Direct Known Subclasses

SingleCollectionObject, SingleOtu, TaggedRowItem

Defined Under Namespace

Classes: SingleCollectionObject, SingleOtu, TaggedRowItem

Constant Summary

ALL_STI_ATTRIBUTES =
[:otu_id, :collection_object_id, :controlled_vocabulary_term_id]

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Housekeeping

#has_polymorphic_relationship?

Class Method Details

+ (Object) human_name



69
70
71
# File 'app/models/observation_matrix_row_item.rb', line 69

def self.human_name
  self.name.demodulize.humanize
end

+ (Array) subclass_attributes

override

Returns:

  • (Array)

    the required attributes for this subclass



90
91
92
# File 'app/models/observation_matrix_row_item.rb', line 90

def self.subclass_attributes
  []
end

Instance Method Details

- (Object) cleanup_matrix_rows



35
36
37
38
39
40
41
42
43
# File 'app/models/observation_matrix_row_item.rb', line 35

def cleanup_matrix_rows
  rows = Array.new
  rows.push *ObservationMatrixRow.where(observation_matrix: observation_matrix, otu_id: otus.map(&:id)) if otus
  rows.push *ObservationMatrixRow.where(observation_matrix: observation_matrix, collection_object_id: collection_objects.map(&:id)) if collection_objects

  rows.each do |mr|
    decrement_matrix_row_reference_count mr
  end
end

- (Object) cleanup_single_matrix_row(object)



57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/observation_matrix_row_item.rb', line 57

def cleanup_single_matrix_row(object)
  mr = nil

  if object.is_a? Otu
    mr = ObservationMatrixRow.where(observation_matrix: observation_matrix, otu_id: object.id).first
  elsif object.is_a? CollectionObject
    mr = ObservationMatrixRow.where(observation_matrix: observation_matrix, collection_object_id: object.id).first
  end

  decrement_matrix_row_reference_count mr
end

- (Array) collection_objects

override

Returns:

  • (Array)

    the collection objects “defined” by this matrix row item



83
84
85
# File 'app/models/observation_matrix_row_item.rb', line 83

def collection_objects
  false
end

- (Object) decrement_matrix_row_reference_count(mr) (private)



122
123
124
125
126
127
128
129
130
# File 'app/models/observation_matrix_row_item.rb', line 122

def decrement_matrix_row_reference_count(mr)
  current = mr.reference_count - 1

  if current == 0
    mr.delete
  else
    mr.update_columns(reference_count: current)
  end
end

- (Object) matrix_row_item_object

override

Returns:

  • (Object)

    the object used to define the set of matrix rows



97
98
99
# File 'app/models/observation_matrix_row_item.rb', line 97

def matrix_row_item_object
  nil 
end

- (matrix_row_item_object?) object_is?(object_type)

Returns:

  • (matrix_row_item_object, nil)


102
103
104
# File 'app/models/observation_matrix_row_item.rb', line 102

def object_is?(object_type)
  matrix_row_item_object.class.name == object_type ? matrix_row_item_object : nil
end

- (Object) other_subclass_attributes_not_set (protected)



108
109
110
111
112
# File 'app/models/observation_matrix_row_item.rb', line 108

def other_subclass_attributes_not_set
  (ALL_STI_ATTRIBUTES - self.type.constantize.subclass_attributes).each do |attr|
    errors.add(attr, 'is not valid for this type of observation matrix row item') if !send(attr).blank?
  end
end

- (Array) otus

override

Returns:

  • (Array)

    the otus “defined” by this matrix row item



76
77
78
# File 'app/models/observation_matrix_row_item.rb', line 76

def otus
  false
end

- (Object) type_is_subclassed (protected)



114
115
116
117
118
# File 'app/models/observation_matrix_row_item.rb', line 114

def type_is_subclassed
  if !MATRIX_ROW_ITEM_TYPES[type]
    errors.add(:type, 'type must be a valid subclass')
  end
end

- (Object) update_matrix_rows



25
26
27
28
29
30
31
32
33
# File 'app/models/observation_matrix_row_item.rb', line 25

def update_matrix_rows
  objects = Array.new
  objects.push *otus if otus
  objects.push *collection_objects if collection_objects

  objects.each do |o|
    update_single_matrix_row o
  end
end

- (Object) update_single_matrix_row(object)



45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/observation_matrix_row_item.rb', line 45

def update_single_matrix_row(object)
  mr = nil

  if object.is_a? Otu
    mr = ObservationMatrixRow.find_or_create_by(observation_matrix: observation_matrix, otu: object)
  elsif object.is_a? CollectionObject
    mr = ObservationMatrixRow.find_or_create_by(observation_matrix: observation_matrix, collection_object: object)
  end

  mr.update_columns(reference_count: mr.reference_count + 1)
end