Class: ObservationMatrixColumnItem

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::Identifiers, Shared::IsData, Shared::Notes, Shared::Tags
Defined in:
app/models/observation_matrix_column_item.rb

Overview

Each ObservationMatrixColumnItem is set containing 1 or more descriptors.

Direct Known Subclasses

SingleDescriptor, TaggedDescriptor

Defined Under Namespace

Classes: SingleDescriptor, TaggedDescriptor

Constant Summary

ALL_STI_ATTRIBUTES =
[:descriptor_id, :controlled_vocabulary_term_id].freeze

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods included from ActiverecordUtilities

#trim_attributes

Instance Attribute Details

- (Integer) controlled_vocabulary_term_id

Returns id of the Keyword (a dynamic subclass)

Returns:

  • (Integer)

    id of the Keyword (a dynamic subclass)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
109
110
111
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
139
140
141
142
143
144
145
146
147
148
149
150
151
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/observation_matrix_column_item.rb', line 18

class ObservationMatrixColumnItem < ApplicationRecord
  include Housekeeping
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags

  acts_as_list scope: [:observation_matrix_id, :project_id]

  ALL_STI_ATTRIBUTES = [:descriptor_id, :controlled_vocabulary_term_id].freeze

  belongs_to :observation_matrix, inverse_of: :observation_matrix_column_items

  # TODO: remove from subclasses
  belongs_to :descriptor, inverse_of: :observation_matrix_column_items
  belongs_to :controlled_vocabulary_term

  #  belongs_to :controlled_vocabulary_term (belongs elsewhere)

  validates_presence_of :observation_matrix
  validate :type_is_subclassed
  validate :other_subclass_attributes_not_set

  after_save :update_matrix_columns
  after_destroy :cleanup_matrix_columns

  def cleanup_matrix_columns
    ObservationMatrixColumn.where(descriptor_id: descriptors.map(&:id), observation_matrix: observation_matrix).each do |mc|
      cleanup_single_matrix_column(mc.descriptor_id, mc)
    end
    true
  end

  def update_matrix_columns
    descriptors.each do |d|
      update_single_matrix_column(d)
    end
  end

  def cleanup_single_matrix_column(descriptor_id, mc = nil)
    mc ||= ObservationMatrixColumn.where(
      descriptor_id: descriptor_id, 
      observation_matrix: observation_matrix
    ).first

    decrement_matrix_column_reference_count(mc)
  end

  def decrement_matrix_column_reference_count(mc)
    current = mc.reference_count - 1
    if current == 0
      mc.delete
    else
      mc.update_columns(reference_count: current)
      mr.update_columns(cached_observation_matrix_column_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged
    end
  end

  def update_single_matrix_column(descriptor)
    mc = ObservationMatrixColumn.find_or_create_by(
      observation_matrix: observation_matrix, 
      descriptor: descriptor)
    increment_matrix_column_reference_count(mc)
  end

  def increment_matrix_column_reference_count(mc)
    mc.update_columns(reference_count: (mc.reference_count || 0) + 1)
    mc.update_columns(cached_observation_matrix_column_item_id: id)  if type =~ /Single/
  end 

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

  # @return [Array]
  #    the required attributes for this subclass
  # override
  def self.subclass_attributes
    []
  end

  # @return [Array]
  #    the descriptors "defined" by this matrix column item
  # override
  def descriptors
    false
  end

  # @return [Array] of ObservationMatrixColumnItems
  def self.batch_create(params)
    case params[:batch_type]
    when 'tags'
      batch_create_from_tags(params[:keyword_id], params[:klass], params[:observation_matrix_id])
    when 'pinboard'
      batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass])
    end
  end

  # @params klass [String] the subclass of the Descriptor ike `Descriptor::Working` or `Descriptor::Continuous`
  # @return [Array, false]
  def self.batch_create_from_tags(keyword_id, klass, observation_matrix_id)
    created = []
    ObservationMatrixColumnItem.transaction do
      begin
        if klass
          klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id } ).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_tags(
            Tag.where(keyword_id: keyword_id, tag_object_type: 'Descriptor').all,
            observation_matrix_id
          )       
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @params klass [String] the class name like `Otu` or `CollectionObject`
  # @return [Array, false]
  def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass)
    return false if observation_matrix_id.blank? || project_id.blank? || user_id.blank?
    created = []
    ObservationMatrixColumn.transaction do
      begin
        if klass
          klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id}).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_pinboard_items(
            PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: 'Descriptor').all,
            observation_matrix_id
          )
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @return [Boolean]
  #   whether this is a dynamic or fixed class
  #   override in subclasses
  def is_dynamic?
    false
  end

  private

  # @return [Array]
  def self.create_for_tags(tag_scope, observation_matrix_id)
    a = []
    tag_scope.each do |o|
      a.push create_for(o.tag_object, observation_matrix_id)
    end
    a
  end

  # @param pinboard_item_scope [PinboardItem Scope]
  # @return [Array]
  #   create observation matrix row items for all scope items
  def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id)
    a = []
    pinboard_item_scope.each do |o|
      a.push create_for(o.pinned_object, observation_matrix_id)
    end
    a 
  end

  def self.create_for(object, observation_matrix_id)
    ObservationMatrixColumnItem.create!(
      observation_matrix_id: observation_matrix_id,
      descriptor_id: object.id,
      type: 'ObservationMatrixColumnItem::SingleDescriptor'
    )
  end

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

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

end

- (Integer) descriptor_id

Returns id of the descriptor (a single/static subclass)

Returns:

  • (Integer)

    id of the descriptor (a single/static subclass)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
109
110
111
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
139
140
141
142
143
144
145
146
147
148
149
150
151
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/observation_matrix_column_item.rb', line 18

class ObservationMatrixColumnItem < ApplicationRecord
  include Housekeeping
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags

  acts_as_list scope: [:observation_matrix_id, :project_id]

  ALL_STI_ATTRIBUTES = [:descriptor_id, :controlled_vocabulary_term_id].freeze

  belongs_to :observation_matrix, inverse_of: :observation_matrix_column_items

  # TODO: remove from subclasses
  belongs_to :descriptor, inverse_of: :observation_matrix_column_items
  belongs_to :controlled_vocabulary_term

  #  belongs_to :controlled_vocabulary_term (belongs elsewhere)

  validates_presence_of :observation_matrix
  validate :type_is_subclassed
  validate :other_subclass_attributes_not_set

  after_save :update_matrix_columns
  after_destroy :cleanup_matrix_columns

  def cleanup_matrix_columns
    ObservationMatrixColumn.where(descriptor_id: descriptors.map(&:id), observation_matrix: observation_matrix).each do |mc|
      cleanup_single_matrix_column(mc.descriptor_id, mc)
    end
    true
  end

  def update_matrix_columns
    descriptors.each do |d|
      update_single_matrix_column(d)
    end
  end

  def cleanup_single_matrix_column(descriptor_id, mc = nil)
    mc ||= ObservationMatrixColumn.where(
      descriptor_id: descriptor_id, 
      observation_matrix: observation_matrix
    ).first

    decrement_matrix_column_reference_count(mc)
  end

  def decrement_matrix_column_reference_count(mc)
    current = mc.reference_count - 1
    if current == 0
      mc.delete
    else
      mc.update_columns(reference_count: current)
      mr.update_columns(cached_observation_matrix_column_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged
    end
  end

  def update_single_matrix_column(descriptor)
    mc = ObservationMatrixColumn.find_or_create_by(
      observation_matrix: observation_matrix, 
      descriptor: descriptor)
    increment_matrix_column_reference_count(mc)
  end

  def increment_matrix_column_reference_count(mc)
    mc.update_columns(reference_count: (mc.reference_count || 0) + 1)
    mc.update_columns(cached_observation_matrix_column_item_id: id)  if type =~ /Single/
  end 

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

  # @return [Array]
  #    the required attributes for this subclass
  # override
  def self.subclass_attributes
    []
  end

  # @return [Array]
  #    the descriptors "defined" by this matrix column item
  # override
  def descriptors
    false
  end

  # @return [Array] of ObservationMatrixColumnItems
  def self.batch_create(params)
    case params[:batch_type]
    when 'tags'
      batch_create_from_tags(params[:keyword_id], params[:klass], params[:observation_matrix_id])
    when 'pinboard'
      batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass])
    end
  end

  # @params klass [String] the subclass of the Descriptor ike `Descriptor::Working` or `Descriptor::Continuous`
  # @return [Array, false]
  def self.batch_create_from_tags(keyword_id, klass, observation_matrix_id)
    created = []
    ObservationMatrixColumnItem.transaction do
      begin
        if klass
          klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id } ).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_tags(
            Tag.where(keyword_id: keyword_id, tag_object_type: 'Descriptor').all,
            observation_matrix_id
          )       
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @params klass [String] the class name like `Otu` or `CollectionObject`
  # @return [Array, false]
  def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass)
    return false if observation_matrix_id.blank? || project_id.blank? || user_id.blank?
    created = []
    ObservationMatrixColumn.transaction do
      begin
        if klass
          klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id}).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_pinboard_items(
            PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: 'Descriptor').all,
            observation_matrix_id
          )
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @return [Boolean]
  #   whether this is a dynamic or fixed class
  #   override in subclasses
  def is_dynamic?
    false
  end

  private

  # @return [Array]
  def self.create_for_tags(tag_scope, observation_matrix_id)
    a = []
    tag_scope.each do |o|
      a.push create_for(o.tag_object, observation_matrix_id)
    end
    a
  end

  # @param pinboard_item_scope [PinboardItem Scope]
  # @return [Array]
  #   create observation matrix row items for all scope items
  def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id)
    a = []
    pinboard_item_scope.each do |o|
      a.push create_for(o.pinned_object, observation_matrix_id)
    end
    a 
  end

  def self.create_for(object, observation_matrix_id)
    ObservationMatrixColumnItem.create!(
      observation_matrix_id: observation_matrix_id,
      descriptor_id: object.id,
      type: 'ObservationMatrixColumnItem::SingleDescriptor'
    )
  end

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

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

