Method: Switchman::ActiveRecord::QueryMethods#transpose_predicate_value

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

#transpose_predicate_value(value, current_shard, target_shard, attribute_type, remove_non_local_ids) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/switchman/active_record/query_methods.rb', line 331

def transpose_predicate_value(value, current_shard, target_shard, attribute_type, remove_non_local_ids)
  if value.is_a?(::Arel::Nodes::BindParam)
    query_att = value.value
    current_id = query_att.value_before_type_cast
    if current_id.is_a?(::ActiveRecord::StatementCache::Substitute)
      current_id.sharded = true # mark for transposition later
      current_id.primary = true if attribute_type == :primary
      value
    else
      local_id = Shard.relative_id_for(current_id, current_shard, target_shard) || current_id
      local_id = [] if remove_non_local_ids && local_id.is_a?(Integer) && local_id > Shard::IDS_PER_SHARD
      if current_id != local_id
        # make a new bind param
        ::Arel::Nodes::BindParam.new(query_att.class.new(query_att.name, local_id, query_att.type))
      else
        value
      end
    end
  else
    local_id = Shard.relative_id_for(value, current_shard, target_shard) || value
    local_id = [] if remove_non_local_ids && local_id.is_a?(Integer) && local_id > Shard::IDS_PER_SHARD
    local_id
  end
end