Commit a005c996574e07c974ae1fb2c833dd575182dbcb

Authored by Moises Machado
1 parent 52591d04

ActionItem916: changed birth_date to use select forms

Had to convert the birth_date setting to a database column for active_record do its magic and couple with the date_select helper that is used on the pick_date helper
app/helpers/dates_helper.rb
... ... @@ -86,8 +86,8 @@ module DatesHelper
86 86 link_to show_month(next_month_date.year, next_month_date.month) + ' →', :year => next_month_date.year, :month => next_month_date.month
87 87 end
88 88  
89   - def pick_date(object, method)
90   - date_select(object, method, :use_month_names => MONTHS.map {|item| gettext(item)})
  89 + def pick_date(object, method, options = {}, html_options = {})
  90 + date_select(object, method, html_options.merge(options.merge(:use_month_names => MONTHS.map {|item| gettext(item)})))
91 91 end
92 92  
93 93 end
... ...
app/models/person.rb
... ... @@ -105,8 +105,8 @@ class Person < Profile
105 105 N_('Formation'); N_('Custom formation'); N_('Custom area of study');
106 106 settings_items :formation, :custom_formation, :custom_area_of_study
107 107  
108   - N_('Contact information'); N_('Birth date'); N_('City'); N_('State'); N_('Country'); N_('Sex'); N_('Zip code')
109   - settings_items :photo, :contact_information, :birth_date, :sex, :city, :state, :country, :zip_code
  108 + N_('Contact information'); N_('City'); N_('State'); N_('Country'); N_('Sex'); N_('Zip code')
  109 + settings_items :photo, :contact_information, :sex, :city, :state, :country, :zip_code
110 110  
111 111 def self.conditions_for_profiles(conditions, person)
112 112 new_conditions = sanitize_sql(['role_assignments.accessor_id = ?', person])
... ...
app/views/profile_editor/_person_form.rhtml
... ... @@ -14,7 +14,7 @@
14 14 <%= optional_field(@person, 'cell_phone', f.text_field(:cell_phone)) %>
15 15 <%= optional_field(@person, 'comercial_phone', f.text_field(:comercial_phone)) %>
16 16 <%= optional_field(@person, 'sex', f.radio_group(:profile_data, :sex, [ ['male',_('Male')], ['female',_('Female')] ])) %>
17   -<%= optional_field(@person, 'birth_date', f.text_field(:birth_date)) %>
  17 +<%= optional_field(@person, 'birth_date', labelled_form_field(_('Birth date'), '<div class="select-birth-date">' + pick_date(:profile_data, :birth_date, {:start_year => (Date.today.year - 100), :end_year => (Date.today.year - 10)}) + '</div>')) %>