end

- (Integer) observation_matrix_id

Returns id of the matrix

Returns:

  • (Integer)

    id of the matrix



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
109
110
111
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
139
140
141
142
143
144
145
146
147
148
149
150
151
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/observation_matrix_column_item.rb', line 18

class ObservationMatrixColumnItem < ApplicationRecord
  include Housekeeping
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags

  acts_as_list scope: [:observation_matrix_id, :project_id]

  ALL_STI_ATTRIBUTES = [:descriptor_id, :controlled_vocabulary_term_id].freeze

  belongs_to :observation_matrix, inverse_of: :observation_matrix_column_items

  # TODO: remove from subclasses
  belongs_to :descriptor, inverse_of: :observation_matrix_column_items
  belongs_to :controlled_vocabulary_term

  #  belongs_to :controlled_vocabulary_term (belongs elsewhere)

  validates_presence_of :observation_matrix
  validate :type_is_subclassed
  validate :other_subclass_attributes_not_set

  after_save :update_matrix_columns
  after_destroy :cleanup_matrix_columns

  def cleanup_matrix_columns
    ObservationMatrixColumn.where(descriptor_id: descriptors.map(&:id), observation_matrix: observation_matrix).each do |mc|
      cleanup_single_matrix_column(mc.descriptor_id, mc)
    end
    true
  end

  def update_matrix_columns
    descriptors.each do |d|
      update_single_matrix_column(d)
    end
  end

  def cleanup_single_matrix_column(descriptor_id, mc = nil)
    mc ||= ObservationMatrixColumn.where(
      descriptor_id: descriptor_id, 
      observation_matrix: observation_matrix
    ).first

    decrement_matrix_column_reference_count(mc)
  end

  def decrement_matrix_column_reference_count(mc)
    current = mc.reference_count - 1
    if current == 0
      mc.delete
    else
      mc.update_columns(reference_count: current)
      mr.update_columns(cached_observation_matrix_column_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged
    end
  end

  def update_single_matrix_column(descriptor)
    mc = ObservationMatrixColumn.find_or_create_by(
      observation_matrix: observation_matrix, 
      descriptor: descriptor)
    increment_matrix_column_reference_count(mc)
  end

  def increment_matrix_column_reference_count(mc)
    mc.update_columns(reference_count: (mc.reference_count || 0) + 1)
    mc.update_columns(cached_observation_matrix_column_item_id: id)  if type =~ /Single/
  end 

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

  # @return [Array]
  #    the required attributes for this subclass
  # override
  def self.subclass_attributes
    []
  end

  # @return [Array]
  #    the descriptors "defined" by this matrix column item
  # override
  def descriptors
    false
  end

  # @return [Array] of ObservationMatrixColumnItems
  def self.batch_create(params)
    case params[:batch_type]
    when 'tags'
      batch_create_from_tags(params[:keyword_id], params[:klass], params[:observation_matrix_id])
    when 'pinboard'
      batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass])
    end
  end

  # @params klass [String] the subclass of the Descriptor ike `Descriptor::Working` or `Descriptor::Continuous`
  # @return [Array, false]
  def self.batch_create_from_tags(keyword_id, klass, observation_matrix_id)
    created = []
    ObservationMatrixColumnItem.transaction do
      begin
        if klass
          klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id } ).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_tags(
            Tag.where(keyword_id: keyword_id, tag_object_type: 'Descriptor').all,
            observation_matrix_id
          )       
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @params klass [String] the class name like `Otu` or `CollectionObject`
  # @return [Array, false]
  def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass)
    return false if observation_matrix_id.blank? || project_id.blank? || user_id.blank?
    created = []
    ObservationMatrixColumn.transaction do
      begin
        if klass
          klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id}).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_pinboard_items(
            PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: 'Descriptor').all,
            observation_matrix_id
          )
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @return [Boolean]
  #   whether this is a dynamic or fixed class
  #   override in subclasses
  def is_dynamic?
    false
  end

  private

  # @return [Array]
  def self.create_for_tags(tag_scope, observation_matrix_id)
    a = []
    tag_scope.each do |o|
      a.push create_for(o.tag_object, observation_matrix_id)
    end
    a
  end

  # @param pinboard_item_scope [PinboardItem Scope]
  # @return [Array]
  #   create observation matrix row items for all scope items
  def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id)
    a = []
    pinboard_item_scope.each do |o|
      a.push create_for(o.pinned_object, observation_matrix_id)
    end
    a 
  end

  def self.create_for(object, observation_matrix_id)
    ObservationMatrixColumnItem.create!(
      observation_matrix_id: observation_matrix_id,
      descriptor_id: object.id,
      type: 'ObservationMatrixColumnItem::SingleDescriptor'
    )
  end

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

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

