aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert2017-06-23 11:43:56 +0200
committerRobert2017-06-23 11:51:48 +0200
commit970954938043d8d73c4457ee2d91e22c0e422e65 (patch)
tree8c633f0437b0bd76c21d40d3bc7225230d8ce61f
parentd51985fc2a7c2138fd12cb9116ebf05d8b0e7dac (diff)
downloadchouette-core-970954938043d8d73c4457ee2d91e22c0e422e65.tar.bz2
JS for date validation
-rw-r--r--app/assets/javascripts/calendars.coffee35
-rw-r--r--app/controllers/calendars_controller.rb2
-rw-r--r--app/models/calendar/calendar_date.rb2
-rw-r--r--spec/javascripts/calendars/index_spec.coffee0
-rw-r--r--spec/javascripts/spec_helper.coffee32
5 files changed, 69 insertions, 2 deletions
diff --git a/app/assets/javascripts/calendars.coffee b/app/assets/javascripts/calendars.coffee
new file mode 100644
index 000000000..ac5330cc8
--- /dev/null
+++ b/app/assets/javascripts/calendars.coffee
@@ -0,0 +1,35 @@
+
+isEmpty = (x) -> x == ''
+
+daySelector = '#q_contains_date_3i'
+monthSelector = '#q_contains_date_2i'
+yearSelector = '#q_contains_date_1i'
+
+dateSelectors = [ daySelector, monthSelector, yearSelector ]
+
+checkDate = (args...) ->
+ vals = args.map((ele) -> ele.val())
+ return if vals.find( isEmpty ) == "" # no checking needed, no checking possible yet
+
+ dates = vals.map (x) -> parseInt(x)
+ return if isLegalDate(dates)
+ $('.alert').text('illegal date')
+ $('.alert').show()
+
+legalDaysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+
+isLeapYear = (year) ->
+ (year % 4 == 0) && ((year % 100 != 0) || (year % 1000 == 0))
+
+isLegalDate = (dates) ->
+ [day, month, year] = dates
+ return true if legalDaysPerMonth[month-1] >= day
+ return true if day == 29 && isLeapYear(year)
+ false
+
+defineChangeHandler = (selector) ->
+ $(selector).on 'change', ->
+ checkDate $(daySelector), $(monthSelector), $(yearSelector)
+
+$ ->
+ defineChangeHandler selector for selector in dateSelectors
diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb
index a2bfe9b63..415ebcaeb 100644
--- a/app/controllers/calendars_controller.rb
+++ b/app/controllers/calendars_controller.rb
@@ -50,7 +50,7 @@ class CalendarsController < BreadcrumbController
date << params[:q][key].to_i
params[:q].delete(key)
end
- params[:q]['contains_date'] = Date.new(*date)
+ params[:q]['contains_date'] = Date.new(*date) rescue nil
end
end
diff --git a/app/models/calendar/calendar_date.rb b/app/models/calendar/calendar_date.rb
index 7537f562d..cfee95a25 100644
--- a/app/models/calendar/calendar_date.rb
+++ b/app/models/calendar/calendar_date.rb
@@ -3,7 +3,7 @@ class Calendar
module IllegalDate
attr_reader :year, :month, :day
- def to_s
+ def to_s(*_args)
"%d-%02d-%02d" % [year, month, day]
end
end
diff --git a/spec/javascripts/calendars/index_spec.coffee b/spec/javascripts/calendars/index_spec.coffee
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/spec/javascripts/calendars/index_spec.coffee
diff --git a/spec/javascripts/spec_helper.coffee b/spec/javascripts/spec_helper.coffee
new file mode 100644
index 000000000..9ff516885
--- /dev/null
+++ b/spec/javascripts/spec_helper.coffee
@@ -0,0 +1,32 @@
+# Teaspoon includes some support files, but you can use anything from your own support path too.
+# require support/jasmine-jquery-1.7.0
+# require support/jasmine-jquery-2.0.0
+# require support/jasmine-jquery-2.1.0
+# require support/sinon
+# require support/your-support-file
+#
+# PhantomJS (Teaspoons default driver) doesn't have support for Function.prototype.bind, which has caused confusion.
+# Use this polyfill to avoid the confusion.
+#= require support/phantomjs-shims
+#
+# You can require your own javascript files here. By default this will include everything in application, however you
+# may get better load performance if you require the specific files that are being used in the spec that tests them.
+#= require application
+#
+# Deferring execution
+# If you're using CommonJS, RequireJS or some other asynchronous library you can defer execution. Call
+# Teaspoon.execute() after everything has been loaded. Simple example of a timeout:
+#
+# Teaspoon.defer = true
+# setTimeout(Teaspoon.execute, 1000)
+#
+# Matching files
+# By default Teaspoon will look for files that match _spec.{js,js.coffee,.coffee}. Add a filename_spec.js file in your
+# spec path and it'll be included in the default suite automatically. If you want to customize suites, check out the
+# configuration in teaspoon_env.rb
+#
+# Manifest
+# If you'd rather require your spec files manually (to control order for instance) you can disable the suite matcher in
+# the configuration and use this file as a manifest.
+#
+# For more information: http://github.com/modeset/teaspoon