18 18 <%= optional_field(@person, 'address', labelled_form_field(_('Address (street and number)'), text_field(:profile_data, :address))) %>
19 19 <%= optional_field(@person, 'zip_code', labelled_form_field(_('ZIP code'), text_field(:profile_data, :zip_code))) %>
20 20 <%= optional_field(@person, 'city', f.text_field(:city)) %>
... ...
db/migrate/059_add_birth_date_to_person.rb 0 → 100644
... ... @@ -0,0 +1,111 @@
  1 +require File.dirname(__FILE__) + '/../../config/environment'
  2 +
  3 +class AddBirthDateToPerson < ActiveRecord::Migration
  4 +
  5 + class ConvertDates
  6 + def self.convert(date_string)
  7 + return if date_string.blank?
  8 + return date_string if date_string.kind_of?(Date)
  9 + return unless date_string.kind_of?(String)
  10 + return if date_string =~ /[a-zA-Z]/
  11 +
  12 + if date_string =~ /^\d\d([^\d]+)\d\d$/
  13 + date_string += $1 + (Date.today.year - 100).to_s
  14 + end
  15 +
  16 + if date_string =~ /[^\d](\d\d)$/
  17 + year = $1.to_i
  18 + date_string = date_string[0..-3] + (year > (Date.today.year - 2000) ? year + 1900 : year + 2000).to_s
  19 + end
  20 +
  21 + date_string.gsub!('/', '.')
  22 + Date.parse(date_string)
  23 + end
  24 + end
  25 +
  26 + class Person < ActiveRecord::Base
  27 + set_table_name 'profiles'
  28 + serialize :data, Hash
  29 + end
  30 +
  31 + def self.up
  32 + add_column :profiles, :birth_date, :date
  33 + Person.find(:all).select{|p| p.type = 'Person'}.each do |p|
  34 + p.birth_date = ConvertDates.convert(p.data[:birth_date].to_s)
  35 + p.save
  36 + end
  37 + end
  38 +
  39 + def self.down
  40 + remove_column :profiles, :birth_date
  41 + end
  42 +end
  43 +
  44 +if $PROGRAM_NAME == __FILE__
  45 + require File.dirname(__FILE__) + '/../../test/test_helper'
  46 +
  47 + class ConvertDatesTest < Test::Unit::TestCase
  48 +
  49 + should 'convert with slash' do
  50 + date = ConvertDates.convert('10/01/2009')
  51 + assert_equal [10, 1, 2009], [date.day, date.month, date.year]
  52 + end
  53 +
  54 + should 'convert with hyphen' do
  55 + date = ConvertDates.convert('10-01-2009')
  56 + assert_equal [10, 1, 2009], [date.day, date.month, date.year]
  57 + end
  58 +
  59 + should 'convert with dot' do
  60 + date = ConvertDates.convert('10.01.2009')
  61 + assert_equal [10, 1, 2009], [date.day, date.month, date.year]
  62 + end
  63 +
  64 + should 'convert with slash and space' do
  65 + date = ConvertDates.convert('10/ 01/ 2009')
  66 + assert_equal [10, 1, 2009], [date.day, date.month, date.year]
  67 + end
  68 +
  69 + should 'convert with empty to nil' do
  70 + date = ConvertDates.convert('')
  71 + assert_nil date
  72 + end
  73 +
  74 + should 'convert with nil to nil' do
  75 + date = ConvertDates.convert(nil)
  76 + assert_nil date
  77 + end
  78 +
  79 + should 'convert with two digits 1900' do
  80 + date = ConvertDates.convert('10/01/99')
  81 + assert_equal [10, 1, 1999], [date.day, date.month, date.year]
  82 + end
  83 +
  84 + should 'convert with two digits 2000' do
  85 + date = ConvertDates.convert('10/01/09')
  86 + assert_equal [10, 1, 2009], [date.day, date.month, date.year]
  87 + end
  88 +
  89 + should 'convert with two numbers' do
  90 + date = ConvertDates.convert('10/01')
  91 + assert_equal [10, 1, (Date.today.year - 100)], [date.day, date.month, date.year]
  92 + end
  93 +
  94 + should 'convert to nil if non-numeric date' do
  95 + date = ConvertDates.convert('10 de agosto de 2009')
  96 + assert_nil date
  97 + end
  98 +
  99 + should 'do nothing if date' do
  100 + date = ConvertDates.convert(Date.today)
  101 + assert_equal Date.today, date
  102 + end
  103 +
  104 + should 'return nil when not string nor date' do
  105 + date = ConvertDates.convert(1001)
  106 + assert_nil date
  107 + end
  108 +
  109 + end
  110 +
  111 +end
... ...
db/schema.rb
... ... @@ -9,7 +9,7 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11  
12   -ActiveRecord::Schema.define(:version => 58) do
  12 +ActiveRecord::Schema.define(:version => 59) do
13 13  
14 14 create_table "article_versions", :force => true do |t|
15 15 t.integer "article_id"
... ... @@ -232,6 +232,7 @@ ActiveRecord::Schema.define(:version =&gt; 58) do
232 232 t.text "custom_footer"
233 233 t.string "theme"
234 234 t.boolean "public_profile", :default => true
  235 + t.date "birth_date"
235 236 end
236 237  
237 238 add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id"
... ...
public/stylesheets/person_data.css
... ... @@ -8,3 +8,7 @@
8 8 width: 108px;
9 9 }
10 10  
  11 +#profile-data .select-birth-date select {
  12 + width: 70px;
  13 +}
  14 +
... ...
test/functional/profile_editor_controller_test.rb
... ... @@ -279,7 +279,7 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase
279 279  
280 280 should 'render person partial' do
281 281 person = create_user('test_profile', :environment => Environment.default).person
282   - Person.any_instance.expects(:active_fields).returns(['contact_phone', 'birth_date', 'address']).at_least_once
  282 + Person.any_instance.expects(:active_fields).returns(['contact_phone', 'address']).at_least_once
283 283 get :edit, :profile => person.identifier
284 284 person.active_fields.each do |field|
285 285 assert_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" }
... ...