From 9d1ea14de596c46c4d8e39686e3ead5c6cb7d8f3 Mon Sep 17 00:00:00 2001 From: Justin Leger Date: Wed, 19 Mar 2025 12:09:43 -0700 Subject: [PATCH 1/3] Support overwriting scope blocks Previously, block was only able to be configured via the first scope definition. If it was redefined to add a scope or change the scope, it wouldn't get configured due to the conditional assignment operator. --- lib/has_scope.rb | 1 + test/has_scope_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/has_scope.rb b/lib/has_scope.rb index 1f28f3a..76b3ddb 100644 --- a/lib/has_scope.rb +++ b/lib/has_scope.rb @@ -96,6 +96,7 @@ def has_scope(*scopes, &block) options[:only] = Array(options[:only]) options[:except] = Array(options[:except]) + options[:block] = block if block self.scopes_configuration = scopes_configuration.dup diff --git a/test/has_scope_test.rb b/test/has_scope_test.rb index c61abdc..3a79a5c 100644 --- a/test/has_scope_test.rb +++ b/test/has_scope_test.rb @@ -76,6 +76,9 @@ def default_render class BonsaisController < TreesController has_scope :categories, if: :categories? + has_scope :content do |controller, scope| + scope.by_content('some other content') + end protected def categories? @@ -483,6 +486,11 @@ def test_overwritten_scope assert_equal(:categories?, BonsaisController.scopes_configuration[:categories][:if]) end + def test_overwritten_scope_with_block + assert_nil(TreesController.scopes_configuration[:content][:block]) + assert(BonsaisController.scopes_configuration[:content][:block]) + end + protected def mock_tree(stubs = {}) From 15880b5b0cdcd387228d643022592892bd0fea64 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 6 Apr 2026 17:50:06 -0300 Subject: [PATCH 2/3] Expand test coverage for overwritten scopes --- lib/has_scope.rb | 2 +- test/has_scope_test.rb | 36 +++++++++++++++++++++++++++++------- test/test_helper.rb | 1 + 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/has_scope.rb b/lib/has_scope.rb index 76b3ddb..c902f9d 100644 --- a/lib/has_scope.rb +++ b/lib/has_scope.rb @@ -96,7 +96,7 @@ def has_scope(*scopes, &block) options[:only] = Array(options[:only]) options[:except] = Array(options[:except]) - options[:block] = block if block + options[:block] = block if block self.scopes_configuration = scopes_configuration.dup diff --git a/test/has_scope_test.rb b/test/has_scope_test.rb index 3a79a5c..8cac678 100644 --- a/test/has_scope_test.rb +++ b/test/has_scope_test.rb @@ -481,26 +481,48 @@ def test_scope_with_nested_hash_and_in_option assert_equal({ q: hash }, current_scopes) end - def test_overwritten_scope + protected + + def mock_tree(stubs = {}) + @mock_tree ||= mock(stubs) + end + + def current_scopes + @controller.send :current_scopes + end + + def assigns(ivar) + @controller.instance_variable_get(ivar) + end +end + +class HasScopeOverridesTest < ActionController::TestCase + tests BonsaisController + + def test_overwritten_scopes_configuration assert_nil(TreesController.scopes_configuration[:categories][:if]) assert_equal(:categories?, BonsaisController.scopes_configuration[:categories][:if]) - end - def test_overwritten_scope_with_block assert_nil(TreesController.scopes_configuration[:content][:block]) assert(BonsaisController.scopes_configuration[:content][:block]) end + def test_overwritten_scope_block_is_called + Tree.expects(:by_content).with('some other content').returns(Tree) + Tree.expects(:metadata_blank).with(nil).returns(Tree) + Tree.expects(:all).returns([mock_tree]) + + get :index, params: { q: { content: 'the-content' } } + + assert_equal([mock_tree], assigns(:@trees)) + end + protected def mock_tree(stubs = {}) @mock_tree ||= mock(stubs) end - def current_scopes - @controller.send :current_scopes - end - def assigns(ivar) @controller.instance_variable_get(ivar) end diff --git a/test/test_helper.rb b/test/test_helper.rb index e3d1ce0..4646837 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,6 +13,7 @@ HasScope::Routes = ActionDispatch::Routing::RouteSet.new HasScope::Routes.draw do resources :trees, only: %i[index new edit show] + resources :bonsais, only: %i[index] end class ApplicationController < ActionController::Base From 816e678db44dae95b8f03e5b068cd20fde1426f1 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 6 Apr 2026 17:51:14 -0300 Subject: [PATCH 3/3] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f941b3f..a31eb06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Unreleased +* Support overwriting scope blocks (#126) * Ruby 4.0 support (no changes required) ## 0.9.0