From 6b377a577ea7fa602573a51a75233d1cb22ca8d6 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sat, 10 Aug 2013 09:24:49 -0400 Subject: [PATCH] Validate the date format --- Gemfile | 1 + Gemfile.lock | 4 +++ app/models/workout.rb | 2 +- config/initializers/validates_timeliness.rb | 40 +++++++++++++++++++++ config/locales/validates_timeliness.en.yml | 16 +++++++++ spec/features/workouts_spec.rb | 5 +-- 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 config/initializers/validates_timeliness.rb create mode 100644 config/locales/validates_timeliness.en.yml diff --git a/Gemfile b/Gemfile index 3888a12..e84adbe 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ gem 'jquery-rails' gem 'jquery-ui-rails' gem 'jbuilder', '~> 1.2' gem 'thin' +gem 'validates_timeliness', '~> 3.0' group :doc do gem 'sdoc', require: false diff --git a/Gemfile.lock b/Gemfile.lock index c70f13f..9a62ef9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -168,6 +168,7 @@ GEM thread_safe (0.1.2) atomic tilt (1.4.1) + timeliness (0.3.7) treetop (1.4.14) polyglot polyglot (>= 0.3.1) @@ -175,6 +176,8 @@ GEM uglifier (2.1.2) execjs (>= 0.3.0) multi_json (~> 1.0, >= 1.0.2) + validates_timeliness (3.0.14) + timeliness (~> 0.3.6) xpath (2.0.0) nokogiri (~> 1.3) @@ -200,3 +203,4 @@ DEPENDENCIES sqlite3 thin uglifier (>= 1.3.0) + validates_timeliness (~> 3.0) diff --git a/app/models/workout.rb b/app/models/workout.rb index ccfe050..645e141 100644 --- a/app/models/workout.rb +++ b/app/models/workout.rb @@ -1,3 +1,3 @@ class Workout < ActiveRecord::Base - validates :date, :presence => true + validates_date :date end diff --git a/config/initializers/validates_timeliness.rb b/config/initializers/validates_timeliness.rb new file mode 100644 index 0000000..fbf9b41 --- /dev/null +++ b/config/initializers/validates_timeliness.rb @@ -0,0 +1,40 @@ +ValidatesTimeliness.setup do |config| + # Extend ORM/ODMs for full support (:active_record, :mongoid). + # config.extend_orms = [ :active_record ] + # + # Default timezone + # config.default_timezone = :utc + # + # Set the dummy date part for a time type values. + # config.dummy_date_for_time_type = [ 2000, 1, 1 ] + # + # Ignore errors when restriction options are evaluated + # config.ignore_restriction_errors = false + # + # Re-display invalid values in date/time selects + # config.enable_date_time_select_extension! + # + # Handle multiparameter date/time values strictly + # config.enable_multiparameter_extension! + # + # Shorthand date and time symbols for restrictions + # config.restriction_shorthand_symbols.update( + # :now => lambda { Time.current }, + # :today => lambda { Date.current } + # ) + # + # Use the plugin date/time parser which is stricter and extendable + # config.use_plugin_parser = false + # + # Add one or more formats making them valid. e.g. add_formats(:date, 'd(st|rd|th) of mmm, yyyy') + # config.parser.add_formats() + # + # Remove one or more formats making them invalid. e.g. remove_formats(:date, 'dd/mm/yyy') + # config.parser.remove_formats() + # + # Change the amiguous year threshold when parsing a 2 digit year + # config.parser.ambiguous_year_threshold = 30 + # + # Treat ambiguous dates, such as 01/02/1950, as a Non-US date. + # config.parser.remove_us_formats +end diff --git a/config/locales/validates_timeliness.en.yml b/config/locales/validates_timeliness.en.yml new file mode 100644 index 0000000..634c9da --- /dev/null +++ b/config/locales/validates_timeliness.en.yml @@ -0,0 +1,16 @@ +en: + errors: + messages: + invalid_date: "is not a valid date" + invalid_time: "is not a valid time" + invalid_datetime: "is not a valid datetime" + is_at: "must be at %{restriction}" + before: "must be before %{restriction}" + on_or_before: "must be on or before %{restriction}" + after: "must be after %{restriction}" + on_or_after: "must be on or after %{restriction}" + validates_timeliness: + error_value_formats: + date: '%Y-%m-%d' + time: '%H:%M:%S' + datetime: '%Y-%m-%d %H:%M:%S' diff --git a/spec/features/workouts_spec.rb b/spec/features/workouts_spec.rb index 02fc7a9..e57e327 100644 --- a/spec/features/workouts_spec.rb +++ b/spec/features/workouts_spec.rb @@ -79,12 +79,13 @@ describe "Workouts" do page.should have_content 'Workout created' end - it "should not allow a blank date" do + it "should only accept real dates" do visit workouts_path click_link 'Create' + fill_in 'Date', :with => '2013-13-13' click_button 'Create Workout' - page.should have_content "Date can't be blank" + page.should have_content "Date is not a valid date" end end