rubygem-shoulda-matchers/rubygem-shoulda-matchers-3.1.2-Drop-legacy-callback-matchers-under-Rails-5.patch
2020-08-28 14:20:55 +08:00

202 lines
7.1 KiB
Diff

From 6ea9afc1066502fdd5c556fa9edd4ce7085169fc Mon Sep 17 00:00:00 2001
From: Elliot Winkler <elliot.winkler@gmail.com>
Date: Sun, 17 Sep 2017 19:51:41 -0500
Subject: [PATCH] Drop legacy callback matchers under Rails 5
Rails 5 dropped the legacy controller callbacks `before_filter`,
`after_filter` and `around_filter`, so the matchers for these callbacks
are useless.
---
.../action_controller/callback_matcher.rb | 24 +++++++++----
lib/shoulda/matchers/rails_shim.rb | 10 +++++-
.../unit/helpers/action_pack_versions.rb | 6 +++-
spec/support/unit/helpers/rails_versions.rb | 2 +-
.../callback_matcher_spec.rb | 36 +++++++++----------
.../allow_mass_assignment_of_matcher_spec.rb | 2 +-
6 files changed, 52 insertions(+), 28 deletions(-)
diff --git a/lib/shoulda/matchers/action_controller/callback_matcher.rb b/lib/shoulda/matchers/action_controller/callback_matcher.rb
index 9569945f..5b915270 100644
--- a/lib/shoulda/matchers/action_controller/callback_matcher.rb
+++ b/lib/shoulda/matchers/action_controller/callback_matcher.rb
@@ -20,10 +20,14 @@ module ActionController
# should_not use_before_filter(:prevent_ssl)
# end
#
+ # @note This method is only available when using shoulda-matchers under
+ # Rails 4.x.
# @return [CallbackMatcher]
#
- def use_before_filter(callback)
- CallbackMatcher.new(callback, :before, :filter)
+ if RailsShim.action_pack_lt_5?
+ def use_before_filter(callback)
+ CallbackMatcher.new(callback, :before, :filter)
+ end
end
# The `use_after_filter` matcher is used to test that an after_filter
@@ -45,10 +49,14 @@ def use_before_filter(callback)
# should_not use_after_filter(:destroy_user)
# end
#
+ # @note This method is only available when using shoulda-matchers under
+ # Rails 4.x.
# @return [CallbackMatcher]
#
- def use_after_filter(callback)
- CallbackMatcher.new(callback, :after, :filter)
+ if RailsShim.action_pack_lt_5?
+ def use_after_filter(callback)
+ CallbackMatcher.new(callback, :after, :filter)
+ end
end
# The `use_before_action` matcher is used to test that a before_action
@@ -120,10 +128,14 @@ def use_after_action(callback)
# should_not use_around_filter(:save_view_context)
# end
#
+ # @note This method is only available when using shoulda-matchers under
+ # Rails 4.x.
# @return [CallbackMatcher]
#
- def use_around_filter(callback)
- CallbackMatcher.new(callback, :around, :filter)
+ if RailsShim.action_pack_lt_5?
+ def use_around_filter(callback)
+ CallbackMatcher.new(callback, :around, :filter)
+ end
end
# The `use_around_action` matcher is used to test that an around_action
diff --git a/lib/shoulda/matchers/rails_shim.rb b/lib/shoulda/matchers/rails_shim.rb
index 1fea4eb1..c5be6a30 100644
--- a/lib/shoulda/matchers/rails_shim.rb
+++ b/lib/shoulda/matchers/rails_shim.rb
@@ -1,7 +1,7 @@
module Shoulda
module Matchers
# @private
- class RailsShim
+ module RailsShim
class << self
def action_pack_gte_4_1?
Gem::Requirement.new('>= 4.1').satisfied_by?(action_pack_version)
@@ -11,12 +11,20 @@ def action_pack_gte_5?
Gem::Requirement.new('>= 5').satisfied_by?(action_pack_version)
end
+ def action_pack_lt_5?
+ Gem::Requirement.new('< 5').satisfied_by?(action_pack_version)
+ end
+
def action_pack_version
Gem::Version.new(::ActionPack::VERSION::STRING)
+ rescue NameError
+ Gem::Version.new('0')
end
def active_record_major_version
::ActiveRecord::VERSION::MAJOR
+ rescue NameError
+ Gem::Version.new('0')
end
def generate_validation_message(
diff --git a/spec/support/unit/helpers/action_pack_versions.rb b/spec/support/unit/helpers/action_pack_versions.rb
index 86606f7b..5e6b77fa 100644
--- a/spec/support/unit/helpers/action_pack_versions.rb
+++ b/spec/support/unit/helpers/action_pack_versions.rb
@@ -8,7 +8,11 @@ def self.configure_example_group(example_group)
end
def action_pack_gte_5?
- action_pack_version =~ '>= 5'
+ action_pack_version >= 5
+ end
+
+ def action_pack_lt_5?
+ action_pack_version < 5
end
def action_pack_version
diff --git a/spec/support/unit/helpers/rails_versions.rb b/spec/support/unit/helpers/rails_versions.rb
index 161f9628..d555378e 100644
--- a/spec/support/unit/helpers/rails_versions.rb
+++ b/spec/support/unit/helpers/rails_versions.rb
@@ -25,7 +25,7 @@ def rails_gte_4_2?
rails_version >= 4.2
end
- def rails_lte_5?
+ def rails_lt_5?
rails_version < 5
end
end
diff --git a/spec/unit/shoulda/matchers/action_controller/callback_matcher_spec.rb b/spec/unit/shoulda/matchers/action_controller/callback_matcher_spec.rb
index cca0bbed..81b8249b 100644
--- a/spec/unit/shoulda/matchers/action_controller/callback_matcher_spec.rb
+++ b/spec/unit/shoulda/matchers/action_controller/callback_matcher_spec.rb
@@ -54,29 +54,29 @@ def add_callback(kind, callback_type, callback)
end
end
- describe '#use_before_filter' do
- it_behaves_like 'CallbackMatcher', :before, :filter
- end
+ if action_pack_lt_5?
+ describe '#use_before_filter' do
+ it_behaves_like 'CallbackMatcher', :before, :filter
+ end
- describe '#use_after_filter' do
- it_behaves_like 'CallbackMatcher', :after, :filter
- end
+ describe '#use_after_filter' do
+ it_behaves_like 'CallbackMatcher', :after, :filter
+ end
- describe '#use_around_filter' do
- it_behaves_like 'CallbackMatcher', :around, :filter
+ describe '#use_around_filter' do
+ it_behaves_like 'CallbackMatcher', :around, :filter
+ end
end
- if rails_4_x?
- describe '#use_before_action' do
- it_behaves_like 'CallbackMatcher', :before, :action
- end
+ describe '#use_before_action' do
+ it_behaves_like 'CallbackMatcher', :before, :action
+ end
- describe '#use_after_action' do
- it_behaves_like 'CallbackMatcher', :after, :action
- end
+ describe '#use_after_action' do
+ it_behaves_like 'CallbackMatcher', :after, :action
+ end
- describe '#use_around_action' do
- it_behaves_like 'CallbackMatcher', :around, :action
- end
+ describe '#use_around_action' do
+ it_behaves_like 'CallbackMatcher', :around, :action
end
end
diff --git a/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb
index f53c82e6..3c374f3c 100644
--- a/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb
+++ b/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb
@@ -1,7 +1,7 @@
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveModel::AllowMassAssignmentOfMatcher, type: :model do
- if rails_lte_5?
+ if action_pack_lt_5?
context '#description' do
context 'without a role' do
it 'includes the attribute name' do