Class: PinFlags::FeatureSubscription::BulkProcessor

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations
Defined in:
app/models/pin_flags/feature_subscription/bulk_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BulkProcessor

Returns a new instance of BulkProcessor.



17
18
19
20
# File 'app/models/pin_flags/feature_subscription/bulk_processor.rb', line 17

def initialize(attributes = {})
  super
  @feature_taggable_ids = Array(attributes[:feature_taggable_ids]) if attributes[:feature_taggable_ids]
end

Instance Attribute Details

#feature_taggable_idsObject

Returns the value of attribute feature_taggable_ids.



9
10
11
# File 'app/models/pin_flags/feature_subscription/bulk_processor.rb', line 9

def feature_taggable_ids
  @feature_taggable_ids
end

Instance Method Details

#processObject



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
# File 'app/models/pin_flags/feature_subscription/bulk_processor.rb', line 22

def process
  return false unless valid?

  # Prepare records for upsert_all
  records = sanitized_feature_taggable_ids.map do |feature_taggable_id|
    {
      feature_tag_id: feature_tag.id,
      feature_taggable_type: feature_taggable_type,
      feature_taggable_id: feature_taggable_id,
      created_at: Time.current,
      updated_at: Time.current
    }
  end

  begin
    # Use upsert_all to insert or update records in bulk
    PinFlags::FeatureSubscription.upsert_all(
      records,
      unique_by: [ :feature_tag_id, :feature_taggable_type, :feature_taggable_id ]
    )
    true
  rescue ActiveRecord::RecordInvalid, ActiveRecord::StatementInvalid => _e
    false
  end
end