Deprecate ensure_inclusion_of
This commit is contained in:
parent
06070f9400
commit
15d0cee93b
|
@ -217,7 +217,16 @@ module Shoulda
|
|||
def validate_inclusion_of(attr)
|
||||
ValidateInclusionOfMatcher.new(attr)
|
||||
end
|
||||
alias_method :ensure_inclusion_of, :validate_inclusion_of
|
||||
|
||||
# @deprecated Use {#validate_inclusion_of} instead.
|
||||
# @return [ValidateInclusionOfMatcher]
|
||||
def ensure_inclusion_of(attr)
|
||||
Shoulda::Matchers.warn_about_deprecated_method(
|
||||
:ensure_inclusion_of,
|
||||
:validate_inclusion_of
|
||||
)
|
||||
validate_inclusion_of(attr)
|
||||
end
|
||||
|
||||
# @private
|
||||
class ValidateInclusionOfMatcher < ValidationMatcher
|
||||
|
|
|
@ -1,8 +1,36 @@
|
|||
module Shoulda
|
||||
module Matchers
|
||||
TERMINAL_MAX_WIDTH = 72
|
||||
|
||||
# @private
|
||||
def self.warn(msg)
|
||||
Kernel.warn "Warning from shoulda-matchers:\n\n#{msg}"
|
||||
def self.warn(message)
|
||||
header = "Warning from shoulda-matchers:"
|
||||
divider = "*" * TERMINAL_MAX_WIDTH
|
||||
wrapped_message = word_wrap(message, TERMINAL_MAX_WIDTH)
|
||||
full_message = [
|
||||
divider,
|
||||
[header, wrapped_message.strip].join("\n\n"),
|
||||
divider
|
||||
].join("\n")
|
||||
|
||||
Kernel.warn(full_message)
|
||||
end
|
||||
|
||||
# @private
|
||||
def self.warn_about_deprecated_method(old_method, new_method)
|
||||
warn <<EOT
|
||||
#{old_method} is deprecated and will be removed in the next major
|
||||
release. Please use #{new_method} instead.
|
||||
EOT
|
||||
end
|
||||
|
||||
# Source: <https://www.ruby-forum.com/topic/57805>
|
||||
# @private
|
||||
def self.word_wrap(text, width=80)
|
||||
text.
|
||||
gsub(/\n+/, " ").
|
||||
gsub( /(\S{#{width}})(?=\S)/, '\1 ' ).
|
||||
gsub( /(.{1,#{width}})(?:\s+|$)/, "\\1\n" )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,9 +3,17 @@ require 'spec_helper'
|
|||
describe Shoulda::Matchers::ActiveModel do
|
||||
describe '#ensure_inclusion_of' do
|
||||
it 'is aliased to #validate_inclusion_of' do
|
||||
expect(method(:ensure_inclusion_of)).to eq(method(:validate_inclusion_of))
|
||||
matchers.expects(:validate_inclusion_of).with(:attr)
|
||||
|
||||
silence_warnings do
|
||||
matchers.ensure_inclusion_of(:attr)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def matchers
|
||||
@_matchers ||= Object.new.extend(described_class)
|
||||
end
|
||||
end
|
||||
|
||||
describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher do
|
||||
|
|
Loading…
Reference in New Issue