lib/polyamorous.rb in polyamorous-1.1.0 vs lib/polyamorous.rb in polyamorous-1.2.0

- old
+ new

@@ -1,6 +1,6 @@ -require "polyamorous/version" +require 'polyamorous/version' if defined?(::ActiveRecord) module Polyamorous if defined?(Arel::InnerJoin) InnerJoin = Arel::InnerJoin @@ -21,23 +21,41 @@ end end require 'polyamorous/tree_node' require 'polyamorous/join' + require 'polyamorous/swapping_reflection_class' - if ActiveRecord::VERSION::STRING >= "4.2" - require 'polyamorous/activerecord_4.1/join_association' - require 'polyamorous/activerecord_4.2/join_dependency' - elsif ActiveRecord::VERSION::STRING >= "4.1" - require 'polyamorous/activerecord_4.1/join_association' - require 'polyamorous/activerecord_4.1/join_dependency' - else - require 'polyamorous/activerecord_3_and_4.0/join_association' - require 'polyamorous/activerecord_3_and_4.0/join_dependency' + ar_version = + case ::ActiveRecord::VERSION::STRING[0,3] + when '4.2', '5.0' + '4.2' + when '4.1' + '4.1' + else + '3_and_4.0' + end + + method, ruby_version = + if RUBY_VERSION >= '2.0' && ar_version >= '4.1' + # Ruby 2; we can use `prepend` to patch Active Record cleanly. + [:prepend, '2'] + else + # Ruby 1.9; we must use `alias_method` to patch Active Record. + [:include, '1.9'] + end + + %w(join_association join_dependency).each do |file| + require "polyamorous/activerecord_#{ar_version}_ruby_#{ruby_version}/#{file}" end - Polyamorous::JoinDependency.send(:include, Polyamorous::JoinDependencyExtensions) - Polyamorous::JoinAssociation.send(:include, Polyamorous::JoinAssociationExtensions) + Polyamorous::JoinDependency.send(method, Polyamorous::JoinDependencyExtensions) + if method == :prepend + Polyamorous::JoinDependency.singleton_class + .send(:prepend, Polyamorous::JoinDependencyExtensions::ClassMethods) + end + Polyamorous::JoinAssociation.send(method, Polyamorous::JoinAssociationExtensions) + Polyamorous::JoinBase.class_eval do if method_defined?(:active_record) alias_method :base_klass, :active_record end end