end

- (Integer) position

Returns a sort order

Returns:

  • (Integer)

    a sort order



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
109
110
111
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
139
140
141
142
143
144
145
146
147
148
149
150
151
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/observation_matrix_column_item.rb', line 18

class ObservationMatrixColumnItem < ApplicationRecord
  include Housekeeping
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags

  acts_as_list scope: [:observation_matrix_id, :project_id]

  ALL_STI_ATTRIBUTES = [:descriptor_id, :controlled_vocabulary_term_id].freeze

  belongs_to :observation_matrix, inverse_of: :observation_matrix_column_items

  # TODO: remove from subclasses
  belongs_to :descriptor, inverse_of: :observation_matrix_column_items
  belongs_to :controlled_vocabulary_term

  #  belongs_to :controlled_vocabulary_term (belongs elsewhere)

  validates_presence_of :observation_matrix
  validate :type_is_subclassed
  validate :other_subclass_attributes_not_set

  after_save :update_matrix_columns
  after_destroy :cleanup_matrix_columns

  def cleanup_matrix_columns
    ObservationMatrixColumn.where(descriptor_id: descriptors.map(&:id), observation_matrix: observation_matrix).each do |mc|
      cleanup_single_matrix_column(mc.descriptor_id, mc)
    end
    true
  end

  def update_matrix_columns
    descriptors.each do |d|
      update_single_matrix_column(d)
    end
  end

  def cleanup_single_matrix_column(descriptor_id, mc = nil)
    mc ||= ObservationMatrixColumn.where(
      descriptor_id: descriptor_id, 
      observation_matrix: observation_matrix
    ).first

    decrement_matrix_column_reference_count(mc)
  end

  def decrement_matrix_column_reference_count(mc)
    current = mc.reference_count - 1
    if current == 0
      mc.delete
    else
      mc.update_columns(reference_count: current)
      mr.update_columns(cached_observation_matrix_column_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged
    end
  end

  def update_single_matrix_column(descriptor)
    mc = ObservationMatrixColumn.find_or_create_by(
      observation_matrix: observation_matrix, 
      descriptor: descriptor)
    increment_matrix_column_reference_count(mc)
  end

  def increment_matrix_column_reference_count(mc)
    mc.update_columns(reference_count: (mc.reference_count || 0) + 1)
    mc.update_columns(cached_observation_matrix_column_item_id: id)  if type =~ /Single/
  end 

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

  # @return [Array]
  #    the required attributes for this subclass
  # override
  def self.subclass_attributes
    []
  end

  # @return [Array]
  #    the descriptors "defined" by this matrix column item
  # override
  def descriptors
    false
  end

  # @return [Array] of ObservationMatrixColumnItems
  def self.batch_create(params)
    case params[:batch_type]
    when 'tags'
      batch_create_from_tags(params[:keyword_id], params[:klass], params[:observation_matrix_id])
    when 'pinboard'
      batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass])
    end
  end

  # @params klass [String] the subclass of the Descriptor ike `Descriptor::Working` or `Descriptor::Continuous`
  # @return [Array, false]
  def self.batch_create_from_tags(keyword_id, klass, observation_matrix_id)
    created = []
    ObservationMatrixColumnItem.transaction do
      begin
        if klass
          klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id } ).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_tags(
            Tag.where(keyword_id: keyword_id, tag_object_type: 'Descriptor').all,
            observation_matrix_id
          )       
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @params klass [String] the class name like `Otu` or `CollectionObject`
  # @return [Array, false]
  def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass)
    return false if observation_matrix_id.blank? || project_id.blank? || user_id.blank?
    created = []
    ObservationMatrixColumn.transaction do
      begin
        if klass
          klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id}).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_pinboard_items(
            PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: 'Descriptor').all,
            observation_matrix_id
          )
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @return [Boolean]
  #   whether this is a dynamic or fixed class
  #   override in subclasses
  def is_dynamic?
    false
  end

  private

  # @return [Array]
  def self.create_for_tags(tag_scope, observation_matrix_id)
    a = []
    tag_scope.each do |o|
      a.push create_for(o.tag_object, observation_matrix_id)
    end
    a
  end

  # @param pinboard_item_scope [PinboardItem Scope]
  # @return [Array]
  #   create observation matrix row items for all scope items
  def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id)
    a = []
    pinboard_item_scope.each do |o|
      a.push create_for(o.pinned_object, observation_matrix_id)
    end
    a 
  end

  def self.create_for(object, observation_matrix_id)
    ObservationMatrixColumnItem.create!(
      observation_matrix_id: observation_matrix_id,
      descriptor_id: object.id,
      type: 'ObservationMatrixColumnItem::SingleDescriptor'
    )
  end

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

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

