Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

* Support overwriting scope blocks (#126)
* Ruby 4.0 support (no changes required)

## 0.9.0
Expand Down
1 change: 1 addition & 0 deletions lib/has_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 35 additions & 5 deletions test/has_scope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -478,11 +481,6 @@ def test_scope_with_nested_hash_and_in_option
assert_equal({ q: hash }, current_scopes)
end

def test_overwritten_scope
assert_nil(TreesController.scopes_configuration[:categories][:if])
assert_equal(:categories?, BonsaisController.scopes_configuration[:categories][:if])
end

protected

def mock_tree(stubs = {})
Expand All @@ -498,6 +496,38 @@ def assigns(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])

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 assigns(ivar)
@controller.instance_variable_get(ivar)
end
end

class TreeHugger
include HasScope

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading