From cdc112e3ea8bd7b5ba787e64f3f8ee3da3e5a64f Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Thu, 4 Feb 2016 08:02:45 +1030 Subject: Defer Arel attribute lookup to the model class This still isn't as separated as I'd like, but it at least moves most of the burden of alias mapping in one place. --- activerecord/lib/active_record/relation/batches.rb | 6 +++--- activerecord/lib/active_record/relation/calculations.rb | 12 ++---------- activerecord/lib/active_record/relation/finder_methods.rb | 4 ++-- .../relation/predicate_builder/relation_handler.rb | 2 +- activerecord/lib/active_record/relation/query_methods.rb | 12 +++++------- 5 files changed, 13 insertions(+), 23 deletions(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index 54587ae18e..af951d4da7 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -204,15 +204,15 @@ module ActiveRecord yield yielded_relation break if ids.length < of - batch_relation = relation.where(table[primary_key].gt(primary_key_offset)) + batch_relation = relation.where(klass.arel_attribute(primary_key, table).gt(primary_key_offset)) end end private def apply_limits(relation, start, finish) - relation = relation.where(table[primary_key].gteq(start)) if start - relation = relation.where(table[primary_key].lteq(finish)) if 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 end diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index f45844a9ea..c02b648cb1 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -155,15 +155,7 @@ module ActiveRecord # See also #ids. # def pluck(*column_names) - column_names.map! do |column_name| - if column_name.is_a?(Symbol) && attribute_alias?(column_name) - attribute_alias(column_name) - else - column_name.to_s - end - end - - if loaded? && (column_names - @klass.column_names).empty? + if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty? return @records.pluck(*column_names) end @@ -172,7 +164,7 @@ module ActiveRecord else relation = spawn relation.select_values = column_names.map { |cn| - columns_hash.key?(cn) ? arel_table[cn] : cn + @klass.has_attribute?(cn) || @klass.attribute_alias?(cn) ? klass.arel_attribute(cn, table) : cn } result = klass.connection.select_all(relation.arel, nil, bound_attributes) result.cast_values(klass.attribute_types) diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 3f5d6de78a..5388c4cd65 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -147,7 +147,7 @@ module ActiveRecord def last(limit = nil) if limit if order_values.empty? && primary_key - order(arel_table[primary_key].desc).limit(limit).reverse + order(klass.arel_attribute(primary_key, table).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(arel_table[primary_key].asc) + order(klass.arel_attribute(primary_key, table).asc) else self end diff --git a/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb index 063150958a..a43478f815 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb @@ -3,7 +3,7 @@ module ActiveRecord class RelationHandler # :nodoc: def call(attribute, value) if value.select_values.empty? - value = value.select(value.klass.arel_table[value.klass.primary_key]) + value = value.select(value.klass.arel_attribute(value.klass.primary_key, value.klass.arel_table)) end attribute.in(value.arel) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 8ef9f9f627..ce03c0902b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1093,8 +1093,8 @@ module ActiveRecord def arel_columns(columns) columns.map do |field| - if (Symbol === field || String === field) && columns_hash.key?(field.to_s) && !from_clause.value - arel_table[field] + if (Symbol === field || String === field) && (klass.has_attribute?(field) || klass.attribute_alias?(field)) && !from_clause.value + klass.arel_attribute(field, table) 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 [table[primary_key].desc] if primary_key + return [klass.arel_attribute(primary_key, table).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,12 +1170,10 @@ module ActiveRecord order_args.map! do |arg| case arg when Symbol - arg = klass.attribute_alias(arg) if klass.attribute_alias?(arg) - table[arg].asc + klass.arel_attribute(arg, table).asc when Hash arg.map { |field, dir| - field = klass.attribute_alias(field) if klass.attribute_alias?(field) - table[field].send(dir.downcase) + klass.arel_attribute(field, table).send(dir.downcase) } else arg -- cgit v1.2.3 From 5952861948a0918b1955202c1ea19589634537dc Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Thu, 4 Feb 2016 09:14:05 +1030 Subject: Extract a Relation#arel_attribute --- activerecord/lib/active_record/relation/batches.rb | 6 +++--- activerecord/lib/active_record/relation/calculations.rb | 2 +- activerecord/lib/active_record/relation/finder_methods.rb | 4 ++-- .../active_record/relation/predicate_builder/relation_handler.rb | 2 +- activerecord/lib/active_record/relation/query_methods.rb | 8 ++++---- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index af951d4da7..de005e2810 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -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 diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index c02b648cb1..54c9af4898 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -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) diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 5388c4cd65..d48bcea28a 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -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 diff --git a/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb index a43478f815..8a910a82fe 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb @@ -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) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index ce03c0902b..91bfa4d131 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -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 -- cgit v1.2.3