end

- (String) type

Returns the column type

Returns:

  • (String)

    the column type



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
109
110
111
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
139
140
141
142
143
144
145
146
147
148
149
150
151
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/observation_matrix_column_item.rb', line 18

class ObservationMatrixColumnItem < ApplicationRecord
  include Housekeeping
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags

  acts_as_list scope: [:observation_matrix_id, :project_id]

  ALL_STI_ATTRIBUTES = [:descriptor_id, :controlled_vocabulary_term_id].freeze

  belongs_to :observation_matrix, inverse_of: :observation_matrix_column_items

  # TODO: remove from subclasses
  belongs_to :descriptor, inverse_of: :observation_matrix_column_items
  belongs_to :controlled_vocabulary_term

  #  belongs_to :controlled_vocabulary_term (belongs elsewhere)

  validates_presence_of :observation_matrix
  validate :type_is_subclassed
  validate :other_subclass_attributes_not_set

  after_save :update_matrix_columns
  after_destroy :cleanup_matrix_columns

  def cleanup_matrix_columns
    ObservationMatrixColumn.where(descriptor_id: descriptors.map(&:id), observation_matrix: observation_matrix).each do |mc|
      cleanup_single_matrix_column(mc.descriptor_id, mc)
    end
    true
  end

  def update_matrix_columns
    descriptors.each do |d|
      update_single_matrix_column(d)
    end
  end

  def cleanup_single_matrix_column(descriptor_id, mc = nil)
    mc ||= ObservationMatrixColumn.where(
      descriptor_id: descriptor_id, 
      observation_matrix: observation_matrix
    ).first

    decrement_matrix_column_reference_count(mc)
  end

  def decrement_matrix_column_reference_count(mc)
    current = mc.reference_count - 1
    if current == 0
      mc.delete
    else
      mc.update_columns(reference_count: current)
      mr.update_columns(cached_observation_matrix_column_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged
    end
  end

  def update_single_matrix_column(descriptor)
    mc = ObservationMatrixColumn.find_or_create_by(
      observation_matrix: observation_matrix, 
      descriptor: descriptor)
    increment_matrix_column_reference_count(mc)
  end

  def increment_matrix_column_reference_count(mc)
    mc.update_columns(reference_count: (mc.reference_count || 0) + 1)
    mc.update_columns(cached_observation_matrix_column_item_id: id)  if type =~ /Single/
  end 

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

  # @return [Array]
  #    the required attributes for this subclass
  # override
  def self.subclass_attributes
    []
  end

  # @return [Array]
  #    the descriptors "defined" by this matrix column item
  # override
  def descriptors
    false
  end

  # @return [Array] of ObservationMatrixColumnItems
  def self.batch_create(params)
    case params[:batch_type]
    when 'tags'
      batch_create_from_tags(params[:keyword_id], params[:klass], params[:observation_matrix_id])
    when 'pinboard'
      batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass])
    end
  end

  # @params klass [String] the subclass of the Descriptor ike `Descriptor::Working` or `Descriptor::Continuous`
  # @return [Array, false]
  def self.batch_create_from_tags(keyword_id, klass, observation_matrix_id)
    created = []
    ObservationMatrixColumnItem.transaction do
      begin
        if klass
          klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id } ).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_tags(
            Tag.where(keyword_id: keyword_id, tag_object_type: 'Descriptor').all,
            observation_matrix_id
          )       
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @params klass [String] the class name like `Otu` or `CollectionObject`
  # @return [Array, false]
  def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass)
    return false if observation_matrix_id.blank? || project_id.blank? || user_id.blank?
    created = []
    ObservationMatrixColumn.transaction do
      begin
        if klass
          klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id}).each do |o|
            created.push create_for(o, observation_matrix_id)
          end
        else
          created += create_for_pinboard_items(
            PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: 'Descriptor').all,
            observation_matrix_id
          )
        end
      rescue ActiveRecord::RecordInvalid => e
        return false
      end
    end
    return created
  end

  # @return [Boolean]
  #   whether this is a dynamic or fixed class
  #   override in subclasses
  def is_dynamic?
    false
  end

  private

  # @return [Array]
  def self.create_for_tags(tag_scope, observation_matrix_id)
    a = []
    tag_scope.each do |o|
      a.push create_for(o.tag_object, observation_matrix_id)
    end
    a
  end

  # @param pinboard_item_scope [PinboardItem Scope]
  # @return [Array]
  #   create observation matrix row items for all scope items
  def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id)
    a = []
    pinboard_item_scope.each do |o|
      a.push create_for(o.pinned_object, observation_matrix_id)
    end
    a 
  end

  def self.create_for(object, observation_matrix_id)
    ObservationMatrixColumnItem.create!(
      observation_matrix_id: observation_matrix_id,
      descriptor_id: object.id,
      type: 'ObservationMatrixColumnItem::SingleDescriptor'
    )
  end

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

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

