From 6176e9b0491397281a5fc2dffd719b6149dffb8f Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 5 May 2017 10:45:31 -0400 Subject: [PATCH] Rails 5 support. --- .gitignore | 2 ++ .travis.yml | 10 +++++++++- destroyed_at.gemspec | 4 ++-- gemfiles/Gemfile.4.2 | 6 ++++++ gemfiles/Gemfile.5.0 | 6 ++++++ lib/destroyed_at.rb | 3 ++- lib/destroyed_at/has_many_association.rb | 3 ++- 7 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 gemfiles/Gemfile.4.2 create mode 100644 gemfiles/Gemfile.5.0 diff --git a/.gitignore b/.gitignore index d74489c..5ce3e1b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ .config .yardoc Gemfile.lock +gemfiles/*.lock +vendor/ InstalledFiles _yardoc coverage diff --git a/.travis.yml b/.travis.yml index b432484..41f3859 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,18 @@ rvm: -- 2.0.0 - 2.1.0 +- 2.2.2 before_install: - gem install bundler -v '= 1.5.1' +gemfile: +- gemfiles/Gemfile.4.2 +- gemfiles/Gemfile.5.0 + +exclude: + - rvm: 2.1.0 + gemfile: gemfiles/Gemfile.5.0 + notifications: hipchat: rooms: diff --git a/destroyed_at.gemspec b/destroyed_at.gemspec index 28864f0..bffa4a3 100644 --- a/destroyed_at.gemspec +++ b/destroyed_at.gemspec @@ -19,8 +19,8 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '~> 2.0' - spec.add_runtime_dependency "activerecord", '~> 4.1' - spec.add_runtime_dependency 'actionpack', '~> 4.1' + spec.add_runtime_dependency "activerecord", '>= 4.1' + spec.add_runtime_dependency 'actionpack', '>= 4.1' spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" diff --git a/gemfiles/Gemfile.4.2 b/gemfiles/Gemfile.4.2 new file mode 100644 index 0000000..4bc5d61 --- /dev/null +++ b/gemfiles/Gemfile.4.2 @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in destroyed_at.gemspec +gemspec :path => "../" +gem 'm', github: 'unmanbearpig/m' +gem "activerecord", "~> 4.2.0" diff --git a/gemfiles/Gemfile.5.0 b/gemfiles/Gemfile.5.0 new file mode 100644 index 0000000..16079b2 --- /dev/null +++ b/gemfiles/Gemfile.5.0 @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in destroyed_at.gemspec +gemspec :path => "../" +gem 'm', github: 'unmanbearpig/m' +gem "activerecord", "~> 5.0.0" diff --git a/lib/destroyed_at.rb b/lib/destroyed_at.rb index fef8088..61d241c 100644 --- a/lib/destroyed_at.rb +++ b/lib/destroyed_at.rb @@ -30,7 +30,8 @@ def self.has_destroy_at?(object) module ClassMethods def destroyed(time = nil) query = where.not(destroyed_at: nil) - query.where_values.reject! do |node| + values = query.respond_to?(:where_clause) ? query.where_clause.send(:predicates) : query.where_values + values.reject! do |node| Arel::Nodes::Equality === node && node.left.name == 'destroyed_at' && node.right.nil? end time ? query.where(destroyed_at: time) : query.where.not(destroyed_at: nil) diff --git a/lib/destroyed_at/has_many_association.rb b/lib/destroyed_at/has_many_association.rb index 489402b..c812a00 100644 --- a/lib/destroyed_at/has_many_association.rb +++ b/lib/destroyed_at/has_many_association.rb @@ -9,7 +9,8 @@ def delete_records(records, method) r.destroy end end - update_counter(-records.length) unless inverse_updates_counter_cache? + iucc = ActiveRecord::VERSION::MAJOR >= 5 ? reflection.inverse_updates_counter_cache? : inverse_updates_counter_cache? + update_counter(-records.length) unless iucc else super end