1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Extract a Relation#arel_attribute

This commit is contained in:
Matthew Draper 2016-02-04 09:14:05 +10:30
parent cdc112e3ea
commit 5952861948
7 changed files with 20 additions and 16 deletions

View file

@ -256,7 +256,7 @@ module ActiveRecord
end
end
def arel_attribute(name, table) # :nodoc:
def arel_attribute(name, table = arel_table) # :nodoc:
name = attribute_alias(name) if attribute_alias?(name)
table[name]
end

View file

@ -47,7 +47,7 @@ module ActiveRecord
if !primary_key_value && connection.prefetch_primary_key?(klass.table_name)
primary_key_value = connection.next_sequence_value(klass.sequence_name)
values[klass.arel_attribute(klass.primary_key, table)] = primary_key_value
values[arel_attribute(klass.primary_key)] = primary_key_value
end
end
@ -105,6 +105,10 @@ module ActiveRecord
[substitutes, binds]
end
def arel_attribute(name) # :nodoc:
klass.arel_attribute(name, table)
end
# Initializes new record from relation while maintaining the current
# scope.
#
@ -373,9 +377,9 @@ module ActiveRecord
stmt.table(table)
if joins_values.any?
@klass.connection.join_to_update(stmt, arel, @klass.arel_attribute(primary_key, table))
@klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key))
else
stmt.key = @klass.arel_attribute(primary_key, table)
stmt.key = arel_attribute(primary_key)
stmt.take(arel.limit)
stmt.order(*arel.orders)
stmt.wheres = arel.constraints
@ -527,7 +531,7 @@ module ActiveRecord
stmt.from(table)
if joins_values.any?
@klass.connection.join_to_delete(stmt, arel, @klass.arel_attribute(primary_key, table))
@klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key))
else
stmt.wheres = arel.constraints
end

View file

@ -204,15 +204,15 @@ module ActiveRecord
yield yielded_relation
break if ids.length < of
batch_relation = relation.where(klass.arel_attribute(primary_key, table).gt(primary_key_offset))
batch_relation = relation.where(arel_attribute(primary_key).gt(primary_key_offset))
end
end
private
def apply_limits(relation, start, finish)
relation = relation.where(klass.arel_attribute(primary_key, table).gteq(start)) if start
relation = relation.where(klass.arel_attribute(primary_key, table).lteq(finish)) if finish
relation = relation.where(arel_attribute(primary_key).gteq(start)) if start
relation = relation.where(arel_attribute(primary_key).lteq(finish)) if finish
relation
end

View file

@ -164,7 +164,7 @@ module ActiveRecord
else
relation = spawn
relation.select_values = column_names.map { |cn|
@klass.has_attribute?(cn) || @klass.attribute_alias?(cn) ? klass.arel_attribute(cn, table) : cn
@klass.has_attribute?(cn) || @klass.attribute_alias?(cn) ? arel_attribute(cn) : cn
}
result = klass.connection.select_all(relation.arel, nil, bound_attributes)
result.cast_values(klass.attribute_types)

View file

@ -147,7 +147,7 @@ module ActiveRecord
def last(limit = nil)
if limit
if order_values.empty? && primary_key
order(klass.arel_attribute(primary_key, table).desc).limit(limit).reverse
order(arel_attribute(primary_key).desc).limit(limit).reverse
else
to_a.last(limit)
end
@ -514,7 +514,7 @@ module ActiveRecord
# TODO: once the offset argument is removed from find_nth,
# find_nth_with_limit_and_offset can be merged into this method
relation = if order_values.empty? && primary_key
order(klass.arel_attribute(primary_key, table).asc)
order(arel_attribute(primary_key).asc)
else
self
end

View file

@ -3,7 +3,7 @@ module ActiveRecord
class RelationHandler # :nodoc:
def call(attribute, value)
if value.select_values.empty?
value = value.select(value.klass.arel_attribute(value.klass.primary_key, value.klass.arel_table))
value = value.select(value.arel_attribute(value.klass.primary_key))
end
attribute.in(value.arel)

View file

@ -1094,7 +1094,7 @@ module ActiveRecord
def arel_columns(columns)
columns.map do |field|
if (Symbol === field || String === field) && (klass.has_attribute?(field) || klass.attribute_alias?(field)) && !from_clause.value
klass.arel_attribute(field, table)
arel_attribute(field)
elsif Symbol === field
connection.quote_table_name(field.to_s)
else
@ -1105,7 +1105,7 @@ module ActiveRecord
def reverse_sql_order(order_query)
if order_query.empty?
return [klass.arel_attribute(primary_key, table).desc] if primary_key
return [arel_attribute(primary_key).desc] if primary_key
raise IrreversibleOrderError,
"Relation has no current order and table has no primary key to be used as default order"
end
@ -1170,10 +1170,10 @@ module ActiveRecord
order_args.map! do |arg|
case arg
when Symbol
klass.arel_attribute(arg, table).asc
arel_attribute(arg).asc
when Hash
arg.map { |field, dir|
klass.arel_attribute(field, table).send(dir.downcase)
arel_attribute(field).send(dir.downcase)
}
else
arg