From d22aa94b8942e74f98fd40b6743790a5d6f0230d Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Wed, 14 May 2008 08:54:50 +1000 Subject: [PATCH] changed version check to Rails constant as AR VERSION not loaded by default changed time component validation to use dummy date parts to avoid pre epoch issues --- lib/validates_timeliness/date_helper.rb | 46 +++++++++++++++++++++++++ spec/date_helper_spec.rb | 11 ++++++ spec/spec_helper.rb | 4 +-- 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 lib/validates_timeliness/date_helper.rb create mode 100644 spec/date_helper_spec.rb diff --git a/lib/validates_timeliness/date_helper.rb b/lib/validates_timeliness/date_helper.rb new file mode 100644 index 0000000..9e86463 --- /dev/null +++ b/lib/validates_timeliness/date_helper.rb @@ -0,0 +1,46 @@ +# This module intercepts the date and time select helpers to allow the +# attribute value before type cast to be used as in the select helpers. +# This means that an invalid date or time will be redisplayed rather than the +# implicitly converted value which occurs by default with Rails. +module ValidatesTimeliness + module DateHelper + + class InstanceTag + TimelinessDateTime = Struct.new(:year, :day, :month, :hour, :min, :sec) + + def to_date_select_tag_with_timeliness(options = {}, html_options = {}) + @timeliness_date_or_time_tag = true + date_or_time_select_without_timeliness(options.merge(:discard_hour => true), html_options) + end + alias_method_chain :to_date_select_tag, :timeliness + + def to_time_select_tag_with_timeliness(options = {}, html_options = {}) + @timeliness_date_or_time_tag = true + date_or_time_select_without_timeliness(options.merge(:discard_year => true, :discard_month => true), html_options) + end + alias_method_chain :to_time_select_tag, :timeliness + + def to_datetime_select_tag_with_timeliness(options = {}, html_options = {}) + @timeliness_date_or_time_tag = true + date_or_time_select_without_timeliness(options, html_options) + end + alias_method_chain :to_date_select_tag, :timeliness + + def value_with_timeliness(object) + return value_without_timeliness(object) unless @timeliness_date_or_time_tag + + raw_value = value_before_type_case(object) + + if raw_value.acts_as?(:time) || raw_value.is_a?(Date) + return value_without_timeliness(object) + end + + time_array = ParseDate.parsedate(raw_value) + + TimelinessDateTime.new(time_array) + end + alias_method_chain :value, :timeliness + + end + end +end diff --git a/spec/date_helper_spec.rb b/spec/date_helper_spec.rb new file mode 100644 index 0000000..dc19bf8 --- /dev/null +++ b/spec/date_helper_spec.rb @@ -0,0 +1,11 @@ +#require File.dirname(__FILE__) + '/spec_helper' + +#describe ValidatesTimeliness::DateHelper::InstanceTag do + +# it "should return struct for time string" do +# object = mock("Model", :birth_date_and_time_before_type_cast => "2008-01-01 12:00:00") +# struct = value_with_timeliness(object) +# struct.should be_kind_of(DateTimeStruct) +# end + +#end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 518fe4b..6abd8ed 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,13 +9,13 @@ if File.exists?(File.dirname(__FILE__) + '/../../../../vendor/rails') require 'activerecord/lib/active_record' require 'railties/lib/rails/version' - puts "Using vendored ActiveRecord version #{ActiveRecord::VERSION::STRING}" + puts "Using vendored Rails version #{Rails::VERSION::STRING}" else require 'active_record' require 'active_record/version' require 'rails/version' - puts "Using gem ActiveRecord version #{ActiveRecord::VERSION::STRING}" + puts "Using gem Rails version #{Rails::VERSION::STRING}" end require 'validates_timeliness'