aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activemodel/test/cases/validations/conditional_validation_test.rb20
-rw-r--r--activemodel/test/cases/validations/with_validation_test.rb20
-rw-r--r--activesupport/CHANGELOG.md5
-rw-r--r--activesupport/lib/active_support/callbacks.rb16
-rw-r--r--activesupport/test/callbacks_test.rb21
5 files changed, 68 insertions, 14 deletions
diff --git a/activemodel/test/cases/validations/conditional_validation_test.rb b/activemodel/test/cases/validations/conditional_validation_test.rb
index 4881008017..048d27446e 100644
--- a/activemodel/test/cases/validations/conditional_validation_test.rb
+++ b/activemodel/test/cases/validations/conditional_validation_test.rb
@@ -43,7 +43,9 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_if_validation_using_string_true
# When the evaluated string returns true
- Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: "a = 1; a == 1")
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: "a = 1; a == 1")
+ end
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.invalid?
assert t.errors[:title].any?
@@ -52,7 +54,9 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_unless_validation_using_string_true
# When the evaluated string returns true
- Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: "a = 1; a == 1")
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: "a = 1; a == 1")
+ end
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert_empty t.errors[:title]
@@ -60,7 +64,9 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_if_validation_using_string_false
# When the evaluated string returns false
- Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: "false")
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: "false")
+ end
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert_empty t.errors[:title]
@@ -68,7 +74,9 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_unless_validation_using_string_false
# When the evaluated string returns false
- Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: "false")
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: "false")
+ end
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.invalid?
assert t.errors[:title].any?
@@ -118,7 +126,9 @@ class ConditionalValidationTest < ActiveModel::TestCase
# ensure that it works correctly
def test_validation_with_if_as_string
Topic.validates_presence_of(:title)
- Topic.validates_presence_of(:author_name, if: "title.to_s.match('important')")
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_presence_of(:author_name, if: "title.to_s.match('important')")
+ end
t = Topic.new
assert t.invalid?, "A topic without a title should not be valid"
diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb
index 20c11dd852..5ce86738cd 100644
--- a/activemodel/test/cases/validations/with_validation_test.rb
+++ b/activemodel/test/cases/validations/with_validation_test.rb
@@ -69,26 +69,34 @@ class ValidatesWithTest < ActiveModel::TestCase
end
test "with if statements that return false" do
- Topic.validates_with(ValidatorThatAddsErrors, if: "1 == 2")
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_with(ValidatorThatAddsErrors, if: "1 == 2")
+ end
topic = Topic.new
assert topic.valid?
end
test "with if statements that return true" do
- Topic.validates_with(ValidatorThatAddsErrors, if: "1 == 1")
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_with(ValidatorThatAddsErrors, if: "1 == 1")
+ end
topic = Topic.new
assert topic.invalid?
assert_includes topic.errors[:base], ERROR_MESSAGE
end
test "with unless statements that return true" do
- Topic.validates_with(ValidatorThatAddsErrors, unless: "1 == 1")
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_with(ValidatorThatAddsErrors, unless: "1 == 1")
+ end
topic = Topic.new
assert topic.valid?
end
test "with unless statements that returns false" do
- Topic.validates_with(ValidatorThatAddsErrors, unless: "1 == 2")
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_with(ValidatorThatAddsErrors, unless: "1 == 2")
+ end
topic = Topic.new
assert topic.invalid?
assert_includes topic.errors[:base], ERROR_MESSAGE
@@ -102,7 +110,9 @@ class ValidatesWithTest < ActiveModel::TestCase
validator.expect(:is_a?, false, [Symbol])
validator.expect(:is_a?, false, [String])
- Topic.validates_with(validator, if: "1 == 1", foo: :bar)
+ ActiveSupport::Deprecation.silence do
+ Topic.validates_with(validator, if: "1 == 1", foo: :bar)
+ end
assert topic.valid?
validator.verify
end
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index c7594f71e3..afc5c5d78b 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Deprecate passing string to `:if` and `:unless` conditional options
+ on `set_callback` and `skip_callback`.
+
+ *Ryuta Kamizono*
+
* Raise `ArgumentError` when passing string to define callback.
*Ryuta Kamizono*
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb
index 16455c8b93..b3d3f3caec 100644
--- a/activesupport/lib/active_support/callbacks.rb
+++ b/activesupport/lib/active_support/callbacks.rb
@@ -667,6 +667,14 @@ module ActiveSupport
# existing chain rather than appended.
def set_callback(name, *filter_list, &block)
type, filters, options = normalize_callback_params(filter_list, block)
+
+ if options[:if].is_a?(String) || options[:unless].is_a?(String)
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
+ Passing string to :if and :unless conditional options is deprecated
+ and will be removed in Rails 5.2 without replacement.
+ MSG
+ end
+
self_chain = get_callbacks name
mapped = filters.map do |filter|
Callback.build(self_chain, filter, type, options)
@@ -690,6 +698,14 @@ module ActiveSupport
# already been set (unless the <tt>:raise</tt> option is set to <tt>false</tt>).
def skip_callback(name, *filter_list, &block)
type, filters, options = normalize_callback_params(filter_list, block)
+
+ if options[:if].is_a?(String) || options[:unless].is_a?(String)
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
+ Passing string to :if and :unless conditional options is deprecated
+ and will be removed in Rails 5.2 without replacement.
+ MSG
+ end
+
options[:raise] = true unless options.key?(:raise)
__update_callbacks(name) do |target, chain|
diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb
index dd6b89e8a7..ed26081d4e 100644
--- a/activesupport/test/callbacks_test.rb
+++ b/activesupport/test/callbacks_test.rb
@@ -192,10 +192,12 @@ module CallbacksTest
before_save Proc.new { |r| r.history << [:before_save, :symbol] }, unless: :no
before_save Proc.new { |r| r.history << "b00m" }, unless: :yes
# string
- before_save Proc.new { |r| r.history << [:before_save, :string] }, if: "yes"
- before_save Proc.new { |r| r.history << "b00m" }, if: "no"
- before_save Proc.new { |r| r.history << [:before_save, :string] }, unless: "no"
- before_save Proc.new { |r| r.history << "b00m" }, unless: "yes"
+ ActiveSupport::Deprecation.silence do
+ before_save Proc.new { |r| r.history << [:before_save, :string] }, if: "yes"
+ before_save Proc.new { |r| r.history << "b00m" }, if: "no"
+ before_save Proc.new { |r| r.history << [:before_save, :string] }, unless: "no"
+ before_save Proc.new { |r| r.history << "b00m" }, unless: "yes"
+ end
# Combined if and unless
before_save Proc.new { |r| r.history << [:before_save, :combined_symbol] }, if: :yes, unless: :no
before_save Proc.new { |r| r.history << "b00m" }, if: :yes, unless: :yes
@@ -1204,6 +1206,17 @@ module CallbacksTest
end
end
+ class DeprecatedWarningTest < ActiveSupport::TestCase
+ def test_deprecate_string_conditional_options
+ klass = Class.new(Record)
+
+ assert_deprecated { klass.before_save :tweedle, if: "true" }
+ assert_deprecated { klass.after_save :tweedle, unless: "false" }
+ assert_deprecated { klass.skip_callback :save, :before, :tweedle, if: "true" }
+ assert_deprecated { klass.skip_callback :save, :after, :tweedle, unless: "false" }
+ end
+ end
+
class NotPermittedStringCallbackTest < ActiveSupport::TestCase
def test_passing_string_callback_is_not_permitted
klass = Class.new(Record)