Sha256: 8d74a48e2414ee68facd71848e1c997aa8fe2b12507b073c57cbc7a6c542e66c

Contents?: true

Size: 1.77 KB

Versions: 18

Compression:

Stored size: 1.77 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?(conditions = :none)
        conditions = conditions.id if ::ActiveRecord::Base === conditions
        return false if !conditions

        if ::Rails.version >= '4.1'
          relation = apply_join_dependency(self, construct_join_dependency)
          return false if ::ActiveRecord::NullRelation === relation
        else
          join_dependency = construct_join_dependency_for_association_find
          relation = construct_relation_for_association_find(join_dependency)
        end

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

        case conditions
        when Array, Hash
          relation = relation.where(conditions)
        else
          relation = relation.where(table[primary_key].eq(conditions)) if conditions != :none
        end

        args = [relation, "#{name} Exists"]
        args << relation.bind_values if ::Rails.version >= '4.1'
        activate { return true if connection.select_value(*args) }
        false
      rescue
        raise if ::Rails.version >= '4.1' || !(::ActiveRecord::ThrowResult === $!)
        false
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
switchman-1.2.19 lib/switchman/active_record/finder_methods.rb
switchman-1.2.18 lib/switchman/active_record/finder_methods.rb
switchman-1.2.17 lib/switchman/active_record/finder_methods.rb
switchman-1.2.16 lib/switchman/active_record/finder_methods.rb
switchman-1.2.15 lib/switchman/active_record/finder_methods.rb
switchman-1.2.14 lib/switchman/active_record/finder_methods.rb
switchman-1.2.13 lib/switchman/active_record/finder_methods.rb
switchman-1.2.12 lib/switchman/active_record/finder_methods.rb
switchman-1.2.11 lib/switchman/active_record/finder_methods.rb
switchman-1.2.10 lib/switchman/active_record/finder_methods.rb
switchman-1.2.9 lib/switchman/active_record/finder_methods.rb
switchman-1.2.8 lib/switchman/active_record/finder_methods.rb
switchman-1.2.7 lib/switchman/active_record/finder_methods.rb
switchman-1.2.5 lib/switchman/active_record/finder_methods.rb
switchman-1.2.4 lib/switchman/active_record/finder_methods.rb
switchman-1.2.3 lib/switchman/active_record/finder_methods.rb
switchman-1.2.2 lib/switchman/active_record/finder_methods.rb
switchman-1.2.1 lib/switchman/active_record/finder_methods.rb