Sha256: 582bad2edf6795f4a3190a1783ed324b510f4170c28043d88bfe0fc53428fd74

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 KB

Contents

module DbCharmer
  module ActiveRecord
    module FinderOverrides

      module ClassMethods
        SLAVE_METHODS = [ :find_by_sql, :count_by_sql, :calculate ]
        MASTER_METHODS = [ :update, :create, :delete, :destroy, :delete_all, :destroy_all, :update_all, :update_counters ]

        SLAVE_METHODS.each do |slave_method|
          class_eval <<-EOF, __FILE__, __LINE__ + 1
            def #{slave_method}(*args, &block)
              first_level_on_slave do
                super(*args, &block)
              end
            end
          EOF
        end

        MASTER_METHODS.each do |master_method|
          class_eval <<-EOF, __FILE__, __LINE__ + 1
            def #{master_method}(*args, &block)
              on_master do
                super(*args, &block)
              end
            end
          EOF
        end

        def find(*args, &block)
          options = args.last
          if options.is_a?(Hash) && options[:lock]
            on_master { super(*args, &block) }
          else
            super(*args, &block)
          end
        end

      private

        def first_level_on_slave
          first_level = db_charmer_top_level_connection? && on_master.connection.open_transactions.zero?
          if first_level && db_charmer_force_slave_reads?
            on_slave { yield }
          else
            yield
          end
        end

      end

      module InstanceMethods
        def reload(*args, &block)
          self.class.on_master do
            super(*args, &block)
          end
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
db-charmer-1.7.0.pre6 lib/db_charmer/active_record/finder_overrides.rb
db-charmer-1.7.0.pre5 lib/db_charmer/active_record/finder_overrides.rb
db-charmer-1.7.0.pre4 lib/db_charmer/active_record/finder_overrides.rb
db-charmer-1.7.0.pre3 lib/db_charmer/active_record/finder_overrides.rb
db-charmer-1.7.0.pre2 lib/db_charmer/active_record/finder_overrides.rb
db-charmer-1.7.0.pre1 lib/db_charmer/active_record/finder_overrides.rb