From a4765458cb3afe61398572be36498e8d20b42336 Mon Sep 17 00:00:00 2001 From: Aditya Kapoor Date: Wed, 22 Mar 2017 13:24:10 +0530 Subject: [PATCH] Ability to include custom restriction variable value and add the error message to base. --- lib/validates_timeliness/validator.rb | 23 +++++++++++-- spec/validates_timeliness/validator_spec.rb | 37 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 0e2c8b44..ed64b4c6 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -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) @@ -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 diff --git a/spec/validates_timeliness/validator_spec.rb b/spec/validates_timeliness/validator_spec.rb index 69ec35a4..a17a5225 100644 --- a/spec/validates_timeliness/validator_spec.rb +++ b/spec/validates_timeliness/validator_spec.rb @@ -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