end

Class Method Details

+ (Array) batch_create(params)

Returns of ObservationMatrixColumnItems

Returns:

  • (Array)

    of ObservationMatrixColumnItems



107
108
109
110
111
112
113
114
# File 'app/models/observation_matrix_column_item.rb', line 107

def self.batch_create(params)
  case params[:batch_type]
  when 'tags'
    batch_create_from_tags(params[:keyword_id], params[:klass], params[:observation_matrix_id])
  when 'pinboard'
    batch_create_from_pinboard(params[:observation_matrix_id], params[:project_id], params[:user_id], params[:klass])
  end
end

+ (Array, false) batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass)

Returns:

  • (Array, false)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/observation_matrix_column_item.rb', line 141

def self.batch_create_from_pinboard(observation_matrix_id, project_id, user_id, klass)
  return false if observation_matrix_id.blank? || project_id.blank? || user_id.blank?
  created = []
  ObservationMatrixColumn.transaction do
    begin
      if klass
        klass.constantize.joins(:pinboard_items).where(pinboard_items: {user_id: user_id, project_id: project_id}).each do |o|
          created.push create_for(o, observation_matrix_id)
        end
      else
        created += create_for_pinboard_items(
          PinboardItem.where(project_id: project_id, user_id: user_id, pinned_object_type: 'Descriptor').all,
          observation_matrix_id
        )
      end
    rescue ActiveRecord::RecordInvalid => e
      return false
    end
  end
  return created
