Sha256: 276275f52f127d76add68db1c7abf7435c3ea1c1b636c309eea593b29bf7aa36

Contents?: true

Size: 1.49 KB

Versions: 7

Compression:

Stored size: 1.49 KB

Contents

module Tako
  module ActiveRecordExt
    module Base
      module ClassMethods
        def shard(shard_name)
          if block_given?
            Tako.shard(shard_name) do
              yield
            end
          else
            Tako::QueryChain.new(
              Tako::Repository.create_proxy(shard_name),
              self
            )
          end
        end

        def force_shard(shard_name)
          @force_shard = shard_name
        end

        def force_shard?
          @force_shard.present?
        end
      end

      module InstanceMethods
        attr_accessor :current_shard

        def self.included(mod)
          mod.extend(ShardedMethods)
          mod.sharded_methods :update_attribute,
                              :update_attributes,
                              :update_attributes!,
                              :reload,
                              :delete,
                              :destroy,
                              :touch,
                              :update_column,
                              :save,
                              :save!

          mod.class_eval do
            after_initialize :set_current_shard

            private

              def set_current_shard
                @current_shard = ::Tako::ProxyStack.top.try(:shard_name)
              end
          end
        end
      end
    end
  end
end

ActiveRecord::Base.class_eval do
  extend Tako::ActiveRecordExt::Base::ClassMethods
  include Tako::ActiveRecordExt::Base::InstanceMethods
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tako-0.6.0 lib/tako/active_record_ext/base.rb
tako-0.5.0 lib/tako/active_record_ext/base.rb
tako-0.4.1 lib/tako/active_record_ext/base.rb
tako-0.4.0 lib/tako/active_record_ext/base.rb
tako-0.3.2 lib/tako/active_record_ext/base.rb
tako-0.3.1 lib/tako/active_record_ext/base.rb
tako-0.3.0 lib/tako/active_record_ext/base.rb