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

ensure options on validations are passed into errors #232

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
2 changes: 1 addition & 1 deletion lib/validates_timeliness/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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.fetch(:"#{message}_message", options[:message]), restriction: value }
record.errors.add(attr_name, message, **message_options)
record.errors.add(attr_name, message, **options, **message_options)
end

def format_error_value(value)
Expand Down
21 changes: 21 additions & 0 deletions spec/validates_timeliness/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ class PersonWithFormatOption
end
end

describe "custom option" do
class PersonWithCustomOption
include TestModel
include TestModelShim
attribute :birth_date, :date
attribute :birth_time, :time
attribute :birth_datetime, :datetime
validates_date :birth_date, :format => 'dd-mm-yyyy', :custom_option => "custom option"
end

let(:person) { PersonWithCustomOption.new }

with_config(:use_plugin_parser, true)

it "should be included in the errors" do
person.birth_date = '1913-12-11'
person.valid?
expect(person.errors.first.options).to include(:custom_option => "custom option")
end
end

describe "restriction value errors" do
let(:person) { Person.new(:birth_date => Date.today) }

Expand Down