Module: Switchman::ActiveRecord::WhereClauseFactory

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#scope=(value) ⇒ Object (writeonly)

Sets the attribute scope

Parameters:

  • value

    the value to set the attribute scope to.



4
5
6
# File 'lib/switchman/active_record/where_clause_factory.rb', line 4

def scope=(value)
  @scope = value
end

Instance Method Details

#build(opts, other = []) ⇒ 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
# File 'lib/switchman/active_record/where_clause_factory.rb', line 6

def build(opts, other = [])
  case opts
  when String, Array
    values = Hash === other.first ? other.first.values : other

    values.grep(ActiveRecord::Relation) do |rel|
      # serialize subqueries against the same shard as the outer query is currently
      # targeted to run against
      if rel.shard_source_value == :implicit && rel.primary_shard != @scope.primary_shard
        rel.shard!(@scope.primary_shard)
      end
    end

    super
  when Hash, ::Arel::Nodes::Node
    where_clause = super
    binds = ::Rails.version >= "5.2" ? nil : where_clause.binds
    predicates = where_clause.send(:predicates)
    @scope.send(:infer_shards_from_primary_key, predicates, binds) if @scope.shard_source_value == :implicit && @scope.shard_value.is_a?(Shard)
    predicates, _new_binds = @scope.transpose_predicates(predicates, nil, @scope.primary_shard, false, binds: binds)
    where_clause.instance_variable_set(:@predicates, predicates)
    where_clause
  else
    super
  end
end