Skip to content

Commit

Permalink
Merge pull request #125 from heartcombo/ca/rails-71
Browse files Browse the repository at this point in the history
Rails 7.1
  • Loading branch information
carlosantoniodasilva authored Oct 11, 2023
2 parents 7cb25ad + ee923f8 commit 612c97b
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +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

Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
87 changes: 52 additions & 35 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,80 @@ 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.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.4)
activesupport (= 7.0.4)
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.4)
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.1.10)
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.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)
mutex_m (0.1.2)
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 (3.0.8)
rack-session (2.0.0)
rack (>= 3.0.0)
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
ruby

DEPENDENCIES
actionpack (~> 7.0.0)
activesupport (~> 7.0.0)
actionpack (~> 7.1.0)
activesupport (~> 7.1.0)
has_scope!
mocha (~> 1.0.0)
mocha
rake

BUNDLED WITH
2.4.3
2.4.20
6 changes: 6 additions & 0 deletions gemfiles/Gemfile-rails-7-0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'

gemspec path: ".."

gem "actionpack", "~> 7.0.0"
gem "activesupport", "~> 7.0.0"
2 changes: 1 addition & 1 deletion has_scope.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion lib/has_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lib/has_scope/railtie.rb
Original file line number Diff line number Diff line change
@@ -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

64 changes: 37 additions & 27 deletions test/has_scope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def apply_scopes(*)
if params[:eval_plant]
super
else
ActiveSupport::Deprecation.silence { super }
HasScope.deprecator.silence { super }
end
end

Expand Down Expand Up @@ -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' }

Expand All @@ -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' }

Expand All @@ -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' }

Expand Down Expand Up @@ -172,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

Expand Down Expand Up @@ -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' }

Expand All @@ -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: '' }

Expand All @@ -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: '' }

Expand Down Expand Up @@ -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' }

Expand All @@ -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',
Expand All @@ -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' }

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'minitest/autorun'
require 'mocha'
require 'mocha/mini_test'
require 'mocha/minitest'

# Configure Rails
ENV['RAILS_ENV'] = 'test'
Expand Down

0 comments on commit 612c97b

Please sign in to comment.