Sha256: 0765ab538b0e9c12bc1259b787984a80ed99840759fd50b068b2ce00b54f8a8c

Contents?: true

Size: 1.1 KB

Versions: 8

Compression:

Stored size: 1.1 KB

Contents

module ActiveRecordShards
  module Model
    def not_sharded
      if self == ActiveRecord::Base || self != base_class
        raise "You should only call not_sharded on direct descendants of ActiveRecord::Base"
      end
      @sharded = false
    end

    def is_sharded?
      if self == ActiveRecord::Base
        true
      elsif self == base_class
        @sharded != false
      else
        base_class.is_sharded?
      end
    end

    module InstanceMethods
      def after_initialize_with_slave
        after_initialize_without_slave if respond_to?(:after_initialize_without_slave)
        @shard_selection = self.class.current_shard_selection.options.clone
      end

      def from_slave?
        @shard_selection[:slave]
      end

      def from_shard
        @shard_selection[:shard]
      end
    end

    def self.extended(base)
      base.send(:include, InstanceMethods)

      if base.method_defined?(:after_initialize)
        base.alias_method_chain :after_initialize, :slave
      else
        base.send(:alias_method, :after_initialize, :after_initialize_with_slave)
      end
      
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
active_record_shards-2.4.5 lib/active_record_shards/model.rb
active_record_shards-2.4.4 lib/active_record_shards/model.rb
active_record_shards-2.4.3 lib/active_record_shards/model.rb
active_record_shards-2.4.2 lib/active_record_shards/model.rb
active_record_shards-2.4.1 lib/active_record_shards/model.rb
active_record_shards-2.3.1 lib/active_record_shards/model.rb
active_record_shards-2.3.0 lib/active_record_shards/model.rb
active_record_shards-2.2.0 lib/active_record_shards/model.rb