diff --git a/README.rdoc b/README.rdoc index 0b966eb..6364857 100644 --- a/README.rdoc +++ b/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. lambda { Time.now }. +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 lambda { Date.today }.call. 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 diff --git a/lib/validates_timeliness.rb b/lib/validates_timeliness.rb index 923d6e5..9b08d39 100644 --- a/lib/validates_timeliness.rb +++ b/lib/validates_timeliness.rb @@ -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 diff --git a/lib/validates_timeliness/conversion.rb b/lib/validates_timeliness/conversion.rb index 4e77a06..b47dae5 100644 --- a/lib/validates_timeliness/conversion.rb +++ b/lib/validates_timeliness/conversion.rb @@ -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 diff --git a/spec/validates_timeliness/conversion_spec.rb b/spec/validates_timeliness/conversion_spec.rb index 4370146..4765f89 100644 --- a/spec/validates_timeliness/conversion_spec.rb +++ b/spec/validates_timeliness/conversion_spec.rb @@ -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