end

+ (Array, false) batch_create_from_tags(keyword_id, klass, observation_matrix_id)

Returns:

  • (Array, false)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/observation_matrix_column_item.rb', line 118

def self.batch_create_from_tags(keyword_id, klass, observation_matrix_id)
  created = []
  ObservationMatrixColumnItem.transaction do
    begin
      if klass
        klass.constantize.joins(:tags).where(tags: {keyword_id: keyword_id } ).each do |o|
          created.push create_for(o, observation_matrix_id)
        end
      else
        created += create_for_tags(
          Tag.where(keyword_id: keyword_id, tag_object_type: 'Descriptor').all,
          observation_matrix_id
        )       
      end
    rescue ActiveRecord::RecordInvalid => e
      return false
    end
  end
  return created
end

+ (Object) create_for(object, observation_matrix_id) (private)



192
193
194
195
196
197
198
# File 'app/models/observation_matrix_column_item.rb', line 192

def self.create_for(object, observation_matrix_id)
  ObservationMatrixColumnItem.create!(
    observation_matrix_id: observation_matrix_id,
    descriptor_id: object.id,
    type: 'ObservationMatrixColumnItem::SingleDescriptor'
  )
end

+ (Array) create_for_pinboard_items(pinboard_item_scope, observation_matrix_id) (private)

Returns create observation matrix row items for all scope items

Parameters:

Returns:

  • (Array)

    create observation matrix row items for all scope items



184
185
186
187
188
189
190
# File 'app/models/observation_matrix_column_item.rb', line 184

