Module: DbCharmer::ActionControllerExtensions::ClassMethods

Defined in:
lib/db_charmer/action_controller_extensions.rb

Instance Method Summary collapse

Instance Method Details

#force_master_db(*models) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/db_charmer/action_controller_extensions.rb', line 8

def force_master_db(*models)
  models_array = case models
    when String, Symbol
      [models]
    when Array
      models
    when nil
      #use all models
    else
      raise ArgumentError, "You should specify correct list of models"
  end.map { |m| m.to_s.camelize.constantize }

  old_proxies = {}
  append_around_filter do |c,a|
    begin
      models_array.each do |model|
        model.db_charmer_connection_level += 1
        old_proxies[model.object_id] = model.db_charmer_connection_proxy
        model.switch_connection_to(nil, DbCharmer.migration_connections_should_exist?)
      end
      a.call
    ensure
      models_array.each do |model|
        model.switch_connection_to(old_proxies[model.object_id], DbCharmer.migration_connections_should_exist?)
        model.db_charmer_connection_level -= 1
      end
    end
  end
end