Permalink
Browse files

Reorganize adapters and elininate code duplication

  • Loading branch information...
ernie committed Sep 4, 2012
1 parent df12f2e commit 298357b68bbaa2b9786c0927ce1eafeb68ef021b
@@ -3,36 +3,10 @@
ActiveRecord::Relation.send :include, Squeel::Nodes::Aliasing
require 'squeel/adapters/active_record/join_dependency_extensions'
require 'squeel/adapters/active_record/base_extensions'
- ActiveRecord::Base.extend Squeel::Adapters::ActiveRecord::BaseExtensions
- case ActiveRecord::VERSION::MINOR
- when 0
- require 'squeel/adapters/active_record/3.0/compat'
- require 'squeel/adapters/active_record/3.0/relation_extensions'
- require 'squeel/adapters/active_record/3.0/association_preload_extensions'
- require 'squeel/adapters/active_record/3.0/context'
-
- ActiveRecord::Relation.send :include, Squeel::Adapters::ActiveRecord::RelationExtensions
- ActiveRecord::Associations::ClassMethods::JoinDependency.send :include, Squeel::Adapters::ActiveRecord::JoinDependencyExtensions
- ActiveRecord::Base.extend Squeel::Adapters::ActiveRecord::AssociationPreloadExtensions
- when 1
- require 'squeel/adapters/active_record/3.1/compat'
- require 'squeel/adapters/active_record/3.1/relation_extensions'
- require 'squeel/adapters/active_record/3.1/preloader_extensions'
- require 'squeel/adapters/active_record/3.1/context'
-
- ActiveRecord::Relation.send :include, Squeel::Adapters::ActiveRecord::RelationExtensions
- ActiveRecord::Associations::JoinDependency.send :include, Squeel::Adapters::ActiveRecord::JoinDependencyExtensions
- ActiveRecord::Associations::Preloader.send :include, Squeel::Adapters::ActiveRecord::PreloaderExtensions
- else
- require 'squeel/adapters/active_record/compat'
- require 'squeel/adapters/active_record/relation_extensions'
- require 'squeel/adapters/active_record/preloader_extensions'
- require 'squeel/adapters/active_record/context'
-
- ActiveRecord::Relation.send :include, Squeel::Adapters::ActiveRecord::RelationExtensions
- ActiveRecord::Associations::JoinDependency.send :include, Squeel::Adapters::ActiveRecord::JoinDependencyExtensions
- ActiveRecord::Associations::Preloader.send :include, Squeel::Adapters::ActiveRecord::PreloaderExtensions
+ adapter_directory = "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
+ Dir[File.expand_path("../active_record/#{adapter_directory}/*.rb", __FILE__)].each do |f|
+ require f
end
else
raise NotImplementedError, "Squeel does not support ActiveRecord version #{ActiveRecord::VERSION::STRING}"
@@ -11,3 +11,5 @@ def preload_associations(records, associations, preload_options={})
end
end
end
+
+ActiveRecord::Base.extend Squeel::Adapters::ActiveRecord::AssociationPreloadExtensions
@@ -1,73 +1,14 @@
-require 'squeel/context'
+require 'squeel/adapters/active_record/context'
module Squeel
module Adapters
module ActiveRecord
class Context < ::Squeel::Context
- # Because the AR::Associations namespace is insane
- JoinBase = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase
-
- def initialize(object)
- super
- @base = object.join_base
- @engine = @base.arel_engine
- @arel_visitor = Arel::Visitors.visitor_for @engine
- @default_table = Arel::Table.new(@base.table_name, :as => @base.aliased_table_name, :engine => @engine)
- end
-
- def find(object, parent = @base)
- if JoinBase === parent
- case object
- when String, Symbol, Nodes::Stub
- assoc_name = object.to_s
- @object.join_associations.detect { |j|
- j.reflection.name.to_s == assoc_name && j.parent == parent
- }
- when Nodes::Join
- @object.join_associations.detect { |j|
- j.reflection.name == object.name && j.parent == parent &&
- (object.polymorphic? ? j.reflection.klass == object._klass : true)
- }
- else
- @object.join_associations.detect { |j|
- j.reflection == object && j.parent == parent
- }
- end
- end
- end
-
- def traverse(keypath, parent = @base, include_endpoint = false)
- parent = @base if keypath.absolute?
- keypath.path_without_endpoint.each do |key|
- parent = find(key, parent) || key
- end
- parent = find(keypath.endpoint, parent) if include_endpoint
-
- parent
- end
private
- def get_table(object)
- if [Symbol, String, Nodes::Stub].include?(object.class)
- Arel::Table.new(object.to_s, :engine => @engine)
- elsif Nodes::Join === object
- object._klass ? object._klass.arel_table : Arel::Table.new(object._name, :engine => @engine)
- elsif object.respond_to?(:aliased_table_name)
- Arel::Table.new(object.table_name, :as => object.aliased_table_name, :engine => @engine)
- else
- raise ArgumentError, "Unable to get table for #{object}"
- end
- end
-
- def classify(object)
- if Class === object
- object
- elsif object.respond_to? :active_record
- object.active_record
- else
- raise ArgumentError, "#{object} can't be converted to a class"
- end
+ def get_arel_visitor
+ Arel::Visitors.visitor_for @engine
end
end
Oops, something went wrong.

0 comments on commit 298357b

Please sign in to comment.