def self.create_for_pinboard_items(pinboard_item_scope, observation_matrix_id)
  a = []
  pinboard_item_scope.each do |o|
    a.push create_for(o.pinned_object, observation_matrix_id)
  end
  a 
end

+ (Array) create_for_tags(tag_scope, observation_matrix_id) (private)

Returns:

  • (Array)


173
174
175
176
177
178
179
# File 'app/models/observation_matrix_column_item.rb', line 173

def self.create_for_tags(tag_scope, observation_matrix_id)
  a = []
  tag_scope.each do |o|
    a.push create_for(o.tag_object, observation_matrix_id)
  end
  a
end

+ (Object) human_name



88
89
90
# File 'app/models/observation_matrix_column_item.rb', line 88

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

+ (Array) subclass_attributes

override

Returns:

  • (Array)

    the required attributes for this subclass



95
96
97
# File 'app/models/observation_matrix_column_item.rb', line 95

def self.subclass_attributes
  []
end

Instance Method Details

- (Object) cleanup_matrix_columns



44
45
46
47
48
49
# File 'app/models/observation_matrix_column_item.rb', line 44

def cleanup_matrix_columns
  ObservationMatrixColumn.where(descriptor_id: descriptors.map(&:id), observation_matrix: observation_matrix).each do |mc|
    cleanup_single_matrix_column(mc.descriptor_id, mc)
  end
  true
end

- (Object) cleanup_single_matrix_column(descriptor_id, mc = nil)



57
58
59
60
61
62
63
64
# File 'app/models/observation_matrix_column_item.rb', line 57

def cleanup_single_matrix_column(descriptor_id, mc = nil)
  mc ||= ObservationMatrixColumn.where(
    descriptor_id: descriptor_id, 
    observation_matrix: observation_matrix
  ).first

  decrement_matrix_column_reference_count(mc)
end

- (Object) decrement_matrix_column_reference_count(mc)



66
67
68
69
70
71
72
73
74
# File 'app/models/observation_matrix_column_item.rb', line 66

def decrement_matrix_column_reference_count(mc)
  current = mc.reference_count - 1
  if current == 0
    mc.delete
  else
    mc.update_columns(reference_count: current)
    mr.update_columns(cached_observation_matrix_column_item_id: nil) if current == 1 && type =~ /Single/ # we've deleted the only single, so the last must be a Dynamic/Tagged
  end
end

- (Array) descriptors

override

Returns:

  • (Array)

    the descriptors “defined” by this matrix column item



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

def descriptors
  false
end

- (Object) increment_matrix_column_reference_count(mc)



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

def increment_matrix_column_reference_count(mc)
  mc.update_columns(reference_count: (mc.reference_count || 0) + 1)
  mc.update_columns(cached_observation_matrix_column_item_id: id)  if type =~ /Single/
end

- (Boolean) is_dynamic?

Returns whether this is a dynamic or fixed class override in subclasses

Returns:

  • (Boolean)

    whether this is a dynamic or fixed class override in subclasses



166
167
168
# File 'app/models/observation_matrix_column_item.rb', line 166

def is_dynamic?
  false
end

- (Object) other_subclass_attributes_not_set (private)



200
201
202
203
204
# File 'app/models/observation_matrix_column_item.rb', line 200

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

- (Object) type_is_subclassed (private)



206
207
208
209
210
# File 'app/models/observation_matrix_column_item.rb', line 206

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

- (Object) update_matrix_columns



51
52
53
54
55
# File 'app/models/observation_matrix_column_item.rb', line 51

def update_matrix_columns
  descriptors.each do |d|
    update_single_matrix_column(d)
  end
end

- (Object) update_single_matrix_column(descriptor)



76
77
78
79
80
81
# File 'app/models/observation_matrix_column_item.rb', line 76

def update_single_matrix_column(descriptor)
  mc = ObservationMatrixColumn.find_or_create_by(
    observation_matrix: observation_matrix, 
    descriptor: descriptor)
  increment_matrix_column_reference_count(mc)
end