Skip to content

Commit

Permalink
Ability to include custom restriction variable value and add the erro…
Browse files Browse the repository at this point in the history
…r message to base.
  • Loading branch information
aditya-kapoor committed Mar 22, 2017
1 parent 35caf36 commit a476545
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
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

0 comments on commit a476545

Please sign in to comment.