Module: Shared::MatrixHooks::Member

Extended by:
ActiveSupport::Concern
Included in:
Otu
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(columns) ⇒ Object

Columns



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

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

#member_add_matrix_rows(rows) ⇒ Object

Rows



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

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

#member_add_to_matrix_items(of_type = 'row') ⇒ Object



59
60
61
62
63
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 59

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_new_matrix_column_itemsObject



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

def member_of_new_matrix_column_items
  []
end

#member_of_new_matrix_row_itemsObject



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

def member_of_new_matrix_row_items
  []
end

#member_of_old_matrix_column_itemsObject



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

def member_of_old_matrix_column_items
  []
end

#member_of_old_matrix_row_itemsObject

“Interface” To be defined as needed in member including classes.

See app/models/taxon_name/matrix_hooks.rb for an example.



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

def member_of_old_matrix_row_items
  []
end

#member_remove_from_matrix_items(of_type = 'row') ⇒ Object



53
54
55
56
57
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 53

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(columns) ⇒ Object



90
91
92
93
94
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 90

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

#member_remove_matrix_rows(rows) ⇒ Object



76
77
78
79
80
# File 'app/models/concerns/shared/matrix_hooks/member.rb', line 76

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

#member_syncronize_matricesObject



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

def member_syncronize_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



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

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