gov_uk_date_fields 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/lib/gov_uk_date_fields/acts_as_gov_uk_date.rb +122 -0
  5. data/lib/gov_uk_date_fields/form_builder.rb +12 -0
  6. data/lib/gov_uk_date_fields/form_date.rb +127 -0
  7. data/lib/gov_uk_date_fields/form_fields.rb +49 -0
  8. data/lib/gov_uk_date_fields/gov_uk_date_fields.rb +2 -0
  9. data/lib/gov_uk_date_fields/version.rb +3 -0
  10. data/lib/gov_uk_date_fields.rb +7 -0
  11. data/lib/tasks/gov_uk_date_fields_tasks.rake +4 -0
  12. data/test/dummy/README.rdoc +28 -0
  13. data/test/dummy/Rakefile +6 -0
  14. data/test/dummy/app/assets/javascripts/application.js +13 -0
  15. data/test/dummy/app/assets/javascripts/employees.js +2 -0
  16. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  17. data/test/dummy/app/assets/stylesheets/employees.css +4 -0
  18. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  19. data/test/dummy/app/controllers/application_controller.rb +5 -0
  20. data/test/dummy/app/controllers/employees_controller.rb +58 -0
  21. data/test/dummy/app/helpers/application_helper.rb +2 -0
  22. data/test/dummy/app/helpers/employees_helper.rb +2 -0
  23. data/test/dummy/app/models/employee.rb +6 -0
  24. data/test/dummy/app/views/employees/_form.html.erb +29 -0
  25. data/test/dummy/app/views/employees/edit.html.erb +6 -0
  26. data/test/dummy/app/views/employees/index.html.erb +31 -0
  27. data/test/dummy/app/views/employees/new.html.erb +5 -0
  28. data/test/dummy/app/views/employees/show.html.erb +19 -0
  29. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  30. data/test/dummy/bin/bundle +3 -0
  31. data/test/dummy/bin/rails +4 -0
  32. data/test/dummy/bin/rake +4 -0
  33. data/test/dummy/bin/setup +29 -0
  34. data/test/dummy/config/application.rb +25 -0
  35. data/test/dummy/config/boot.rb +5 -0
  36. data/test/dummy/config/database.yml +85 -0
  37. data/test/dummy/config/environment.rb +5 -0
  38. data/test/dummy/config/environments/development.rb +41 -0
  39. data/test/dummy/config/environments/production.rb +79 -0
  40. data/test/dummy/config/environments/test.rb +42 -0
  41. data/test/dummy/config/initializers/assets.rb +11 -0
  42. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  44. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/test/dummy/config/initializers/inflections.rb +16 -0
  46. data/test/dummy/config/initializers/mime_types.rb +4 -0
  47. data/test/dummy/config/initializers/session_store.rb +3 -0
  48. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/test/dummy/config/locales/en.yml +23 -0
  50. data/test/dummy/config/routes.rb +57 -0
  51. data/test/dummy/config/secrets.yml +22 -0
  52. data/test/dummy/config.ru +4 -0
  53. data/test/dummy/db/migrate/20150901103114_create_employees.rb +11 -0
  54. data/test/dummy/db/schema.rb +27 -0
  55. data/test/dummy/log/development.log +995 -0
  56. data/test/dummy/log/test.log +15943 -0
  57. data/test/dummy/public/404.html +67 -0
  58. data/test/dummy/public/422.html +67 -0
  59. data/test/dummy/public/500.html +66 -0
  60. data/test/dummy/public/favicon.ico +0 -0
  61. data/test/dummy/test/controllers/employees_controller_test.rb +72 -0
  62. data/test/dummy/test/fixtures/employees.yml +11 -0
  63. data/test/dummy/test/models/employee_test.rb +126 -0
  64. data/test/dummy/test/models/form_date_test.rb +91 -0
  65. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/3Btcwr72V3UVSQF4sqr__CCT-XdxN0CTruFn7z9Y_XU.cache +2 -0
  66. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/4OGvKM5VGFrmfgpNVn-OVfpy9d666yTUyc-FSiWvwYo.cache +0 -0
  67. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/54EGGKsdgfn0vxxv75SqN98eDhg-ilqjdIkfD08lvrI.cache +1 -0
  68. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/5Lly_CA8DZvPhQV2jDQx-Y6P_y3Ygra9t5jfSlGhHDA.cache +2 -0
  69. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/AAh5PqvwSFjiY74WrVS1iZDEIaV1Esq-U2wnk7iS4us.cache +0 -0
  70. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/AUA1UWYic4L73QsfyqeOPHldlfFq_8j9hAToBfr-f1Q.cache +1 -0
  71. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/AX3L2fGpRF9VevxsOcMT3K8MFiRGjRX81JBxvrH7IIc.cache +2 -0
  72. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/DWiSprTdmJSiF4eYqRLDfLfWLyF-m0Mk3TOoLQXKKW0.cache +2 -0
  73. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/DmmfrCpXtt74Hr6NO54lxyOCDv6klnDyBqeDFR7oDU8.cache +2 -0
  74. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/EQgy9RTV-4e0cBODgHM06wYW7K5ZQPT7QCEd9rw3WOM.cache +0 -0
  75. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/GeZkOF3Y0RVsr_bLJpt8e98qLe6tl2fanP3g8Au3Ki4.cache +1 -0
  76. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/MnCiEhJG594IyEZWxHbFmmj3VVVPj_s6mBDi22R6d4g.cache +0 -0
  77. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/OI6uxGcnsKavdWTtwDAasU3wPx8QXhzBgV0X2n1KjMQ.cache +2 -0
  78. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/QZXYupcseg74D7COWs8-uMvEFMzcF0D2kyZbe9TQ77s.cache +1 -0
  79. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/RLkYXASUya0cLJBhEdan7n0Pd7bxb0bB0Fisje2uDAo.cache +0 -0
  80. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/SOd3-ZZMlfAqcde7u4GC6nyLGyBlZn5GOsNbUpmTKYw.cache +1 -0
  81. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/SRehtTwxOp9802ZoUXpvNaoiTUAAop3BKwD0zbFqDtE.cache +2 -0
  82. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/X9Q9imgKEWoA7sXby5p60U-hJ2dz87Ml_BLtvx3zhic.cache +1 -0
  83. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/ZIi2WsAMg6MivpM37KPVAEt0WF2kOpptOZnB8I13ypQ.cache +0 -0
  84. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/aCaKUaA2zFHYb85d3HsEaLzqqcySgu1RNprCoAsVud0.cache +1 -0
  85. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/aGW0bSYiiwQK9bzVhmWulwv3CPsg3QsaBf3OmDxJgow.cache +0 -0
  86. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/hZi1k6tpxxCGYxRe7zY74ItcOI8gZrREOpGuA8JSpGg.cache +2 -0
  87. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/ihA_BDp1Rs5WPDQas3L0pgMqmZCcgqozdQxu48rJb68.cache +1 -0
  88. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/kTqBP8zzCc8f2JqGQTi3DXs7nGsbgDuRg0jrPG8ZNPk.cache +1 -0
  89. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/oV4rrfh_yARJFyFf415eIZDk0XSRN6pCO9JMJFhb0RI.cache +1 -0
  90. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/pEhaat2KBd5SrT7szC_8R1_6hK17FTpvoRFkmCRSD3M.cache +2 -0
  91. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/tYWKJ-uJLzUiu_GSiw8U9c-NoKsmluXyJoybViJqqSo.cache +1 -0
  92. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/uHgfxDUpQd144IiQx-xG6kwowy8G1sR4iyD9DNL0tC0.cache +1 -0
  93. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/1AsHbcbPJPOX9onZq50MxoxSn1pMnQpGVvG_UYQ13wo.cache +0 -0
  94. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/3Btcwr72V3UVSQF4sqr__CCT-XdxN0CTruFn7z9Y_XU.cache +2 -0
  95. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/5Lly_CA8DZvPhQV2jDQx-Y6P_y3Ygra9t5jfSlGhHDA.cache +2 -0
  96. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/67JJWCc_sYvRYPdITY7j_FNQfhBfvZo5G1sv2dZxIto.cache +1 -0
  97. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/AUA1UWYic4L73QsfyqeOPHldlfFq_8j9hAToBfr-f1Q.cache +1 -0
  98. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/AX3L2fGpRF9VevxsOcMT3K8MFiRGjRX81JBxvrH7IIc.cache +2 -0
  99. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/DNMjaCc-VmsemKVl4YidF-CXIU3genqy7Al84AF5ElQ.cache +1 -0
  100. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/DWiSprTdmJSiF4eYqRLDfLfWLyF-m0Mk3TOoLQXKKW0.cache +2 -0
  101. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/DmmfrCpXtt74Hr6NO54lxyOCDv6klnDyBqeDFR7oDU8.cache +2 -0
  102. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/EkdXohJZf4lGY8JzYoG_7LRxdszy4Swv_1-52AbtQWA.cache +0 -0
  103. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/FqGh56sHucOsjlGrWDO9yNwycTG0LgUKZ16k23kduns.cache +0 -0
  104. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/GeZkOF3Y0RVsr_bLJpt8e98qLe6tl2fanP3g8Au3Ki4.cache +1 -0
  105. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/OI6uxGcnsKavdWTtwDAasU3wPx8QXhzBgV0X2n1KjMQ.cache +2 -0
  106. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/PlM8l5Qld5XuE05WoFYFeHkdTOQlgQDeWcqChn7HrHY.cache +1 -0
  107. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/SRehtTwxOp9802ZoUXpvNaoiTUAAop3BKwD0zbFqDtE.cache +2 -0
  108. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/SrsOoZyuToCzWj-6-unzyuiaqTY_AaQ6OkmF1__p2pA.cache +1 -0
  109. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/Ve3Ff6BHeMiuu3shpU83x1W5kvA_9uYsJb4QTBpa_cg.cache +0 -0
  110. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/gGf5sYnraHtF9zAKHfpmAFfXs91eHz803uWElSZo8hU.cache +1 -0
  111. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/gQ3aDX081uviJlrMPWuDlBeI9mU60eQkKBT_tdRmfHA.cache +0 -0
  112. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/hZi1k6tpxxCGYxRe7zY74ItcOI8gZrREOpGuA8JSpGg.cache +2 -0
  113. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/ihA_BDp1Rs5WPDQas3L0pgMqmZCcgqozdQxu48rJb68.cache +1 -0
  114. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/pEhaat2KBd5SrT7szC_8R1_6hK17FTpvoRFkmCRSD3M.cache +2 -0
  115. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/qw9GAqewo2ndJbMp6cn6aiyI5zrb6Dc2AnBVBmpTAO8.cache +1 -0
  116. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/sjCpIVBR5YqsLCchRCDqeG_WK3Jn7Q3nlWVKNIwerDw.cache +0 -0
  117. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/tYWKJ-uJLzUiu_GSiw8U9c-NoKsmluXyJoybViJqqSo.cache +1 -0
  118. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/uHgfxDUpQd144IiQx-xG6kwowy8G1sR4iyD9DNL0tC0.cache +1 -0
  119. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/uk3t8-0_vMYlXEFPI-zH6U2ONI0NwBrJJrQM2yq_fjQ.cache +0 -0
  120. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/zwMKri38-QDk453A_5RE9PYaTcHIoT73wl79q5P0qfI.cache +1 -0
  121. data/test/gov_uk_date_fields_test.rb +7 -0
  122. data/test/test_helper.rb +31 -0
  123. metadata +306 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 34866dac045629eb836f99aa75a2517669e7e5d6
