Module: Switchman::ActiveRecord::FinderMethods

Defined in:
lib/switchman/active_record/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#exists?(conditions = :none) ⇒ Boolean



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/switchman/active_record/finder_methods.rb', line 45

def exists?(conditions = :none)
  return false if @none

  if Base === conditions
    raise ArgumentError, "      You are passing an instance of ActiveRecord::Base to `exists?`.\n      Please pass the id of the object by calling `.id`.\n    TEXT\n  end\n\n  return false if !conditions || limit_value == 0 # rubocop:disable Style/NumericPredicate\n\n  if eager_loading?\n    relation = apply_join_dependency(eager_loading: false)\n    return relation.exists?(conditions)\n  end\n\n  relation = construct_relation_for_exists(conditions)\n  return false if relation.where_clause.contradiction?\n\n  relation.activate do |shard_rel|\n    return true if skip_query_cache_if_necessary do\n      connection.select_rows(shard_rel.arel, \"\#{name} Exists?\").size == 1\n    end\n  end\n  false\nend\n".squish

#find_one(id) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/switchman/active_record/finder_methods.rb', line 6

def find_one(id)
  return super unless klass.integral_id?

  if shard_source_value != :implicit
    current_shard = Shard.current(klass.connection_class_for_self)
    result = activate do |relation, shard|
      current_id = Shard.relative_id_for(id, current_shard, shard)
      # current_id will be nil for non-integral id
      next unless current_id
      # skip the shard if the object can't be on it. unless we're only looking at one shard;
      # we might be expecting a shadow object
      next if current_id > Shard::IDS_PER_SHARD && all_shards.length > 1

      relation.call_super(:find_one, FinderMethods, current_id)
    end
    result = result.first if result.is_a?(Array)
    # we may have skipped all shards
    raise_record_not_found_exception!(id, 0, 1) unless result
    return result
  end

  local_id, shard = Shard.local_id_for(id)
  if shard
    shard.activate { super(local_id) }
  else
    super
  end
end

#find_or_instantiator_by_attributes(match, attributes, *args) ⇒ Object



41
42
43
# File 'lib/switchman/active_record/finder_methods.rb', line 41

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

#find_some_ordered(ids) ⇒ Object



35
36
37
38
39
# File 'lib/switchman/active_record/finder_methods.rb', line 35

def find_some_ordered(ids)
  current_shard = Shard.current(klass.connection_class_for_self)
  ids = ids.map { |id| Shard.relative_id_for(id, current_shard, current_shard) }
  super
end