Truncate long values in descriptions. Fixes #1041

This commit is contained in:
Isaac Betesh 2017-07-25 16:12:11 -04:00 committed by Elliot Winkler
parent 390c2ca161
commit 8bf411fd44
2 changed files with 14 additions and 1 deletions

View File

@ -4,6 +4,8 @@ module Shoulda
module Matchers
# @private
module Util
MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY = 500
def self.deconstantize(path)
if defined?(ActiveSupport::Inflector) &&
ActiveSupport::Inflector.respond_to?(:deconstantize)
@ -47,7 +49,12 @@ module Shoulda
when Range
inspect_range(value)
else
"#{value.inspect}"
inspected_value = value.inspect
if inspected_value.length > MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY
"#{inspected_value[0, MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY]}..."
else
"#{inspected_value}"
end
end
end

View File

@ -41,6 +41,12 @@ describe Shoulda::Matchers::ActiveModel::AllowValueMatcher, type: :model do
)
end
end
it 'truncates the description when long' do
matcher = allow_value("A" * 10000).for(:baz)
expect(matcher.description).to eq "allow :baz to be \"#{"A" * 499}..."
end
end
describe '#_after_setting_value' do