From a60770d3bf3a8aeac16c110f3a7d05a6d52a86d6 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 26 Dec 2014 15:17:10 -0700 Subject: Remove `klass` and `arel_table` as a dependency of `PredicateBuilder` This class cares far too much about the internals of other parts of Active Record. This is an attempt to break out a meaningful object which represents the needs of the predicate builder. I'm not fully satisfied with the name, but the general concept is an object which represents a table, the associations to/from that table, and the types associated with it. Many of these exist at the `ActiveRecord::Base` class level, not as properties of the table itself, hence the need for another object. Currently it provides these by holding a reference to the class, but that will likely change in the future. This allows the predicate builder to remain wholy concerned with building predicates. /cc @mrgilman --- activerecord/lib/active_record/table_metadata.rb | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 activerecord/lib/active_record/table_metadata.rb (limited to 'activerecord/lib/active_record/table_metadata.rb') diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb new file mode 100644 index 0000000000..bf705d3565 --- /dev/null +++ b/activerecord/lib/active_record/table_metadata.rb @@ -0,0 +1,46 @@ +module ActiveRecord + class TableMetadata # :nodoc: + delegate :foreign_type, :foreign_key, to: :association, prefix: true + + def initialize(klass, arel_table, association = nil) + @klass = klass + @arel_table = arel_table + @association = association + end + + def resolve_column_aliases(hash) + hash = hash.dup + hash.keys.grep(Symbol) do |key| + if klass.attribute_alias? key + hash[klass.attribute_alias(key)] = hash.delete key + end + end + hash + end + + def arel_attribute(column_name) + arel_table[column_name] + end + + def associated_with?(association_name) + klass && klass._reflect_on_association(association_name) + end + + def associated_table(table_name) + arel_table = Arel::Table.new(table_name) + association = klass._reflect_on_association(table_name) + if association && !association.polymorphic? + klass = association.klass + end + TableMetadata.new(klass, arel_table, association) + end + + def polymorphic_association? + association && association.polymorphic? + end + + protected + + attr_reader :klass, :arel_table, :association + end +end -- cgit v1.2.3