Class: OnlineMigrations::BackgroundMigrations::PerformActionOnRelation

Inherits:
OnlineMigrations::BackgroundMigration show all
Defined in:
lib/online_migrations/background_migrations/perform_action_on_relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from OnlineMigrations::BackgroundMigration

#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_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_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_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_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_migrations/perform_action_on_relation.rb', line 7

def options
  @options
end

Instance Method Details

#process_batch(relation) ⇒ Object



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

def process_batch(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

#relationObject



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

def relation
  model.unscoped.where(conditions)
end