module Octopus::ShardTracking

Public Class Methods

included(base) click to toggle source
# File lib/octopus/shard_tracking.rb, line 3
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

run_on_shard() { || ... } click to toggle source

Adds run_on_shard method, but does not implement current_shard method

# File lib/octopus/shard_tracking.rb, line 29
def run_on_shard(&block)
  if (cs = current_shard)
    r = ActiveRecord::Base.connection_proxy.run_queries_on_shard(cs, &block)
    # Use a case statement to avoid any path through ActiveRecord::Delegation's
    # respond_to? code. We want to avoid the respond_to? code because it can have
    # the side effect of causing a call to load_target

    if (ActiveRecord::Relation === r || ActiveRecord::QueryMethods::WhereChain === r) && !(Octopus::RelationProxy === r)
      Octopus::RelationProxy.new(cs, r)
    else
      r
    end
  else
    yield
  end
end