Module: Shared::MatrixHooks::Member

Extended by:
ActiveSupport::Concern
Included in:
Otu, Sound, TaxonName
Defined in:
app/models/concerns/shared/matrix_hooks/member.rb

Overview

Code to update matrices based on what happens with this instance. In this case we check whether updating an attribute might remove this object from a matrix due to direct reference to a corresponding matrix row item.

!! All modules including this code must implement either one or both (row/column) of the in/out "Interface" methods

See spec/models/taxon_name/matrix_hook_spec.rb for specs and exercising.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#member_add_matrix_columns(column_items) ⇒ Object

Columns



93
94
95
96
97
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 93

def member_add_matrix_columns(column_items)
  column_items.each do |mci|
    mci.update_single_matrix_column(self)
  end
end

#member_add_matrix_rows(row_items) ⇒ Object

Rows



79
80
81
82
83
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 79

def member_add_matrix_rows(row_items)
  row_items.each do |mri|
    mri.update_single_matrix_row(self)
  end
end

#member_add_to_matrix_items(of_type = 'row') ⇒ Object



68
69
70
71
72
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 68

def member_add_to_matrix_items(of_type = 'row')
  old = send("member_of_old_matrix_#{of_type}_items")
  new = send("member_of_new_matrix_#{of_type}_items")
  new - (new & old)
end

#member_of_current_matrix_row_items ⇒ Object

Row items this (destroyed) instance is currently a member of through its own attributes (e.g. Otu#taxon_name_id). !! Membership through tags is released by Tag's own matrix hooks, don't include it here. TODO: not good, behavior should (likely) be uniform.



47
48
49
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 47

def member_of_current_matrix_row_items
  []
end

#member_of_new_matrix_column_items ⇒ Object



39
40
41
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 39

def member_of_new_matrix_column_items
  []
end

#member_of_new_matrix_row_items ⇒ Object



31
32
33
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 31

def member_of_new_matrix_row_items
  []
end

#member_of_old_matrix_column_items ⇒ Object



35
36
37
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 35

def member_of_old_matrix_column_items
  []
end

#member_of_old_matrix_row_items ⇒ Object

"Interface" To be defined as needed in member including classes. See app/models/taxon_name/matrix_hooks.rb for an example.



27
28
29
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 27

def member_of_old_matrix_row_items
  []
end

#member_remove_from_matrices ⇒ Object

Decrement (or delete) this destroyed instance's rows, through the row items it was a member of



107
108
109
110
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 107

def member_remove_from_matrices
  return if !ObservationMatrix.where(project_id:).any?
  member_remove_matrix_rows(member_of_current_matrix_row_items)
end

#member_remove_from_matrix_items(of_type = 'row') ⇒ Object



62
63
64
65
66
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 62

def member_remove_from_matrix_items(of_type = 'row')
  old = send("member_of_old_matrix_#{of_type}_items")
  new = send("member_of_new_matrix_#{of_type}_items")
  old - (old & new)
end

#member_remove_matrix_columns(column_items) ⇒ Object



99
100
101
102
103
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 99

def member_remove_matrix_columns(column_items)
  column_items.each do |mci|
    mci.cleanup_single_matrix_column(self)
  end
end

#member_remove_matrix_rows(row_items) ⇒ Object



85
86
87
88
89
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 85

def member_remove_matrix_rows(row_items)
  row_items.each do |mri|
    mri.cleanup_single_matrix_row(self)
  end
end

#member_synchronize_matrices ⇒ Object



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
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 112

def member_synchronize_matrices
  # TODO: refine to eliminate other possibilities
  # Don't bother with the overhead if there are no matrices
  if ObservationMatrix.where(project_id: project_id).any?

    if member_update_matrix_items?('row') || member_update_matrix_items?('column')

      add_rows = member_add_to_matrix_items('row')
      remove_rows = member_remove_from_matrix_items('row')

      add_columns = member_add_to_matrix_items('column')
      remove_columns = member_remove_from_matrix_items('column')

      yield

      member_remove_matrix_rows(remove_rows)
      member_add_matrix_rows(add_rows)

      member_remove_matrix_columns(remove_columns)
      member_add_matrix_columns(add_columns)
    else
      yield
    end
  else
    yield
  end
end

#member_update_matrix_items?(of_type = 'row') ⇒ Boolean

Returns True if we need to update based on matrix row items.

Returns:

  • (Boolean) —

    True if we need to update based on matrix row items



56
57
58
59
60
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 56

def member_update_matrix_items?(of_type = 'row')
  old = send("member_of_old_matrix_#{of_type}_items").to_a
  new = send("member_of_new_matrix_#{of_type}_items").to_a
  old && new != old # intersections assume row_items are not duplicated, which should be the case, but if errors arrise check
end