Module: Shared::MatrixHooks::Dynamic

Extended by:
ActiveSupport::Concern
Included in:
Tag
Defined in:
app/models/concerns/shared/matrix_hooks/dynamic.rb

Overview

Code to update (dynamic) matrix content based on what goes on with this instance.

This code handles first creates and changes, not use increments nor destroys.  It ensures there 
are rows/columns created when changes happens, that's it.

This module handles a once-removed abstraction edge case.

!! 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

#dynamic_add_to_matrix_column_itemsObject



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

def dynamic_add_to_matrix_column_items
  in_scope_observation_matrix_column_items - (in_scope_observation_matrix_column_items & out_of_scope_observation_matrix_column_items)
end

#dynamic_add_to_matrix_row_itemsObject



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

def dynamic_add_to_matrix_row_items
  in_scope_observation_matrix_row_items - (in_scope_observation_matrix_row_items & out_of_scope_observation_matrix_row_items)
end

#dynamic_cleanup_in_scope_column_items(columns) ⇒ Object

adding, incrementing



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 116

def dynamic_cleanup_in_scope_column_items(columns)
  columns.each do |mci|
    mci.column_objects.each do |o|
      p = mci.find_or_build_column(o)
      if p.persisted?
       # See row version.  
       #if p.descriptor == o
       #  #byebug
       #  #p.update_column(:reference_count,  p.reference_count + 1) 
       #end
        #  see "only added observation_matrix column is incremented" spec  # mci.increment_matrix_column_reference_count(p)
      else
        p.reference_count = 1
        p.save!
      end
    end
  end
end

#dynamic_cleanup_in_scope_row_items(rows) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 83

def dynamic_cleanup_in_scope_row_items(rows)
  rows.each do |mri|
    mri.row_objects.each do |o|
      p = mri.find_or_build_row(o)
      if p.persisted?
       #if p.row_object == o
       #  p.update_column(:reference_count,  p.reference_count + 1) 
       #end
      else
        p.reference_count = 1
        p.save!
      end
    end
  end
end

#dynamic_cleanup_out_of_scope_column_items(columns) ⇒ Object

removing, decrementing



136
137
138
139
140
141
142
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 136

def dynamic_cleanup_out_of_scope_column_items(columns)
  columns.each do |mci|
    dynamic_column_out_map[mci.id].each do |o|
      mci.cleanup_single_matrix_column(o)
    end
  end
end

#dynamic_cleanup_out_of_scope_row_items(rows) ⇒ Object



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

def dynamic_cleanup_out_of_scope_row_items(rows)
  rows.each do |mri|
    dynamic_row_out_map[mri.id].each do |o|
      mri.cleanup_single_matrix_row(o)
    end
  end
end

#dynamic_column_items_inObject



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

def dynamic_column_items_in 
  @dynamic_column_items_in ||= in_scope_observation_matrix_column_items
end

#dynamic_column_items_outObject



111
112
113
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 111

def dynamic_column_items_out
  @dynamic_column_items_out ||= out_of_scope_observation_matrix_column_items
end

#dynamic_inspect_matricesObject



178
179
180
181
182
183
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 178

def dynamic_inspect_matrices
  # TODO: strengthen checks for whether this is necessary
  if ObservationMatrix.where(project_id: project_id).any?
    prepare_matrix_items
  end      
end

#dynamic_remove_from_matrix_column_itemsObject



67
68
69
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 67

def dynamic_remove_from_matrix_column_items
  out_of_scope_observation_matrix_column_items - (out_of_scope_observation_matrix_column_items & in_scope_observation_matrix_column_items)
end

#dynamic_remove_from_matrix_row_itemsObject



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

def dynamic_remove_from_matrix_row_items
  out_of_scope_observation_matrix_row_items - (out_of_scope_observation_matrix_row_items & in_scope_observation_matrix_row_items)
end

#dynamic_row_items_inObject



75
76
77
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 75

def dynamic_row_items_in 
  @dynamic_row_items_in ||= in_scope_observation_matrix_row_items
end

#dynamic_row_items_outObject



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

def dynamic_row_items_out
  @dynamic_row_items_out ||= out_of_scope_observation_matrix_row_items
end

#dynamic_syncronize_matricesObject



144
145
146
147
148
149
150
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 144

def dynamic_syncronize_matrices
  dynamic_cleanup_out_of_scope_row_items(@dynamic_row_items_out) if @dynamic_row_items_out
  dynamic_cleanup_in_scope_row_items(@dynamic_row_items_in) if @dynamic_row_items_in

  dynamic_cleanup_out_of_scope_column_items(@dynamic_column_items_out) if @dynamic_column_items_out
  dynamic_cleanup_in_scope_column_items(@dynamic_column_items_in) if @dynamic_column_items_in
end

#dynamic_update_matrix_column_items?Boolean

Returns:

  • (Boolean)


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

def dynamic_update_matrix_column_items?
  dynamic_column_items_out && dynamic_column_items_in != dynamic_column_items_out
end

#dynamic_update_matrix_row_items?Boolean

Shared code

Returns:

  • (Boolean)


51
52
53
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 51

def dynamic_update_matrix_row_items?
  dynamic_row_items_out && dynamic_row_items_in != dynamic_row_items_out
end

#in_scope_observation_matrix_column_itemsObject



41
42
43
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 41

def in_scope_observation_matrix_column_items
  []
end

#in_scope_observation_matrix_row_itemsObject

“Interface” These methods must be implemented in all classes that include this code



33
34
35
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 33

def in_scope_observation_matrix_row_items
  []
end

#out_of_scope_observation_matrix_column_itemsObject



45
46
47
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 45

def out_of_scope_observation_matrix_column_items
  []
end

#out_of_scope_observation_matrix_row_itemsObject



37
38
39
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 37

def out_of_scope_observation_matrix_row_items
  []
end

#prepare_matrix_itemsObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/models/concerns/shared/matrix_hooks/dynamic.rb', line 152

def prepare_matrix_items
  # Rows 
     
  @dynamic_row_items_in = in_scope_observation_matrix_row_items
  @dynamic_row_items_out = out_of_scope_observation_matrix_row_items

  # After save our out maps don't capture items that *were* there, 
  # so we cache those records that need to be popped off the matrix row stack
  @dynamic_row_out_map = {}
  @dynamic_row_items_out.each do |mri|
    @dynamic_row_out_map[mri.id] = mri.row_objects
  end

  # Columns 
 
  @dynamic_column_items_in = in_scope_observation_matrix_column_items
  @dynamic_column_items_out = out_of_scope_observation_matrix_column_items
  
  # After save our out maps don't capture items that *were* there, 
  # so we cache those records that need to be popped off the matrix column stack
  @dynamic_column_out_map = {}
  @dynamic_column_items_out.each do |mci|
    @dynamic_column_out_map[mci.id] = mci.column_objects
  end
end