4
+ data.tar.gz: 1fbb68fa5675ea1defa9b7b22a2e94c8b1ae64d0
5
+ SHA512:
6
+ metadata.gz: 7edae5e8273014df61c7a37b5f80866815e9dfaa812c1c0acbd2b2a68f486af5364d56cb906d158ad0ae06831f90ce63508b99e8c29eaadfa89be74725bbdb77
7
+ data.tar.gz: 61f6b85a1eeae54ddc8fb24a5692c026326515f925b0724664bd0d2d4ea943e38dbe4e31a4c970eaafffa0af2ae947ed4668089127e7d42da79b965adf3323c7
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Stephen Richards
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'GovUkDateFields'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,122 @@
1
+ module GovUkDateFields
2
+ module ActsAsGovUkDate
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_gov_uk_date(*date_fields)
10
+
11
+ validate :validate_gov_uk_dates
12
+
13
+ after_initialize :populate_gov_uk_dates
14
+
15
+ cattr_accessor :_gov_uk_dates
16
+ self._gov_uk_dates = date_fields
17
+
18
+
19
+ # call valid? on each of the gov_uk date fields, and add error messages
20
+ # into the errors hash if not valid
21
+ #
22
+ define_method(:validate_gov_uk_dates) do
23
+ date_fields.each do |date_field|
24
+ unless self.instance_variable_get("@_#{date_field}".to_sym).valid?
25
+ errors[date_field] << "Invalid date"
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ # This is an after initialize method. We want to populate every gov_uk_date_field, unless it's a new
32
+ # record, and there is already a non-nil form date (this happens when you call new and pass a hash of attributes and values)
33
+ define_method(:populate_gov_uk_dates) do
34
+ self._gov_uk_dates.each do |date_field|
35
+ next if self.new_record? && self.instance_variable_get("@_#{date_field}".to_sym) != nil
36
+ GovUkDateFields::FormDate.set_from_date(self, date_field, self[date_field])
37
+ end
38
+ end
39
+
40
+
41
+
42
+ # For each of the gov uk date fields, we have to define the following
43
+ # instance methods (assuming the date field name is dob):
44
+ #
45
+ # * dob - retuns the date object (i.e. @_dob.date)
46
+ # * dob= - populatees @_dob
47
+ # * dob_dd - returns the day value for form popualtion
48
+ # * dob_dd= - sets the day value (used when updating from a form)
49
+ # * dob_mm - returns the month value for form popualtion
50
+ # * dob_mm= - sets the month value (used when updating from a form)
51
+ # * dob_yyyy - returns the year value for form popualtion
52
+ # * dob_yyyy= - sets the year value (used when updating from a form)
53
+ #
54
+ date_fields.each do |field|
55
+
56
+ # #dob -return @_dob.date
57
+ define_method(field) do
58
+ return self.instance_variable_get("@_#{field}".to_sym).date
59
+ end
60
+
61
+ # #dob=(date) = assigns a date to the GovukDateFields::FormDate object
62
+ define_method("#{field}=") do |new_date|
63
+ raise ArgumentError.new("#{new_date} is not a Date object") unless new_date.is_a?(Date) || new_date.nil?
64
+ GovUkDateFields::FormDate.set_from_date(self, field, new_date)
65
+ end
66
+
67
+ # #dob_dd - return the day value for form population
68
+ define_method("#{field}_dd") do
69
+ return self.instance_variable_get("@_#{field}".to_sym).dd
70
+ end
71
+
72
+ # #dob_mm - return the day value for form population
73
+ define_method("#{field}_mm") do
74
+ return self.instance_variable_get("@_#{field}".to_sym).mm
75
+ end
76
+
77
+ # #dob_yyyy - return the day value for form population
78
+ define_method("#{field}_yyyy") do
79
+ return self.instance_variable_get("@_#{field}".to_sym).yyyy
80
+ end
81
+
82
+ # #dob_dd= - set the day part of the date (used in population of model from form)
83
+ define_method("#{field}_dd=") do |day|
84
+ GovUkDateFields::FormDate.set_date_part(:dd, self, field, day)
85
+ end
86
+
87
+ # #dob_dd= - set the day part of the date (used in population of model from form)
88
+ define_method("#{field}_mm=") do |month|
89
+ GovUkDateFields::FormDate.set_date_part(:mm, self, field, month)
90
+ end
91
+
92
+ # #dob_dd= - set the day part of the date (used in population of model from form)
93
+ define_method("#{field}_yyyy=") do |year|
94
+ GovUkDateFields::FormDate.set_date_part(:yyyy, self, field, year)
95
+ end
96
+
97
+ end
98
+
99
+ after_initialize :populate_gov_uk_dates
100
+
101
+ include GovUkDateFields::ActsAsGovUkDate::LocalInstanceMethods
102
+ end
103
+ end
104
+
105
+ module LocalInstanceMethods
106
+ def populate_gov_uk_dates
107
+ self._gov_uk_dates.each do |field_name|
108
+ GovUkDateFields::FormDate.set_from_date(self, field_name, self[field_name])
109
+ end
110
+ end
111
+
112
+ # def populate_gov_uk_dates_from_form_fields
113
+ # MojDateFields::FormDate.set_from_date(self, :first_day_of_trial, self['first_day_of_trial'])
114
+ # end
115
+ end
116
+
117
+
118
+ end
119
+ end
120
+
121
+ ActiveRecord::Base.send :include, GovUkDateFields::ActsAsGovUkDate
122
+
@@ -0,0 +1,12 @@
1
+
2
+
3
+ ActionView::Helpers::FormBuilder.class_eval do
4
+
5
+ def gov_uk_date_field(attribute)
6
+ date_fields = GovUkDateFields::FormFields.new(self, @object_name, attribute)
7
+ date_fields.output
8
+ end
9
+
10
+ end
11
+
12
+
@@ -0,0 +1,127 @@
1
+ module GovUkDateFields
2
+
3
+ class FormDateError < RuntimeError; end
4
+
5
+
6
+ class FormDate
7
+
8
+ attr_accessor :dd, :mm, :yyyy, :date, :temp
9
+
10
+ # This method cannot be called directly: use .new_from_date or .new_from_params
11
+ def initialize(dd, mm, yyyy, date = nil)
12
+ @dd = dd
13
+ @mm = mm
14
+ @yyyy = yyyy
15
+ @date = date
16
+ @valid = date.nil? ? validate_date : true
17
+ end
18
+
19
+ # Creates a FormDate object from the date, and writes it to @_<attr_name>
20
+ # on the object, and updates the attr[<attr_name>] with the real date object
21
+ #
22
+ def self.set_from_date(object, attr_name, date)
23
+ if date.nil?
24
+ form_date = new('', '', '')
25
+ else
26
+ form_date = new(date.day.to_s, date.month.to_s, date.year.to_s, date)
27
+ end
28
+ update_object(object, attr_name, form_date)
29
+ end
30
+
31
+
32
+
33
+ def self.set_date_part(date_part, object, attr_name, value)
34
+ form_date = object.instance_variable_get("@_#{attr_name}".to_sym) || FormDate.nil_date
35
+ form_date.send("#{date_part}=", value)
36
+ form_date.create_date_from_date_parts
37
+ FormDate.update_object(object, attr_name, form_date)
38
+ end
39
+
40
+
41
+ def self.nil_date
42
+ new('', '', '')
43
+ end
44
+
45
+
46
+ def create_date_from_date_parts
47
+ @valid = true
48
+ if @dd.blank? && @mm.blank? && @yyyy.blank?
49
+ @date = nil
50
+ else
51
+ months = %w{ jan feb mar apr may jun jul aug sep oct nov dec }
52
+ mm_as_int = months.include?(@mm.downcase) ? months.index(@mm.downcase) + 1 : @mm.to_i
53
+ begin
54
+ @date = Date.new(@yyyy.to_i, mm_as_int, @dd.to_i)
55
+ rescue ArgumentError => err
56
+ raise err unless err.message == 'invalid date'
57
+ @valid = false
58
+ end
59
+ end
60
+ end
61
+
62
+
63
+ # Creates a FormDate object from the params, and writes it to the instance variable @_<attr_name>
64
+ # on the object, and updates the attr[attr_name] with the real date object
65
+ #
66
+ # If the dob_dd, dob_mm, dob_yy are not in the params hash, or if they are all blank, the object is
67
+ # not updated.
68
+ #
69
+ # def self.set_from_params(object, attr_name, params)
70
+ # params = HashWithIndifferentAccess.new(params) unless params.is_a?(HashWithIndifferentAccess)
71
+ # return if attr_not_present_in_params?(attr_name, params)
72
+ # form_date = new(params["#{attr_name}_dd"], params["#{attr_name}_mm"], params["#{attr_name}_yyyy"] )
73
+ # update_object(object, attr_name, form_date)
74
+ # end
75
+
76
+ def valid?
77
+ @valid
78
+ end
79
+
80
+ private_class_method :new
81
+
82
+ private
83
+
84
+
85
+ def transfer_temp_fields_to_live
86
+ @dd = @temp[:dd]
87
+ @mm = @temp[:mm]
88
+ @yyyy = @temp[:yyyy]
89
+ end
90
+
91
+
92
+
93
+ def self.update_object(object, attr_name, form_date)
94
+ object.instance_variable_set("@_#{attr_name}".to_sym, form_date)
95
+ object[attr_name] = form_date.date
96
+ end
97
+
98
+
99
+ def self.attr_not_present_in_params?(attr_name, params)
100
+ attr_missing_in_params?(attr_name, params) || all_blanks_in_params?(attr_name, params)
101
+ end
102
+
103
+ def self.attr_missing_in_params?(attr_name, params)
104
+ params["#{attr_name}_dd"].nil? && params["#{attr_name}_mm"].nil? && params["#{attr_name}_yyyy"].nil?
105
+ end
106
+
107
+ def self.all_blanks_in_params?(attr_name, params)
108
+ params["#{attr_name}_dd"].blank? && params["#{attr_name}_mm"].blank? && params["#{attr_name}_yyyy"].blank?
109
+ end
110
+
111
+
112
+ def validate_date
113
+ return true if @dd.blank? && @mm.blank? && @yyyy.blank?
114
+ months = %w{ jan feb mar apr may jun jul aug sep oct nov dec }
115
+
116
+ mm_as_int = months.include?(@mm.downcase) ? months.index(@mm.downcase) + 1 : @mm.to_i
117
+ begin
118
+ @date = Date.new(@yyyy.to_i, mm_as_int, @dd.to_i)
119
+ rescue ArgumentError => err
120
+ return false if err.message == 'invalid date'
121
+ raise err
122
+ end
123
+ end
124
+
125
+ end
126
+
127
+ end
@@ -0,0 +1,49 @@
1
+ module GovUkDateFields
2
+
3
+ class FormFields
4
+ DATE_SEGMENTS = {
5
+ day: '_dd',
6
+ month: '_mm',
7
+ year: '_yyyy'
8
+ }
9
+
10
+ def initialize(form, object_name, attribute)
11
+ @form = form
12
+ @object = form.object
13
+ @object_name = object_name
14
+ @attribute = attribute
15
+ end
16
+
17
+ def output
18
+ day_value = @object.send("#{@attribute}_dd")
19
+ month_value = @object.send("#{@attribute}_mm")
20
+ year_value = @object.send("#{@attribute}_yyyy")
21
+
22
+ %Q[
23
+ #{@form.text_field(@attribute, field_options(day_value, html_id(:day), html_name(:day), 'DD', 2))}
24
+ #{@form.text_field(@attribute, field_options(month_value, html_id(:month), html_name(:month), 'MM', 3))}
25
+ #{@form.text_field(@attribute, field_options(year_value, html_id(:year), html_name(:year), 'YYYY', 4))}
26
+ ].html_safe
27
+ end
28
+
29
+ private
30
+
31
+ def html_id(date_segment)
32
+ html_name(date_segment).gsub(/\]\[|\[|\]|\(/, '_').gsub(/\_\z/, '').gsub(/\)/, '')
33
+ end
34
+
35
+ def html_name(date_segment)
36
+ "#{@object_name}[#{@attribute}#{DATE_SEGMENTS[date_segment]}]"
37
+ end
38
+
39
+ def field_options(value, id, name, placeholder, size)
40
+ {
41
+ value: value,
42
+ id: id,
43
+ name: name,
44
+ placeholder: placeholder,
45
+ size: size
46
+ }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,2 @@
1
+ module GovUkDateFields
2
+ end
@@ -0,0 +1,3 @@
1
+ module GovUkDateFields
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'gov_uk_date_fields/acts_as_gov_uk_date'
2
+ require 'gov_uk_date_fields/form_date'
3
+ require 'gov_uk_date_fields/form_fields'
4
+ require 'gov_uk_date_fields/form_builder'
5
+
6
+ module GovUkDateFields
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :gov_uk_date_fields do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .