Class: OnlineMigrations::BackgroundDataMigrations::PerformActionOnRelation

Inherits:
DataMigration
  • Object
show all
Defined in:
lib/online_migrations/background_data_migrations/perform_action_on_relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DataMigration

#after_cancel, #after_complete, #after_pause, #after_resume, #after_start, #after_stop, #around_process, #build_enumerator, collection_batch_size, #count, named

Constructor Details

#initialize(model_name, conditions, action, options = {}) ⇒ PerformActionOnRelation

Returns a new instance of PerformActionOnRelation.



9
10
11
12
13
14
# File 'lib/online_migrations/background_data_migrations/perform_action_on_relation.rb', line 9

def initialize(model_name, conditions, action, options = {})
  @model = Object.const_get(model_name, false)
  @conditions = conditions
  @action = action.to_sym
  @options = options.symbolize_keys
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



7
8
9
# File 'lib/online_migrations/background_data_migrations/perform_action_on_relation.rb', line 7

def action
  @action
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



7
8
9
# File 'lib/online_migrations/background_data_migrations/perform_action_on_relation.rb', line 7

def conditions
  @conditions
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/online_migrations/background_data_migrations/perform_action_on_relation.rb', line 7

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/online_migrations/background_data_migrations/perform_action_on_relation.rb', line 7

def options
  @options
end

Instance Method Details

#collectionObject



16
17
18
# File 'lib/online_migrations/background_data_migrations/perform_action_on_relation.rb', line 16

def collection
  model.unscoped.where(conditions).in_batches(of: 100)
end

#process(relation) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/online_migrations/background_data_migrations/perform_action_on_relation.rb', line 20

def process(relation)
  case action
  when :update_all
    updates = options.fetch(:updates)
    relation.public_send(action, updates)
  when :delete_all, :destroy_all
    relation.public_send(action)
  else
    relation.each(&action)
  end
end