From 132977ed45984b6a841904303199315fc1949c8c Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Wed, 3 Jan 2018 11:17:42 +0000 Subject: [PATCH] Drop support for ruby 1.9 and rails <4.1 Dropping old and unsupported versions of ruby and RoR --- lib/polyamorous.rb | 29 ++---- .../join_association.rb | 76 --------------- .../join_dependency.rb | 96 ------------------- .../join_association.rb | 2 - .../join_dependency.rb | 4 - .../join_association.rb | 2 - .../join_dependency.rb | 3 - .../make_polyamorous_inner_joins.rb | 14 --- .../join_association.rb | 46 --------- .../join_dependency.rb | 87 ----------------- polyamorous.gemspec | 10 +- spec/helpers/polyamorous_helper.rb | 10 +- spec/polyamorous/join_association_spec.rb | 16 +--- spec/polyamorous/join_dependency_spec.rb | 44 ++++----- spec/polyamorous_spec.rb | 8 ++ 15 files changed, 39 insertions(+), 408 deletions(-) delete mode 100644 lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb delete mode 100644 lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_dependency.rb delete mode 100644 lib/polyamorous/activerecord_4.1_ruby_1.9/join_association.rb delete mode 100644 lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb delete mode 100644 lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb delete mode 100644 lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb delete mode 100644 lib/polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins.rb delete mode 100644 lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb delete mode 100644 lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb create mode 100644 spec/polyamorous_spec.rb diff --git a/lib/polyamorous.rb b/lib/polyamorous.rb index c315d93..b3ff5da 100644 --- a/lib/polyamorous.rb +++ b/lib/polyamorous.rb @@ -25,32 +25,15 @@ module Polyamorous require 'polyamorous/join' require 'polyamorous/swapping_reflection_class' - ar_version = ::ActiveRecord::VERSION::STRING[0,3] - ar_version = '3_and_4.0' if ar_version < '4.1' - - 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}" + %w[join_association join_dependency].each do |file| + require "polyamorous/activerecord_#{::ActiveRecord::VERSION::STRING[0, 3]}_ruby_2/#{file}" end - 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::JoinDependency.prepend(Polyamorous::JoinDependencyExtensions) + Polyamorous::JoinDependency.singleton_class.prepend(Polyamorous::JoinDependencyExtensions::ClassMethods) + Polyamorous::JoinAssociation.prepend(Polyamorous::JoinAssociationExtensions) Polyamorous::JoinBase.class_eval do - if method_defined?(:active_record) - alias_method :base_klass, :active_record - end + alias_method :base_klass, :active_record if method_defined?(:active_record) end end diff --git a/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb b/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb deleted file mode 100644 index 3b4c303..0000000 --- a/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb +++ /dev/null @@ -1,76 +0,0 @@ -# active_record_3_and_4.0_ruby_1.9/join_association.rb -module Polyamorous - module JoinAssociationExtensions - include SwappingReflectionClass - def self.included(base) - base.class_eval do - alias_method_chain :initialize, :polymorphism - alias_method :equality_without_polymorphism, :== - alias_method :==, :equality_with_polymorphism - if base.method_defined?(:active_record) - alias_method :base_klass, :active_record - end - - if ActiveRecord::VERSION::STRING =~ /^3\.0\./ - alias_method_chain :association_join, :polymorphism - else - alias_method_chain :build_constraint, :polymorphism - end - end - end - - def initialize_with_polymorphism( - reflection, join_dependency, parent = nil, polymorphic_class = nil - ) - if polymorphic_class && ::ActiveRecord::Base > polymorphic_class - swapping_reflection_klass(reflection, polymorphic_class) do |reflection| - initialize_without_polymorphism(reflection, join_dependency, parent) - self.reflection.options[:polymorphic] = true - end - else - initialize_without_polymorphism(reflection, join_dependency, parent) - end - end - - def equality_with_polymorphism(other) - equality_without_polymorphism(other) && base_klass == other.base_klass - end - - def build_constraint_with_polymorphism( - reflection, table, key, foreign_table, foreign_key - ) - if reflection.options[:polymorphic] - build_constraint_without_polymorphism( - reflection, table, key, foreign_table, foreign_key - ) - .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name)) - else - build_constraint_without_polymorphism( - reflection, table, key, foreign_table, foreign_key - ) - end - end - - def association_join_with_polymorphism - return @join if @Join - @join = association_join_without_polymorphism - if reflection.macro == :belongs_to && reflection.options[:polymorphic] - aliased_table = Arel::Table.new( - table_name, - as: @aliased_table_name, - engine: arel_engine, - columns: klass.columns - ) - parent_table = Arel::Table.new( - parent.table_name, - as: parent.aliased_table_name, - engine: arel_engine, - columns: parent.base_klass.columns - ) - @join << parent_table[reflection.options[:foreign_type]] - .eq(reflection.klass.name) - end - @join - end - end -end diff --git a/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_dependency.rb b/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_dependency.rb deleted file mode 100644 index b67e26f..0000000 --- a/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_dependency.rb +++ /dev/null @@ -1,96 +0,0 @@ -# active_record_3_and_4.0_ruby_1.9/join_dependency.rb -module Polyamorous - module JoinDependencyExtensions - def self.included(base) - base.class_eval do - alias_method_chain :build, :polymorphism - alias_method_chain :graft, :polymorphism - if base.method_defined?(:active_record) - alias_method :base_klass, :active_record - end - end - end - - def graft_with_polymorphism(*associations) - associations.each do |association| - unless join_associations.detect { |a| association == a } - if association.reflection.options[:polymorphic] - build( - Join.new( - association.reflection.name, - association.join_type, - association.reflection.klass - ), - association.find_parent_in(self) || join_base, - association.join_type - ) - else - build( - association.reflection.name, - association.find_parent_in(self) || join_base, - association.join_type - ) - end - end - end - self - end - - if ActiveRecord::VERSION::STRING =~ /^3\.0\./ - def _join_parts - @joins - end - else - def _join_parts - @join_parts - end - end - - def build_with_polymorphism( - associations, parent = nil, join_type = InnerJoin - ) - case associations - when Join - parent ||= _join_parts.last - reflection = parent.reflections[associations.name] or - raise ::ActiveRecord::ConfigurationError, - "Association named '#{associations.name - }' was not found; perhaps you misspelled it?" - - unless join_association = find_join_association_respecting_polymorphism( - reflection, parent, associations.klass - ) - @reflections << reflection - join_association = build_join_association_respecting_polymorphism( - reflection, parent, associations.klass - ) - join_association.join_type = associations.type - _join_parts << join_association - cache_joined_association(join_association) - end - - join_association - else - build_without_polymorphism(associations, parent, join_type) - end - end - - def find_join_association_respecting_polymorphism(reflection, parent, klass) - if association = find_join_association(reflection, parent) - unless reflection.options[:polymorphic] - association - else - association if association.base_klass == klass - end - end - end - - def build_join_association_respecting_polymorphism(reflection, parent, klass) - if reflection.options[:polymorphic] && klass - JoinAssociation.new(reflection, self, parent, klass) - else - JoinAssociation.new(reflection, self, parent) - end - end - end -end diff --git a/lib/polyamorous/activerecord_4.1_ruby_1.9/join_association.rb b/lib/polyamorous/activerecord_4.1_ruby_1.9/join_association.rb deleted file mode 100644 index b56ac7f..0000000 --- a/lib/polyamorous/activerecord_4.1_ruby_1.9/join_association.rb +++ /dev/null @@ -1,2 +0,0 @@ -# active_record_4.1_ruby_1.9/join_association.rb -require 'polyamorous/activerecord_4.2_ruby_1.9/join_association' diff --git a/lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb b/lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb deleted file mode 100644 index e3fd908..0000000 --- a/lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb +++ /dev/null @@ -1,4 +0,0 @@ -# active_record_4.1_ruby_1.9/join_dependency.rb -require 'polyamorous/activerecord_4.2_ruby_2/join_dependency' -require 'polyamorous/activerecord_4.2_ruby_1.9/join_dependency' -require 'polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins' diff --git a/lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb b/lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb deleted file mode 100644 index c7495ad..0000000 --- a/lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb +++ /dev/null @@ -1,2 +0,0 @@ -# active_record_4.1_ruby_2/join_association.rb -require 'polyamorous/activerecord_5.0_ruby_2/join_association' diff --git a/lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb b/lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb deleted file mode 100644 index 967899e..0000000 --- a/lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb +++ /dev/null @@ -1,3 +0,0 @@ -# active_record_4.1_ruby_2/join_dependency.rb -require 'polyamorous/activerecord_4.2_ruby_2/join_dependency' -require 'polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins' diff --git a/lib/polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins.rb b/lib/polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins.rb deleted file mode 100644 index b2ab5ac..0000000 --- a/lib/polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Polyamorous - module JoinDependencyExtensions - # Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins - # - def make_polyamorous_inner_joins(parent, child) - make_constraints( - parent, child, child.tables, child.join_type || Arel::Nodes::InnerJoin - ) - .concat child.children.flat_map { |c| - make_polyamorous_inner_joins(child, c) - } - end - end -end diff --git a/lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb b/lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb deleted file mode 100644 index 4b1ec47..0000000 --- a/lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb +++ /dev/null @@ -1,46 +0,0 @@ -# active_record_4.2_ruby_1.9/join_association.rb -module Polyamorous - module JoinAssociationExtensions - include SwappingReflectionClass - def self.included(base) - base.class_eval do - attr_reader :join_type - alias_method_chain :initialize, :polymorphism - alias_method_chain :build_constraint, :polymorphism - end - end - - def initialize_with_polymorphism(reflection, children, - polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin) - @join_type = join_type - if polymorphic_class && ::ActiveRecord::Base > polymorphic_class - swapping_reflection_klass(reflection, polymorphic_class) do |reflection| - initialize_without_polymorphism(reflection, children) - self.reflection.options[:polymorphic] = true - end - else - initialize_without_polymorphism(reflection, children) - end - end - - # Reference https://github.com/rails/rails/commit/9b15db51b78028bfecdb85595624de4b838adbd1 - def ==(other) - base_klass == other.base_klass - end - - def build_constraint_with_polymorphism( - klass, table, key, foreign_table, foreign_key - ) - if reflection.polymorphic? - build_constraint_without_polymorphism( - klass, table, key, foreign_table, foreign_key - ) - .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name)) - else - build_constraint_without_polymorphism( - klass, table, key, foreign_table, foreign_key - ) - end - end - end -end diff --git a/lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb b/lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb deleted file mode 100644 index 4252c8b..0000000 --- a/lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb +++ /dev/null @@ -1,87 +0,0 @@ -# active_record_4.2_ruby_1.9/join_dependency.rb -require 'polyamorous/activerecord_4.2_ruby_2/join_dependency' - -module Polyamorous - module JoinDependencyExtensions - def self.included(base) - base.extend ClassMethods - base.class_eval do - class << self - alias_method :walk_tree_without_polymorphism, :walk_tree - alias_method :walk_tree, :walk_tree_with_polymorphism - end - - alias_method :build_without_polymorphism, :build - alias_method :build, :build_with_polymorphism - - alias_method :join_constraints_without_polymorphism, :join_constraints - alias_method :join_constraints, :join_constraints_with_polymorphism - end - end - - # Replaces ActiveRecord::Associations::JoinDependency#build - # - def build_with_polymorphism(associations, base_klass) - associations.map do |name, right| - if name.is_a? Join - reflection = find_reflection base_klass, name.name - reflection.check_validity! - klass = if reflection.polymorphic? - name.klass || base_klass - else - reflection.klass - end - JoinAssociation.new(reflection, build(right, klass), name.klass, name.type) - else - reflection = find_reflection base_klass, name - reflection.check_validity! - if reflection.polymorphic? - raise ActiveRecord::EagerLoadPolymorphicError.new(reflection) - end - JoinAssociation.new reflection, build(right, reflection.klass) - end - end - end - - # Replaces ActiveRecord::Associations::JoinDependency#join_constraints - # to call #make_polyamorous_inner_joins instead of #make_inner_joins - # - def join_constraints_with_polymorphism(outer_joins) - joins = join_root.children.flat_map { |child| - make_polyamorous_inner_joins join_root, child - } - joins.concat outer_joins.flat_map { |oj| - if join_root.match? oj.join_root - walk(join_root, oj.join_root) - else - oj.join_root.children.flat_map { |child| - make_outer_joins(oj.join_root, child) - } - end - } - end - - module ClassMethods - # Replaces ActiveRecord::Associations::JoinDependency#self.walk_tree - # - def walk_tree_with_polymorphism(associations, hash) - case associations - when TreeNode - associations.add_to_tree(hash) - when Hash - associations.each do |k, v| - cache = - if TreeNode === k - k.add_to_tree(hash) - else - hash[k] ||= {} - end - walk_tree(v, cache) - end - else - walk_tree_without_polymorphism(associations, hash) - end - end - end - end -end diff --git a/polyamorous.gemspec b/polyamorous.gemspec index 555dd03..6c9017a 100644 --- a/polyamorous.gemspec +++ b/polyamorous.gemspec @@ -20,18 +20,12 @@ Gem::Specification.new do |s| s.rubyforge_project = "polyamorous" - s.add_dependency 'activerecord', '>= 3.0' + s.add_dependency 'activerecord', '>= 4.2' s.add_development_dependency 'rspec', '~> 3' s.add_development_dependency 'machinist', '~> 1.0.6' s.add_development_dependency 'faker', '~> 1.6.5' s.add_development_dependency 'sqlite3', '~> 1.3.3' - s.files = `git ls-files`.split("\n") - s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") - s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } s.require_paths = ["lib"] - - # specify any dependencies here; for example: - # s.add_development_dependency "rspec" - # s.add_runtime_dependency "rest-client" end diff --git a/spec/helpers/polyamorous_helper.rb b/spec/helpers/polyamorous_helper.rb index 8dff269..eca9593 100644 --- a/spec/helpers/polyamorous_helper.rb +++ b/spec/helpers/polyamorous_helper.rb @@ -1,12 +1,6 @@ module PolyamorousHelper - if ActiveRecord::VERSION::STRING >= "4.1" - def new_join_association(reflection, children, klass) - Polyamorous::JoinAssociation.new reflection, children, klass - end - else - def new_join_association(reflection, join_dependency, parent, klass) - Polyamorous::JoinAssociation.new reflection, join_dependency, parent, klass - end + def new_join_association(reflection, children, klass) + Polyamorous::JoinAssociation.new reflection, children, klass end if ActiveRecord::VERSION::STRING >= "5.2" diff --git a/spec/polyamorous/join_association_spec.rb b/spec/polyamorous/join_association_spec.rb index ab8ed56..102b197 100644 --- a/spec/polyamorous/join_association_spec.rb +++ b/spec/polyamorous/join_association_spec.rb @@ -2,20 +2,10 @@ module Polyamorous describe JoinAssociation do - - join_base, join_association_args, polymorphic = - if ActiveRecord::VERSION::STRING >= '4.1' - [:join_root, 'parent.children', 'reflection.options[:polymorphic]'] - else - [:join_base, 'join_dependency, parent', 'options[:polymorphic]'] - end - let(:join_dependency) { new_join_dependency Note, {} } let(:reflection) { Note.reflect_on_association(:notable) } - let(:parent) { join_dependency.send(join_base) } - let(:join_association) { - eval("new_join_association(reflection, #{join_association_args}, Article)") - } + let(:parent) { join_dependency.send(:join_root) } + let(:join_association) { new_join_association(reflection, 'parent.children', Article) } subject { join_dependency.build_join_association_respecting_polymorphism( @@ -48,7 +38,7 @@ module Polyamorous end it 'sets the polymorphic option to true after initializing' do - expect(join_association.instance_eval(polymorphic)).to be true + expect(join_association.instance_eval('reflection.options[:polymorphic]')).to be true end end end diff --git a/spec/polyamorous/join_dependency_spec.rb b/spec/polyamorous/join_dependency_spec.rb index c9f96ab..d55f312 100644 --- a/spec/polyamorous/join_dependency_spec.rb +++ b/spec/polyamorous/join_dependency_spec.rb @@ -2,38 +2,30 @@ module Polyamorous describe JoinDependency do - - method, join_associations, join_base = - if ActiveRecord::VERSION::STRING >= '4.1' - [:instance_eval, 'join_root.drop(1)', :join_root] - else - [:send, 'join_associations', :join_base] - end - context 'with symbol joins' do subject { new_join_dependency Person, articles: :comments } - specify { expect(subject.send(method, join_associations).size) + specify { expect(subject.instance_eval('join_root.drop(1)').size) .to eq(2) } - specify { expect(subject.send(method, join_associations).map(&:join_type)) + specify { expect(subject.instance_eval('join_root.drop(1)').map(&:join_type)) .to be_all { Polyamorous::InnerJoin } } end context 'with has_many :through association' do subject { new_join_dependency Person, :authored_article_comments } - specify { expect(subject.send(method, join_associations).size) + specify { expect(subject.instance_eval('join_root.drop(1)').size) .to eq 1 } - specify { expect(subject.send(method, join_associations).first.table_name) + specify { expect(subject.instance_eval('join_root.drop(1)').first.table_name) .to eq 'comments' } end context 'with outer join' do subject { new_join_dependency Person, new_join(:articles, :outer) } - specify { expect(subject.send(method, join_associations).size) + specify { expect(subject.instance_eval('join_root.drop(1)').size) .to eq 1 } - specify { expect(subject.send(method, join_associations).first.join_type) + specify { expect(subject.instance_eval('join_root.drop(1)').first.join_type) .to eq Polyamorous::OuterJoin } end @@ -41,31 +33,31 @@ module Polyamorous subject { new_join_dependency Person, new_join(:articles, :outer) => new_join(:comments, :outer) } - specify { expect(subject.send(method, join_associations).size) + specify { expect(subject.instance_eval('join_root.drop(1)').size) .to eq 2 } - specify { expect(subject.send(method, join_associations).map(&:join_type)) + specify { expect(subject.instance_eval('join_root.drop(1)').map(&:join_type)) .to eq [Polyamorous::OuterJoin, Polyamorous::OuterJoin] } - specify { expect(subject.send(method, join_associations).map(&:join_type)) + specify { expect(subject.instance_eval('join_root.drop(1)').map(&:join_type)) .to be_all { Polyamorous::OuterJoin } } end context 'with polymorphic belongs_to join' do subject { new_join_dependency Note, new_join(:notable, :inner, Person) } - specify { expect(subject.send(method, join_associations).size) + specify { expect(subject.instance_eval('join_root.drop(1)').size) .to eq 1 } - specify { expect(subject.send(method, join_associations).first.join_type) + specify { expect(subject.instance_eval('join_root.drop(1)').first.join_type) .to eq Polyamorous::InnerJoin } - specify { expect(subject.send(method, join_associations).first.table_name) + specify { expect(subject.instance_eval('join_root.drop(1)').first.table_name) .to eq 'people' } it 'finds a join association respecting polymorphism' do - parent = subject.send(join_base) + parent = subject.send(:join_root) reflection = Note.reflect_on_association(:notable) expect(subject.find_join_association_respecting_polymorphism( reflection, parent, Person)) - .to eq subject.send(method, join_associations).first + .to eq subject.instance_eval('join_root.drop(1)').first end end @@ -73,13 +65,13 @@ module Polyamorous subject { new_join_dependency Note, new_join(:notable, :inner, Person) => :comments } - specify { expect(subject.send(method, join_associations).size) + specify { expect(subject.instance_eval('join_root.drop(1)').size) .to eq 2 } - specify { expect(subject.send(method, join_associations).map(&:join_type)) + specify { expect(subject.instance_eval('join_root.drop(1)').map(&:join_type)) .to be_all { Polyamorous::InnerJoin } } - specify { expect(subject.send(method, join_associations).first.table_name) + specify { expect(subject.instance_eval('join_root.drop(1)').first.table_name) .to eq 'people' } - specify { expect(subject.send(method, join_associations)[1].table_name) + specify { expect(subject.instance_eval('join_root.drop(1)')[1].table_name) .to eq 'comments' } end diff --git a/spec/polyamorous_spec.rb b/spec/polyamorous_spec.rb new file mode 100644 index 0000000..3dd197f --- /dev/null +++ b/spec/polyamorous_spec.rb @@ -0,0 +1,8 @@ + +require 'spec_helper' + +describe Polyamorous do + it 'has a version number' do + expect(Polyamorous::VERSION).not_to be nil + end +end