mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add test/doc for :if/:except in skip_before_action
The new test/docs further explain the conflicts that can happen when mixing `:if`/`:unless` options with `:only`/`:except` options in `skip_before_action`. The gist is that "positive" filters always have priority over negative ones. The previous commit already showed that `:only` has priority over `:if`. This commit shows that `:if` has priority over `:except`. For instance, the following snippets are equivalent: ```ruby skip_before_action :some_callback, if: -> { condition }, except: action ``` ```ruby skip_before_action :some_callback, if: -> { condition } ```
This commit is contained in:
parent
ae9f803c5d
commit
9a25603d0a
2 changed files with 24 additions and 2 deletions
|
@ -33,6 +33,11 @@ module AbstractController
|
||||||
#
|
#
|
||||||
# only: :index, if: -> { true } # the :if option will be ignored.
|
# only: :index, if: -> { true } # the :if option will be ignored.
|
||||||
#
|
#
|
||||||
|
# Note that <tt>:if</tt> has priority over <tt>:except</tt> in case they
|
||||||
|
# are used together.
|
||||||
|
#
|
||||||
|
# except: :index, if: -> { true } # the :except option will be ignored.
|
||||||
|
#
|
||||||
# ==== Options
|
# ==== Options
|
||||||
# * <tt>only</tt> - The callback should be run only for this action
|
# * <tt>only</tt> - The callback should be run only for this action
|
||||||
# * <tt>except</tt> - The callback should be run for all actions except this action
|
# * <tt>except</tt> - The callback should be run for all actions except this action
|
||||||
|
|
|
@ -225,7 +225,7 @@ class FilterTest < ActionController::TestCase
|
||||||
skip_before_action :clean_up_tmp, if: -> { true }
|
skip_before_action :clean_up_tmp, if: -> { true }
|
||||||
end
|
end
|
||||||
|
|
||||||
class SkipFilterUsingOnlyAndConditional < ConditionalFilterController
|
class SkipFilterUsingOnlyAndIf < ConditionalFilterController
|
||||||
before_action :clean_up_tmp
|
before_action :clean_up_tmp
|
||||||
before_action :ensure_login
|
before_action :ensure_login
|
||||||
|
|
||||||
|
@ -237,6 +237,18 @@ class FilterTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class SkipFilterUsingIfAndExcept < ConditionalFilterController
|
||||||
|
before_action :clean_up_tmp
|
||||||
|
before_action :ensure_login
|
||||||
|
|
||||||
|
skip_before_action :ensure_login, if: -> { false }, except: :login
|
||||||
|
skip_before_action :clean_up_tmp, if: -> { true }, except: :login
|
||||||
|
|
||||||
|
def login
|
||||||
|
render text: 'ok'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class ClassController < ConditionalFilterController
|
class ClassController < ConditionalFilterController
|
||||||
before_action ConditionalClassFilter
|
before_action ConditionalClassFilter
|
||||||
end
|
end
|
||||||
|
@ -609,10 +621,15 @@ class FilterTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_if_is_ignored_when_used_with_only
|
def test_if_is_ignored_when_used_with_only
|
||||||
test_process(SkipFilterUsingOnlyAndConditional, 'login')
|
test_process(SkipFilterUsingOnlyAndIf, 'login')
|
||||||
assert_nil assigns['ran_filter']
|
assert_nil assigns['ran_filter']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_except_is_ignored_when_used_with_if
|
||||||
|
test_process(SkipFilterUsingIfAndExcept, 'login')
|
||||||
|
assert_equal %w(ensure_login), assigns["ran_filter"]
|
||||||
|
end
|
||||||
|
|
||||||
def test_skipping_class_actions
|
def test_skipping_class_actions
|
||||||
test_process(ClassController)
|
test_process(ClassController)
|
||||||
assert_equal true, assigns["ran_class_action"]
|
assert_equal true, assigns["ran_class_action"]
|
||||||
|
|
Loading…
Reference in a new issue