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

Ability to include custom restriction variable value and add the error message to base. #151

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
23 changes: 21 additions & 2 deletions lib/validates_timeliness/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ def validate_restrictions(record, attr_name, value)
end

def add_error(record, attr_name, message, value=nil)
value = format_error_value(value) if value
value = determine_value(options[:restriction_value], record) || (format_error_value(value) if value)
message_options = { :message => options[:"#{message}_message"], :restriction => value }
record.errors.add(attr_name, message, message_options)
add_errors_accordingly(record, attr_name, message, message_options)
end

def format_error_value(value)
Expand All @@ -110,6 +110,25 @@ def timezone_aware?(record, attr_name)
record.class.timeliness_attribute_timezone_aware?(attr_name)
end

private

def determine_value(value, record)
case value
when Proc
value.arity > 0 ? value.call(record) : value.call
else
value
end
end

def add_errors_accordingly(record, attr_name, message, message_options)
if options[:add_to_base]
record.errors[:base] << record.errors.generate_message(attr_name, message, message_options)
else
record.errors.add(attr_name, message, message_options)
end
end

end
end

Expand Down
37 changes: 37 additions & 0 deletions spec/validates_timeliness/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,43 @@
end
end

context ":restriction_value option" do
describe "when not Proc" do
before do
Person.validates_datetime :birth_datetime, :on_or_before => Time.now, :restriction_value => 5000
end
let(:person) { Person.new(:birth_datetime => Time.now + 1000.seconds) }
it "should obey restriction_value" do
person.valid?
person.errors[:birth_datetime].should eq(["must be on or before 5000"])
end
end
describe "when Proc" do
before do
Person.validates_datetime :birth_datetime, :on_or_before => Time.now, :restriction_value => Proc.new{ "dummy restriction value" }
end
let(:person) { Person.new(:birth_datetime => Time.now + 1000.seconds) }
it "should obey restriction_value" do
person.valid?
person.errors[:birth_datetime].should eq(["must be on or before dummy restriction value"])
end
end
end

context ":add_to_base option" do
before do
Person.validates_datetime :birth_datetime,
:on_or_before => Time.now,
:add_to_base => true
end
let(:person) { Person.new(:birth_datetime => Time.now + 1000.seconds) }
it "should obey restriction_value" do
person.valid?
person.errors[:birth_datetime].should eq([])
person.errors[:base].should eq(["must be on or before 2010-01-01 00:00:00"])
end
end

describe ":format option" do
class PersonWithFormatOption
include TestModel
Expand Down