Sha256: 67b9e9e91272a2913470d473bc38bf6a802f31a9900daeac0f7f629ac34d5c59

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module DbCharmer
  module ActionControllerExtensions
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      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
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
glebpom-db-charmer-1.3.0 lib/db_charmer/action_controller_extensions.rb
glebpom-db-charmer-1.4.3 lib/db_charmer/action_controller_extensions.rb
glebpom-db-charmer-1.4.2 lib/db_charmer/action_controller_extensions.rb
glebpom-db-charmer-1.4.0 lib/db_charmer/action_controller_extensions.rb