Skip to content

Commit a476545

Browse files
committed
Ability to include custom restriction variable value and add the error message to base.
1 parent 35caf36 commit a476545

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

lib/validates_timeliness/validator.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ def validate_restrictions(record, attr_name, value)
9090
end
9191

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

9898
def format_error_value(value)
@@ -110,6 +110,25 @@ def timezone_aware?(record, attr_name)
110110
record.class.timeliness_attribute_timezone_aware?(attr_name)
111111
end
112112

113+
private
114+
115+
def determine_value(value, record)
116+
case value
117+
when Proc
118+
value.arity > 0 ? value.call(record) : value.call
119+
else
120+
value
121+
end
122+
end
123+
124+
def add_errors_accordingly(record, attr_name, message, message_options)
125+
if options[:add_to_base]
126+
record.errors[:base] << record.errors.generate_message(attr_name, message, message_options)
127+
else
128+
record.errors.add(attr_name, message, message_options)
129+
end
130+
end
131+
113132
end
114133
end
115134

spec/validates_timeliness/validator_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,43 @@
140140
end
141141
end
142142

143+
context ":restriction_value option" do
144+
describe "when not Proc" do
145+
before do
146+
Person.validates_datetime :birth_datetime, :on_or_before => Time.now, :restriction_value => 5000
147+
end
148+
let(:person) { Person.new(:birth_datetime => Time.now + 1000.seconds) }
149+
it "should obey restriction_value" do
150+
person.valid?
151+
person.errors[:birth_datetime].should eq(["must be on or before 5000"])
152+
end
153+
end
154+
describe "when Proc" do
155+
before do
156+
Person.validates_datetime :birth_datetime, :on_or_before => Time.now, :restriction_value => Proc.new{ "dummy restriction value" }
157+
end
158+
let(:person) { Person.new(:birth_datetime => Time.now + 1000.seconds) }
159+
it "should obey restriction_value" do
160+
person.valid?
161+
person.errors[:birth_datetime].should eq(["must be on or before dummy restriction value"])
162+
end
163+
end
164+
end
165+
166+
context ":add_to_base option" do
167+
before do
168+
Person.validates_datetime :birth_datetime,
169+
:on_or_before => Time.now,
170+
:add_to_base => true
171+
end
172+
let(:person) { Person.new(:birth_datetime => Time.now + 1000.seconds) }
173+
it "should obey restriction_value" do
174+
person.valid?
175+
person.errors[:birth_datetime].should eq([])
176+
person.errors[:base].should eq(["must be on or before 2010-01-01 00:00:00"])
177+
end
178+
end
179+
143180
describe ":format option" do
144181
class PersonWithFormatOption
145182
include TestModel

0 commit comments

Comments
 (0)