From 5fdbd06d22e9f045cd894a70ac324db812c29cc7 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Tue, 10 Oct 2023 21:26:35 -0300 Subject: [PATCH 1/3] Upgrade mocha to latest, fix method sequence usage and deprecations --- Gemfile.lock | 58 ++++++++++++++++++++-------------------- has_scope.gemspec | 2 +- test/has_scope_test.rb | 60 ++++++++++++++++++++++++------------------ test/test_helper.rb | 2 +- 4 files changed, 67 insertions(+), 55 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 2b2775c..846fcf4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,52 +8,54 @@ PATH GEM remote: https://rubygems.org/ specs: - actionpack (7.0.4) - actionview (= 7.0.4) - activesupport (= 7.0.4) - rack (~> 2.0, >= 2.2.0) + actionpack (7.0.8) + actionview (= 7.0.8) + activesupport (= 7.0.8) + rack (~> 2.0, >= 2.2.4) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) - actionview (7.0.4) - activesupport (= 7.0.4) + actionview (7.0.8) + activesupport (= 7.0.8) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) - activesupport (7.0.4) + activesupport (7.0.8) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) builder (3.2.4) - concurrent-ruby (1.1.10) + concurrent-ruby (1.2.2) crass (1.0.6) erubi (1.12.0) - i18n (1.12.0) + i18n (1.14.1) concurrent-ruby (~> 1.0) - loofah (2.19.1) + loofah (2.21.4) crass (~> 1.0.2) - nokogiri (>= 1.5.9) - metaclass (0.0.4) - mini_portile2 (2.8.1) - minitest (5.17.0) - mocha (1.0.0) - metaclass (~> 0.0.1) - nokogiri (1.14.0) - mini_portile2 (~> 2.8.0) + nokogiri (>= 1.12.0) + mini_portile2 (2.8.4) + minitest (5.20.0) + mocha (2.1.0) + ruby2_keywords (>= 0.0.5) + nokogiri (1.15.4) + mini_portile2 (~> 2.8.2) racc (~> 1.4) - racc (1.6.2) - rack (2.2.5) - rack-test (2.0.2) + racc (1.7.1) + rack (2.2.8) + rack-test (2.1.0) rack (>= 1.3) - rails-dom-testing (2.0.3) - activesupport (>= 4.2.0) + rails-dom-testing (2.2.0) + activesupport (>= 5.0.0) + minitest nokogiri (>= 1.6) - rails-html-sanitizer (1.4.4) - loofah (~> 2.19, >= 2.19.1) + rails-html-sanitizer (1.6.0) + loofah (~> 2.21) + nokogiri (~> 1.14) rake (13.0.6) - tzinfo (2.0.5) + ruby2_keywords (0.0.5) + tzinfo (2.0.6) concurrent-ruby (~> 1.0) PLATFORMS @@ -63,8 +65,8 @@ DEPENDENCIES actionpack (~> 7.0.0) activesupport (~> 7.0.0) has_scope! - mocha (~> 1.0.0) + mocha rake BUNDLED WITH - 2.4.3 + 2.4.20 diff --git a/has_scope.gemspec b/has_scope.gemspec index 01b50da..245451a 100644 --- a/has_scope.gemspec +++ b/has_scope.gemspec @@ -33,5 +33,5 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'activesupport', '>= 5.2' s.add_development_dependency 'rake' - s.add_development_dependency 'mocha', '~> 1.0.0' + s.add_development_dependency 'mocha' end diff --git a/test/has_scope_test.rb b/test/has_scope_test.rb index eddd7f0..d0cf862 100644 --- a/test/has_scope_test.rb +++ b/test/has_scope_test.rb @@ -87,8 +87,9 @@ class HasScopeTest < ActionController::TestCase tests TreesController def test_boolean_scope_is_called_when_boolean_param_is_true - Tree.expects(:only_tall).with().returns(Tree).in_sequence - Tree.expects(:all).returns([mock_tree]).in_sequence + tree_sequence = sequence('tree') + Tree.expects(:only_tall).with().returns(Tree).in_sequence(tree_sequence) + Tree.expects(:all).returns([mock_tree]).in_sequence(tree_sequence) get :index, params: { only_tall: 'true' } @@ -107,8 +108,9 @@ def test_boolean_scope_is_not_called_when_boolean_param_is_false end def test_boolean_scope_with_allow_blank_is_called_when_boolean_param_is_true - Tree.expects(:conifer).with(true).returns(Tree).in_sequence - Tree.expects(:all).returns([mock_tree]).in_sequence + tree_sequence = sequence('tree') + Tree.expects(:conifer).with(true).returns(Tree).in_sequence(tree_sequence) + Tree.expects(:all).returns([mock_tree]).in_sequence(tree_sequence) get :index, params: { conifer: 'true' } @@ -117,8 +119,9 @@ def test_boolean_scope_with_allow_blank_is_called_when_boolean_param_is_true end def test_boolean_scope_with_allow_blank_is_called_when_boolean_param_is_false - Tree.expects(:conifer).with(false).returns(Tree).in_sequence - Tree.expects(:all).returns([mock_tree]).in_sequence + tree_sequence = sequence('tree') + Tree.expects(:conifer).with(false).returns(Tree).in_sequence(tree_sequence) + Tree.expects(:all).returns([mock_tree]).in_sequence(tree_sequence) get :index, params: { conifer: 'not_true' } @@ -201,8 +204,9 @@ def test_scope_is_called_except_on_index end def test_scope_is_called_with_arguments - Tree.expects(:color).with('blue').returns(Tree).in_sequence - Tree.expects(:all).returns([mock_tree]).in_sequence + tree_sequence = sequence('tree') + Tree.expects(:color).with('blue').returns(Tree).in_sequence(tree_sequence) + Tree.expects(:all).returns([mock_tree]).in_sequence(tree_sequence) get :index, params: { color: 'blue' } @@ -211,8 +215,9 @@ def test_scope_is_called_with_arguments end def test_scope_is_not_called_if_blank + tree_sequence = sequence('tree') Tree.expects(:color).never - Tree.expects(:all).returns([mock_tree]).in_sequence + Tree.expects(:all).returns([mock_tree]).in_sequence(tree_sequence) get :index, params: { color: '' } @@ -221,8 +226,9 @@ def test_scope_is_not_called_if_blank end def test_scope_is_called_when_blank_if_allow_blank_is_given + tree_sequence = sequence('tree') Tree.expects(:root_type).with('').returns(Tree) - Tree.expects(:all).returns([mock_tree]).in_sequence + Tree.expects(:all).returns([mock_tree]).in_sequence(tree_sequence) get :index, params: { root: '' } @@ -349,11 +355,12 @@ def test_scope_of_invalid_type_silently_fails end def test_scope_is_called_with_default_value - Tree.expects(:shadown_range).with(10).returns(Tree).in_sequence - Tree.expects(:paginate_default).with('page' => 1, 'per_page' => 10).returns(Tree).in_sequence - Tree.expects(:args_paginate_default).with(1, 10).returns(Tree).in_sequence - Tree.expects(:metadata_default).with('default').returns(Tree).in_sequence - Tree.expects(:find).with('42').returns(mock_tree).in_sequence + tree_sequence = sequence('tree') + Tree.expects(:shadown_range).with(10).returns(Tree).in_sequence(tree_sequence) + Tree.expects(:paginate_default).with({ 'page' => 1, 'per_page' => 10 }).returns(Tree).in_sequence(tree_sequence) + Tree.expects(:args_paginate_default).with(1, 10).returns(Tree).in_sequence(tree_sequence) + Tree.expects(:metadata_default).with('default').returns(Tree).in_sequence(tree_sequence) + Tree.expects(:find).with('42').returns(mock_tree).in_sequence(tree_sequence) get :edit, params: { id: '42' } @@ -367,12 +374,13 @@ def test_scope_is_called_with_default_value end def test_default_scope_value_can_be_overwritten - Tree.expects(:shadown_range).with('20').returns(Tree).in_sequence - Tree.expects(:paginate_default).with('page' => '2', 'per_page' => '20').returns(Tree).in_sequence - Tree.expects(:args_paginate_default).with('3', '15').returns(Tree).in_sequence - Tree.expects(:metadata_blank).with(nil).returns(Tree).in_sequence - Tree.expects(:metadata_default).with('other').returns(Tree).in_sequence - Tree.expects(:find).with('42').returns(mock_tree).in_sequence + tree_sequence = sequence('tree') + Tree.expects(:shadown_range).with('20').returns(Tree).in_sequence(tree_sequence) + Tree.expects(:paginate_default).with({ 'page' => '2', 'per_page' => '20' }).returns(Tree).in_sequence(tree_sequence) + Tree.expects(:args_paginate_default).with('3', '15').returns(Tree).in_sequence(tree_sequence) + Tree.expects(:metadata_blank).with(nil).returns(Tree).in_sequence(tree_sequence) + Tree.expects(:metadata_default).with('other').returns(Tree).in_sequence(tree_sequence) + Tree.expects(:find).with('42').returns(mock_tree).in_sequence(tree_sequence) get :edit, params: { id: '42', @@ -392,8 +400,9 @@ def test_default_scope_value_can_be_overwritten end def test_scope_with_different_key - Tree.expects(:root_type).with('outside').returns(Tree).in_sequence - Tree.expects(:find).with('42').returns(mock_tree).in_sequence + tree_sequence = sequence('tree') + Tree.expects(:root_type).with('outside').returns(Tree).in_sequence(tree_sequence) + Tree.expects(:find).with('42').returns(mock_tree).in_sequence(tree_sequence) get :show, params: { id: '42', root: 'outside' } @@ -414,8 +423,9 @@ def test_scope_with_default_value_as_a_proc_without_argument def test_scope_with_default_value_as_proc_with_argument session[:height] = 100 - Tree.expects(:calculate_height).with(100).returns(Tree).in_sequence - Tree.expects(:new).returns(mock_tree).in_sequence + tree_sequence = sequence('tree') + Tree.expects(:calculate_height).with(100).returns(Tree).in_sequence(tree_sequence) + Tree.expects(:new).returns(mock_tree).in_sequence(tree_sequence) get :new diff --git a/test/test_helper.rb b/test/test_helper.rb index 3d84a82..e3d1ce0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,7 +2,7 @@ require 'minitest/autorun' require 'mocha' -require 'mocha/mini_test' +require 'mocha/minitest' # Configure Rails ENV['RAILS_ENV'] = 'test' From 39ee26d1746a5c9a9a639cee7a8adb7efb704958 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Tue, 10 Oct 2023 21:29:50 -0300 Subject: [PATCH 2/3] Add support for Rails 7.1 --- .github/workflows/test.yml | 5 +++++ CHANGELOG.md | 1 + Gemfile | 4 ++-- Gemfile.lock | 45 +++++++++++++++++++++++++------------- gemfiles/Gemfile-rails-7-0 | 6 +++++ 5 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 gemfiles/Gemfile-rails-7-0 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b9da04c..7554bf8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,6 +8,7 @@ jobs: gemfile: - Gemfile - gemfiles/Gemfile-rails-main + - gemfiles/Gemfile-rails-7-0 - gemfiles/Gemfile-rails-6-1 - gemfiles/Gemfile-rails-6-0 - gemfiles/Gemfile-rails-5-2 @@ -27,6 +28,10 @@ jobs: ruby: '2.6' - gemfile: gemfiles/Gemfile-rails-main ruby: '2.5' + - gemfile: gemfiles/Gemfile-rails-7-0 + ruby: '2.6' + - gemfile: gemfiles/Gemfile-rails-7-0 + ruby: '2.5' - gemfile: gemfiles/Gemfile-rails-6-0 ruby: '3.2' - gemfile: gemfiles/Gemfile-rails-6-0 diff --git a/CHANGELOG.md b/CHANGELOG.md index 98405e7..eecb6ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Unreleased +* Add support for Rails 7.1. (no changes required.) ## 0.8.1 diff --git a/Gemfile b/Gemfile index 3c4813d..1458fc9 100644 --- a/Gemfile +++ b/Gemfile @@ -2,5 +2,5 @@ source 'https://rubygems.org' gemspec -gem "actionpack", "~> 7.0.0" -gem "activesupport", "~> 7.0.0" +gem "actionpack", "~> 7.1.0" +gem "activesupport", "~> 7.1.0" diff --git a/Gemfile.lock b/Gemfile.lock index 846fcf4..3c0eae4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,27 +8,39 @@ PATH GEM remote: https://rubygems.org/ specs: - actionpack (7.0.8) - actionview (= 7.0.8) - activesupport (= 7.0.8) - rack (~> 2.0, >= 2.2.4) + actionpack (7.1.0) + actionview (= 7.1.0) + activesupport (= 7.1.0) + nokogiri (>= 1.8.5) + rack (>= 2.2.4) + rack-session (>= 1.0.1) rack-test (>= 0.6.3) - rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.2.0) - actionview (7.0.8) - activesupport (= 7.0.8) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + actionview (7.1.0) + activesupport (= 7.1.0) builder (~> 3.1) - erubi (~> 1.4) - rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.1, >= 1.2.0) - activesupport (7.0.8) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activesupport (7.1.0) + base64 + bigdecimal concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb i18n (>= 1.6, < 2) minitest (>= 5.1) + mutex_m tzinfo (~> 2.0) + base64 (0.1.1) + bigdecimal (3.1.4) builder (3.2.4) concurrent-ruby (1.2.2) + connection_pool (2.4.1) crass (1.0.6) + drb (2.1.1) + ruby2_keywords erubi (1.12.0) i18n (1.14.1) concurrent-ruby (~> 1.0) @@ -39,11 +51,14 @@ GEM minitest (5.20.0) mocha (2.1.0) ruby2_keywords (>= 0.0.5) + mutex_m (0.1.2) nokogiri (1.15.4) mini_portile2 (~> 2.8.2) racc (~> 1.4) racc (1.7.1) - rack (2.2.8) + rack (3.0.8) + rack-session (2.0.0) + rack (>= 3.0.0) rack-test (2.1.0) rack (>= 1.3) rails-dom-testing (2.2.0) @@ -62,8 +77,8 @@ PLATFORMS ruby DEPENDENCIES - actionpack (~> 7.0.0) - activesupport (~> 7.0.0) + actionpack (~> 7.1.0) + activesupport (~> 7.1.0) has_scope! mocha rake diff --git a/gemfiles/Gemfile-rails-7-0 b/gemfiles/Gemfile-rails-7-0 new file mode 100644 index 0000000..65517e8 --- /dev/null +++ b/gemfiles/Gemfile-rails-7-0 @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +gemspec path: ".." + +gem "actionpack", "~> 7.0.0" +gem "activesupport", "~> 7.0.0" From ee923f8b7c75f1cf051332f32cbeb9dd293c0b90 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 11 Oct 2023 09:12:30 -0300 Subject: [PATCH 3/3] Create a deprecator for the library and register it with the Rails app We can no longer use a single deprecator instance from Active Support, instead each library should create their own and register with the app so the application deprecators can control things like silencing and other behaviors across the board. --- CHANGELOG.md | 1 + lib/has_scope.rb | 8 +++++++- lib/has_scope/railtie.rb | 10 ++++++++++ test/has_scope_test.rb | 4 ++-- 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 lib/has_scope/railtie.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index eecb6ff..8fb1bac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Unreleased * Add support for Rails 7.1. (no changes required.) +* Add `HasScope.deprecator` to integrate with new application deprecators in Rails 7.1. ## 0.8.1 diff --git a/lib/has_scope.rb b/lib/has_scope.rb index d9e62df..1f28f3a 100644 --- a/lib/has_scope.rb +++ b/lib/has_scope.rb @@ -11,6 +11,10 @@ module HasScope default: [[ String, Numeric ]], } + def self.deprecator + @deprecator ||= ActiveSupport::Deprecation.new("1.0", "HasScope") + end + def self.included(base) base.class_eval do extend ClassMethods @@ -201,7 +205,7 @@ def apply_scope_to_action?(options) #:nodoc: def applicable?(string_proc_or_symbol, expected) #:nodoc: case string_proc_or_symbol when String - ActiveSupport::Deprecation.warn <<-DEPRECATION.squish + HasScope.deprecator.warn <<-DEPRECATION.squish [HasScope] Passing a string to determine if the scope should be applied is deprecated and it will be removed in a future version of HasScope. DEPRECATION @@ -222,6 +226,8 @@ def current_scopes end end +require 'has_scope/railtie' if defined?(Rails) + ActiveSupport.on_load :action_controller do include HasScope helper_method :current_scopes if respond_to?(:helper_method) diff --git a/lib/has_scope/railtie.rb b/lib/has_scope/railtie.rb new file mode 100644 index 0000000..a0abf4f --- /dev/null +++ b/lib/has_scope/railtie.rb @@ -0,0 +1,10 @@ +require 'rails/railtie' + +module HasScope + class Railtie < Rails::Railtie + initializer "has_scope.deprecator" do |app| + app.deprecators[:has_scope] = HasScope.deprecator if app.respond_to?(:deprecators) + end + end +end + diff --git a/test/has_scope_test.rb b/test/has_scope_test.rb index d0cf862..c61abdc 100644 --- a/test/has_scope_test.rb +++ b/test/has_scope_test.rb @@ -57,7 +57,7 @@ def apply_scopes(*) if params[:eval_plant] super else - ActiveSupport::Deprecation.silence { super } + HasScope.deprecator.silence { super } end end @@ -175,7 +175,7 @@ def test_scope_with_eval_string_if_and_unless_options_is_deprecated Tree.expects(:eval_plant).with('value').returns(Tree) Tree.expects(:all).returns([mock_tree]) - assert_deprecated(/Passing a string to determine if the scope should be applied is deprecated/) do + assert_deprecated(/Passing a string to determine if the scope should be applied is deprecated/, HasScope.deprecator) do get :index, params: { eval_plant: 'value', skip_eval_plant: nil } end