Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interpolation value on messages #1322

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/shoulda/matchers/active_model/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ def messages

def matched_messages
if @expected_message
messages.grep(@expected_message)
messages.grep(interpolate_expected_message)
else
messages
end
end

def interpolate_expected_message
return @expected_message unless @expected_message.is_a?(String)

@expected_message.gsub(/%{value}/, record.send(attribute).to_s)
end

def captured_range_error?
!!@captured_range_error
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
it 'matches ensuring the correct range and messages' do
expect_to_match_ensuring_range_and_messages(2..5, 2, 5)
expect_to_match_ensuring_range_and_messages(2...5, 2, 4)
expect_to_match_ensuring_range_and_messages(2..5, 2, 5, "%{value}")
end
end

Expand Down Expand Up @@ -1083,17 +1084,17 @@ def expect_not_to_match_in_range(builder, range)
end
end

def expect_to_match_ensuring_range_and_messages(range, low_value, high_value)
low_message = 'too low'
high_message = 'too high'
def expect_to_match_ensuring_range_and_messages(range, low_value, high_value, with_value=nil)
low_message = menssage_constructor('low', "#{with_value}")
high_message = menssage_constructor('high', "#{with_value}")

builder = build_object custom_validation: -> (object, attribute) {
value = object.public_send(attribute)

if value < low_value
object.errors.add(attribute, low_message)
object.errors.add(attribute, add_value_to_message(low_message, value))
elsif value > high_value
object.errors.add(attribute, high_message)
object.errors.add(attribute, add_value_to_message(high_message, value))
end
}

Expand All @@ -1105,6 +1106,14 @@ def expect_to_match_ensuring_range_and_messages(range, low_value, high_value)
end
end

def add_value_to_message(message, value)
message.gsub(/%{value}/, value.to_s)
end

def menssage_constructor(string, value)
Copy link

@varunlalan varunlalan Jan 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo message_constructor

"#{value} too #{string}"
end

def validation_matcher_scenario_args
super.deep_merge(matcher_name: :validate_inclusion_of)
end
Expand Down