Sha256: 9a54490b899d38ddfa0e96bbd680f3acff04fae3d8a22d0902dbceb1d4b06607

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module Switchman
  module ActiveRecord
    module FinderMethods
      # find_one uses binds, so we can't depend on QueryMethods
      # catching it
      def find_one(id)
        local_id, shard = Shard.local_id_for(id)

        return super(local_id) if shard_source_value != :implicit

        if shard
          begin
            old_shard_value = shard_value
            self.shard_value = shard
            super(local_id)
          ensure
            self.shard_value = old_shard_value
          end
        else
          super
        end
      end

      def find_or_instantiator_by_attributes(match, attributes, *args)
        primary_shard.activate { super }
      end

      def exists?(id = false)
        id = id.id if ActiveRecord::Base === id
        return false if id.nil?

        join_dependency = construct_join_dependency_for_association_find
        relation = construct_relation_for_association_find(join_dependency)
        relation = relation.except(:select, :order).select("1 AS one").limit(1)

        case id
          when Array, Hash
            relation = relation.where(id)
          else
            relation = relation.where(table[primary_key].eq(id)) if id
        end

        activate { return true if connection.select_value(relation, "#{name} Exists") }
        false
      rescue ::ActiveRecord::ThrowResult
        false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
switchman-1.2.0 lib/switchman/active_record/finder_methods.rb