提交 c2fa5360 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #26755 from rafaelfranca/deprecations

Remove deprecations in Active Model, Action View and Active Job
* Removed deprecated `#original_exception` in `ActionView::Template::Error`.
*Rafael Mendonça França*
* Render now accepts any keys for locals, including reserved words * Render now accepts any keys for locals, including reserved words
Only locals with valid variable names get set directly. Others Only locals with valid variable names get set directly. Others
......
...@@ -63,23 +63,13 @@ class Error < ActionViewError #:nodoc: ...@@ -63,23 +63,13 @@ class Error < ActionViewError #:nodoc:
# Override to prevent #cause resetting during re-raise. # Override to prevent #cause resetting during re-raise.
attr_reader :cause attr_reader :cause
def initialize(template, original_exception = nil) def initialize(template)
if original_exception
ActiveSupport::Deprecation.warn("Passing #original_exception is deprecated and has no effect. " \
"Exceptions will automatically capture the original exception.", caller)
end
super($!.message) super($!.message)
set_backtrace($!.backtrace) set_backtrace($!.backtrace)
@cause = $! @cause = $!
@template, @sub_templates = template, nil @template, @sub_templates = template, nil
end end
def original_exception
ActiveSupport::Deprecation.warn("#original_exception is deprecated. Use #cause instead.", caller)
cause
end
def file_name def file_name
@template.identifier @template.identifier
end end
......
* Removed deprecated support to passing the adapter class to `.queue_adapter`.
*Rafael Mendonça França*
* Removed deprecated `#original_exception` in `ActiveJob::DeserializationError`.
*Rafael Mendonça França*
* Added instance variable `@queue` to JobWrapper. * Added instance variable `@queue` to JobWrapper.
This will fix issues in [resque-scheduler](https://github.com/resque/resque-scheduler) `#job_to_hash` method, This will fix issues in [resque-scheduler](https://github.com/resque/resque-scheduler) `#job_to_hash` method,
......
...@@ -5,22 +5,10 @@ module ActiveJob ...@@ -5,22 +5,10 @@ module ActiveJob
# #
# Wraps the original exception raised as +cause+. # Wraps the original exception raised as +cause+.
class DeserializationError < StandardError class DeserializationError < StandardError
def initialize(e = nil) #:nodoc: def initialize #:nodoc:
if e
ActiveSupport::Deprecation.warn("Passing #original_exception is deprecated and has no effect. " \
"Exceptions will automatically capture the original exception.", caller)
end
super("Error while trying to deserialize arguments: #{$!.message}") super("Error while trying to deserialize arguments: #{$!.message}")
set_backtrace $!.backtrace set_backtrace $!.backtrace
end end
# The original exception that was raised during deserialization of job
# arguments.
def original_exception
ActiveSupport::Deprecation.warn("#original_exception is deprecated. Use #cause instead.", caller)
cause
end
end end
# Raised when an unsupported argument type is set as a job argument. We # Raised when an unsupported argument type is set as a job argument. We
......
...@@ -37,12 +37,6 @@ def interpret_adapter(name_or_adapter_or_class) ...@@ -37,12 +37,6 @@ def interpret_adapter(name_or_adapter_or_class)
else else
if queue_adapter?(name_or_adapter_or_class) if queue_adapter?(name_or_adapter_or_class)
name_or_adapter_or_class name_or_adapter_or_class
elsif queue_adapter_class?(name_or_adapter_or_class)
ActiveSupport::Deprecation.warn "Passing an adapter class is deprecated " \
"and will be removed in Rails 5.1. Please pass an adapter name " \
"(.queue_adapter = :#{name_or_adapter_or_class.name.demodulize.remove('Adapter').underscore}) " \
"or an instance (.queue_adapter = #{name_or_adapter_or_class.name}.new) instead."
name_or_adapter_or_class.new
else else
raise ArgumentError raise ArgumentError
end end
...@@ -54,10 +48,6 @@ def interpret_adapter(name_or_adapter_or_class) ...@@ -54,10 +48,6 @@ def interpret_adapter(name_or_adapter_or_class)
def queue_adapter?(object) def queue_adapter?(object)
QUEUE_ADAPTER_METHODS.all? { |meth| object.respond_to?(meth) } QUEUE_ADAPTER_METHODS.all? { |meth| object.respond_to?(meth) }
end end
def queue_adapter_class?(object)
object.is_a?(Class) && QUEUE_ADAPTER_METHODS.all? { |meth| object.public_method_defined?(meth) }
end
end end
end end
end end
...@@ -20,19 +20,6 @@ class QueueAdapterTest < ActiveJob::TestCase ...@@ -20,19 +20,6 @@ class QueueAdapterTest < ActiveJob::TestCase
assert_raises(ArgumentError) { ActiveJob::Base.queue_adapter = Mutex.new } assert_raises(ArgumentError) { ActiveJob::Base.queue_adapter = Mutex.new }
end end
test "should warn on passing an adapter class" do
klass = Class.new do
def self.name
"fake"
end
def enqueue(*); end
def enqueue_at(*); end
end
assert_deprecated { ActiveJob::Base.queue_adapter = klass }
end
test "should allow overriding the queue_adapter at the child class level without affecting the parent or its sibling" do test "should allow overriding the queue_adapter at the child class level without affecting the parent or its sibling" do
base_queue_adapter = ActiveJob::Base.queue_adapter base_queue_adapter = ActiveJob::Base.queue_adapter
......
* Removed deprecated `:tokenizer` in the length validator.
*Rafael Mendonça França*
* Removed deprecated methods in `ActiveModel::Errors`.
`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.
*Rafael Mendonça França*
Please check [5-0-stable](https://github.com/rails/rails/blob/5-0-stable/activemodel/CHANGELOG.md) for previous changes. Please check [5-0-stable](https://github.com/rails/rails/blob/5-0-stable/activemodel/CHANGELOG.md) for previous changes.
...@@ -115,36 +115,6 @@ def include?(attribute) ...@@ -115,36 +115,6 @@ def include?(attribute)
alias :has_key? :include? alias :has_key? :include?
alias :key? :include? alias :key? :include?
# Get messages for +key+.
#
# person.errors.messages # => {:name=>["cannot be nil"]}
# person.errors.get(:name) # => ["cannot be nil"]
# person.errors.get(:age) # => []
def get(key)
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#get is deprecated and will be removed in Rails 5.1.
To achieve the same use model.errors[:#{key}].
MESSAGE
messages[key]
end
# Set messages for +key+ to +value+.
#
# person.errors[:name] # => ["cannot be nil"]
# person.errors.set(:name, ["can't be nil"])
# person.errors[:name] # => ["can't be nil"]
def set(key, value)
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#set is deprecated and will be removed in Rails 5.1.
Use model.errors.add(:#{key}, #{value.inspect}) instead.
MESSAGE
messages[key] = value
end
# Delete messages for +key+. Returns the deleted messages. # Delete messages for +key+. Returns the deleted messages.
# #
# person.errors[:name] # => ["cannot be nil"] # person.errors[:name] # => ["cannot be nil"]
...@@ -173,20 +143,6 @@ def [](attribute) ...@@ -173,20 +143,6 @@ def [](attribute)
messages[attribute.to_sym] messages[attribute.to_sym]
end end
# Adds to the supplied attribute the supplied error message.
#
# person.errors[:name] = "must be set"
# person.errors[:name] # => ['must be set']
def []=(attribute, error)
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#[]= is deprecated and will be removed in Rails 5.1.
Use model.errors.add(:#{attribute}, #{error.inspect}) instead.
MESSAGE
messages[attribute.to_sym] << error
end
# Iterates through each error key, value pair in the error messages hash. # Iterates through each error key, value pair in the error messages hash.
# Yields the attribute and the error for that attribute. If the attribute # Yields the attribute and the error for that attribute. If the attribute
# has more than one error message, yields once for each error message. # has more than one error message, yields once for each error message.
...@@ -338,49 +294,6 @@ def add(attribute, message = :invalid, options = {}) ...@@ -338,49 +294,6 @@ def add(attribute, message = :invalid, options = {})
messages[attribute.to_sym] << message messages[attribute.to_sym] << message
end end
# Will add an error message to each of the attributes in +attributes+
# that is empty.
#
# person.errors.add_on_empty(:name)
# person.errors.messages
# # => {:name=>["can't be empty"]}
def add_on_empty(attributes, options = {})
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#add_on_empty is deprecated and will be removed in Rails 5.1.
To achieve the same use:
errors.add(attribute, :empty, options) if value.nil? || value.empty?
MESSAGE
Array(attributes).each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
is_empty = value.respond_to?(:empty?) ? value.empty? : false
add(attribute, :empty, options) if value.nil? || is_empty
end
end
# Will add an error message to each of the attributes in +attributes+ that
# is blank (using Object#blank?).
#
# person.errors.add_on_blank(:name)
# person.errors.messages
# # => {:name=>["can't be blank"]}
def add_on_blank(attributes, options = {})
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#add_on_blank is deprecated and will be removed in Rails 5.1.
To achieve the same use:
errors.add(attribute, :blank, options) if value.blank?
MESSAGE
Array(attributes).each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
add(attribute, :blank, options) if value.blank?
end
end
# Returns +true+ if an error on the attribute with the given message is # Returns +true+ if an error on the attribute with the given message is
# present, or +false+ otherwise. +message+ is treated the same as for +add+. # present, or +false+ otherwise. +message+ is treated the same as for +add+.
# #
......
...@@ -6,7 +6,7 @@ class LengthValidator < EachValidator # :nodoc: ...@@ -6,7 +6,7 @@ class LengthValidator < EachValidator # :nodoc:
MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze
CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze
RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long] RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :too_short, :too_long]
def initialize(options) def initialize(options)
if range = (options.delete(:in) || options.delete(:within)) if range = (options.delete(:in) || options.delete(:within))
...@@ -18,27 +18,6 @@ def initialize(options) ...@@ -18,27 +18,6 @@ def initialize(options)
options[:minimum] = 1 options[:minimum] = 1
end end
if options[:tokenizer]
ActiveSupport::Deprecation.warn(<<-EOS.strip_heredoc)
The `:tokenizer` option is deprecated, and will be removed in Rails 5.1.
You can achieve the same functionality by defining an instance method
with the value that you want to validate the length of. For example,
validates_length_of :essay, minimum: 100,
tokenizer: ->(str) { str.scan(/\w+/) }
should be written as
validates_length_of :words_in_essay, minimum: 100
private
def words_in_essay
essay.scan(/\w+/)
end
EOS
end
super super
end end
...@@ -59,7 +38,6 @@ def check_validity! ...@@ -59,7 +38,6 @@ def check_validity!
end end
def validate_each(record, attribute, value) def validate_each(record, attribute, value)
value = tokenize(record, value)
value_length = value.respond_to?(:length) ? value.length : value.to_s.length value_length = value.respond_to?(:length) ? value.length : value.to_s.length
errors_options = options.except(*RESERVED_OPTIONS) errors_options = options.except(*RESERVED_OPTIONS)
...@@ -80,17 +58,6 @@ def validate_each(record, attribute, value) ...@@ -80,17 +58,6 @@ def validate_each(record, attribute, value)
end end
private private
def tokenize(record, value)
tokenizer = options[:tokenizer]
if tokenizer && value.kind_of?(String)
if tokenizer.kind_of?(Proc)
tokenizer.call(value)
elsif record.respond_to?(tokenizer)
record.send(tokenizer, value)
end
end || value
end
def skip_nil_check?(key) def skip_nil_check?(key)
key == :maximum && options[:allow_nil].nil? && options[:allow_blank].nil? key == :maximum && options[:allow_nil].nil? && options[:allow_blank].nil?
end end
......
...@@ -79,24 +79,6 @@ def test_no_key ...@@ -79,24 +79,6 @@ def test_no_key
assert person.errors.empty? assert person.errors.empty?
end end
test "get returns the errors for the provided key" do
errors = ActiveModel::Errors.new(self)
errors[:foo] << "omg"
assert_deprecated do
assert_equal ["omg"], errors.get(:foo)
end
end
test "sets the error with the provided key" do
errors = ActiveModel::Errors.new(self)
assert_deprecated do
errors.set(:foo, "omg")
end
assert_equal({ foo: "omg" }, errors.messages)
end
test "error access is indifferent" do test "error access is indifferent" do
errors = ActiveModel::Errors.new(self) errors = ActiveModel::Errors.new(self)
errors[:foo] << "omg" errors[:foo] << "omg"
...@@ -142,14 +124,6 @@ def test_no_key ...@@ -142,14 +124,6 @@ def test_no_key
assert_equal ["cannot be nil"], person.errors[:name] assert_equal ["cannot be nil"], person.errors[:name]
end end
test "assign error" do
person = Person.new
assert_deprecated do
person.errors[:name] = "should not be nil"
end
assert_equal ["should not be nil"], person.errors[:name]
end
test "add an error message on a specific attribute" do test "add an error message on a specific attribute" do
person = Person.new person = Person.new
person.errors.add(:name, "cannot be blank") person.errors.add(:name, "cannot be blank")
...@@ -320,72 +294,6 @@ def test_no_key ...@@ -320,72 +294,6 @@ def test_no_key
} }
end end
test "add_on_empty generates message" do
person = Person.new
assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do
assert_deprecated do
person.errors.add_on_empty :name
end
end
end
test "add_on_empty generates message for multiple attributes" do
person = Person.new
expected_calls = [ [:name, :empty, {}], [:age, :empty, {}] ]
assert_called_with(person.errors, :generate_message, expected_calls) do
assert_deprecated do
person.errors.add_on_empty [:name, :age]
end
end
end
test "add_on_empty generates message with custom default message" do
person = Person.new
assert_called_with(person.errors, :generate_message, [:name, :empty, { message: "custom" }]) do
assert_deprecated do
person.errors.add_on_empty :name, message: "custom"
end
end
end
test "add_on_empty generates message with empty string value" do
person = Person.new
person.name = ""
assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do
assert_deprecated do
person.errors.add_on_empty :name
end
end
end
test "add_on_blank generates message" do
person = Person.new
assert_called_with(person.errors, :generate_message, [:name, :blank, {}]) do
assert_deprecated do
person.errors.add_on_blank :name
end
end
end
test "add_on_blank generates message for multiple attributes" do
person = Person.new
expected_calls = [ [:name, :blank, {}], [:age, :blank, {}] ]
assert_called_with(person.errors, :generate_message, expected_calls) do
assert_deprecated do
person.errors.add_on_blank [:name, :age]
end
end
end
test "add_on_blank generates message with custom default message" do
person = Person.new
assert_called_with(person.errors, :generate_message, [:name, :blank, { message: "custom" }]) do
assert_deprecated do
person.errors.add_on_blank :name, message: "custom"
end
end
end
test "details returns added error detail" do test "details returns added error detail" do
person = Person.new person = Person.new
person.errors.add(:name, :invalid) person.errors.add(:name, :invalid)
......
...@@ -318,42 +318,6 @@ def test_validates_length_of_using_is_utf8 ...@@ -318,42 +318,6 @@ def test_validates_length_of_using_is_utf8
assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"] assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"]
end end
def test_validates_length_of_with_block
assert_deprecated do
Topic.validates_length_of(
:content,
minimum: 5,
too_short: "Your essay must be at least %{count} words.",
tokenizer: lambda { |str| str.scan(/\w+/) },
)
end
t = Topic.new(content: "this content should be long enough")
assert t.valid?
t.content = "not long enough"
assert t.invalid?
assert t.errors[:content].any?
assert_equal ["Your essay must be at least 5 words."], t.errors[:content]
end
def test_validates_length_of_with_symbol
assert_deprecated do
Topic.validates_length_of(
:content,
minimum: 5,
too_short: "Your essay must be at least %{count} words.",
tokenizer: :my_word_tokenizer,
)
end
t = Topic.new(content: "this content should be long enough")
assert t.valid?
t.content = "not long enough"
assert t.invalid?
assert t.errors[:content].any?
assert_equal ["Your essay must be at least 5 words."], t.errors[:content]
end
def test_validates_length_of_for_integer def test_validates_length_of_for_integer
Topic.validates_length_of(:approved, is: 4) Topic.validates_length_of(:approved, is: 4)
......
...@@ -36,8 +36,4 @@ def my_validation ...@@ -36,8 +36,4 @@ def my_validation
def my_validation_with_arg(attr) def my_validation_with_arg(attr)
errors.add attr, "is missing" unless send(attr) errors.add attr, "is missing" unless send(attr)
end end
def my_word_tokenizer(str)
str.scan(/\w+/)
end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册