Allow the validator to take in the message option too. This is useful in those scenarios where we have a set of generic keys which we need to show.

This would eliminate the need to keeping multiple copies for the default keys set defined by gem.

    The following seems more apt than the later one:

    ```
    validates_date :start_at, on_or_after: :today, message: 'should not be in past'
    ```

    ```
    validates_date :start_at, on_or_after: :today, on_or_after_message: 'should not be in past'
    ```
This commit is contained in:
Aditya Kapoor 2017-03-22 13:21:59 +05:30
parent 35caf3638e
commit e9c9914c4f
2 changed files with 15 additions and 1 deletions

View File

@ -91,7 +91,7 @@ module ValidatesTimeliness
def add_error(record, attr_name, message, value=nil)
value = format_error_value(value) if value
message_options = { :message => options[:"#{message}_message"], :restriction => value }
message_options = { :message => (options[:"#{message}_message"] || options[:message]), :restriction => value }
record.errors.add(attr_name, message, message_options)
end

View File

@ -87,6 +87,20 @@ RSpec.describe ValidatesTimeliness::Validator do
end
end
describe ':message options' do
it 'should allow message option too' do
Person.validates_date :birth_date, on_or_after: :today, message: 'cannot be in past'
invalid!(:birth_date, Date.today - 5.days, 'cannot be in past')
valid!(:birth_date, Date.today)
end
it 'should first allow the defined message' do
Person.validates_date :birth_date, on_or_after: :today, on_or_after_message: 'cannot be in past', message: 'dummy message'
invalid!(:birth_date, Date.today - 5.days, 'cannot be in past')
valid!(:birth_date, Date.today)
end
end
describe ":between option" do
describe "array value" do
it 'should be split option into :on_or_after and :on_or_before values' do