Skip to content

Commit

Permalink
Merge pull request #152 from aditya-kapoor/allow-generic-msg
Browse files Browse the repository at this point in the history
Allow the validator to take in the `message` option too.
  • Loading branch information
adzap committed May 13, 2018
2 parents fc2af73 + da473b7 commit 43554d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/validates_timeliness/validator.rb
Expand Up @@ -91,7 +91,7 @@ def validate_restrictions(record, attr_name, value)

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.fetch(:"#{message}_message", options[:message]), :restriction => value }
record.errors.add(attr_name, message, message_options)
end

Expand Down
14 changes: 14 additions & 0 deletions spec/validates_timeliness/validator_spec.rb
Expand Up @@ -87,6 +87,20 @@
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
Expand Down

0 comments on commit 43554d8

Please sign in to comment.