mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
restriction shorthand symbols
This commit is contained in:
parent
924c6766c1
commit
82ede60985
20
README.rdoc
20
README.rdoc
@ -112,6 +112,26 @@ Using the I18n system to define new defaults:
|
||||
The %{restriction} signifies where the interpolation value for the restriction will be inserted.
|
||||
|
||||
|
||||
=== RESTRICTION OPTION SHORTHAND
|
||||
|
||||
It is common to restrict an attribute to being on or before the current time or current day.
|
||||
To specify this you need to use a lambda as an option value e.g. <tt>lambda { Time.now }</tt>.
|
||||
This can be tedious noise amongst your validations for something so common. To combat this the
|
||||
plugin allows you to use shorthand symbols for often used relative times or dates.
|
||||
|
||||
Just provide the symbol as the option value like so:
|
||||
|
||||
validates_date :birth_date, :on_or_before => :today
|
||||
|
||||
The :today symbol is evaluated as <tt>lambda { Date.today }.call</tt>. The :now and :today
|
||||
symbols are pre-configured. Configure your own like so:
|
||||
|
||||
# in the setup block
|
||||
config.restriction_shorthand_symbols.update(
|
||||
:yesterday => lambda { 1.day.ago }
|
||||
)
|
||||
|
||||
|
||||
=== DUMMY DATE FOR TIME TYPES
|
||||
|
||||
Given that Ruby has no support for a time-only type, all time type columns are evaluated
|
||||
|
||||
@ -26,6 +26,13 @@ module ValidatesTimeliness
|
||||
mattr_accessor :ignore_restriction_errors
|
||||
@@ignore_restriction_errors = defined?(Rails) ? !Rails.env.test? : false
|
||||
|
||||
# Shorthand time and date symbols for restrictions
|
||||
mattr_accessor :restriction_shorthand_symbols
|
||||
@@restriction_shorthand_symbols = {
|
||||
:now => lambda { Time.now },
|
||||
:today => lambda { Date.today }
|
||||
}
|
||||
|
||||
# Setup method for plugin configuration
|
||||
def self.setup
|
||||
yield self
|
||||
|
||||
@ -31,7 +31,11 @@ module ValidatesTimeliness
|
||||
when String
|
||||
value.to_time(:local)
|
||||
when Symbol
|
||||
evaluate_option_value(record.send(value), record)
|
||||
if !record.respond_to?(value) && restriction_shorthand?(value)
|
||||
ValidatesTimeliness.restriction_shorthand_symbols[value].call
|
||||
else
|
||||
evaluate_option_value(record.send(value), record)
|
||||
end
|
||||
when Proc
|
||||
result = value.arity > 0 ? value.call(record) : value.call
|
||||
evaluate_option_value(result, record)
|
||||
@ -40,5 +44,9 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
def restriction_shorthand?(symbol)
|
||||
ValidatesTimeliness.restriction_shorthand_symbols.keys.include?(symbol)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@ -150,5 +150,25 @@ describe ValidatesTimeliness::Conversion do
|
||||
person.birth_time = value
|
||||
evaluate_option_value(:birth_time, person).should == Time.mktime(2010,1,1,12,0,0)
|
||||
end
|
||||
|
||||
context "restriction shorthand" do
|
||||
before do
|
||||
Timecop.freeze(Time.mktime(2010, 1, 1, 0, 0, 0))
|
||||
end
|
||||
|
||||
it 'should evaluate :now as current time' do
|
||||
evaluate_option_value(:now, person).should == Time.now
|
||||
end
|
||||
|
||||
it 'should evaluate :today as current time' do
|
||||
evaluate_option_value(:today, person).should == Date.today
|
||||
end
|
||||
|
||||
it 'should not use shorthand if symbol if is record method' do
|
||||
time = 1.day.from_now
|
||||
person.stub!(:now).and_return(time)
|
||||
evaluate_option_value(:now, person).should == time
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user