From a005c996574e07c974ae1fb2c833dd575182dbcb Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Tue, 27 Jan 2009 09:26:58 -0300 Subject: [PATCH] ActionItem916: changed birth_date to use select forms --- app/helpers/dates_helper.rb | 4 ++-- app/models/person.rb | 4 ++-- app/views/profile_editor/_person_form.rhtml | 2 +- db/migrate/059_add_birth_date_to_person.rb | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ db/schema.rb | 3 ++- public/stylesheets/person_data.css | 4 ++++ test/functional/profile_editor_controller_test.rb | 2 +- 7 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 db/migrate/059_add_birth_date_to_person.rb diff --git a/app/helpers/dates_helper.rb b/app/helpers/dates_helper.rb index ce8aa3b..04326ca 100644 --- a/app/helpers/dates_helper.rb +++ b/app/helpers/dates_helper.rb @@ -86,8 +86,8 @@ module DatesHelper link_to show_month(next_month_date.year, next_month_date.month) + ' →', :year => next_month_date.year, :month => next_month_date.month end - def pick_date(object, method) - date_select(object, method, :use_month_names => MONTHS.map {|item| gettext(item)}) + def pick_date(object, method, options = {}, html_options = {}) + date_select(object, method, html_options.merge(options.merge(:use_month_names => MONTHS.map {|item| gettext(item)}))) end end diff --git a/app/models/person.rb b/app/models/person.rb index 7d21a82..693e818 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -105,8 +105,8 @@ class Person < Profile N_('Formation'); N_('Custom formation'); N_('Custom area of study'); settings_items :formation, :custom_formation, :custom_area_of_study - N_('Contact information'); N_('Birth date'); N_('City'); N_('State'); N_('Country'); N_('Sex'); N_('Zip code') - settings_items :photo, :contact_information, :birth_date, :sex, :city, :state, :country, :zip_code + N_('Contact information'); N_('City'); N_('State'); N_('Country'); N_('Sex'); N_('Zip code') + settings_items :photo, :contact_information, :sex, :city, :state, :country, :zip_code def self.conditions_for_profiles(conditions, person) new_conditions = sanitize_sql(['role_assignments.accessor_id = ?', person]) diff --git a/app/views/profile_editor/_person_form.rhtml b/app/views/profile_editor/_person_form.rhtml index 1008f94..77f4877 100644 --- a/app/views/profile_editor/_person_form.rhtml +++ b/app/views/profile_editor/_person_form.rhtml @@ -14,7 +14,7 @@ <%= optional_field(@person, 'cell_phone', f.text_field(:cell_phone)) %> <%= optional_field(@person, 'comercial_phone', f.text_field(:comercial_phone)) %> <%= optional_field(@person, 'sex', f.radio_group(:profile_data, :sex, [ ['male',_('Male')], ['female',_('Female')] ])) %> -<%= optional_field(@person, 'birth_date', f.text_field(:birth_date)) %> +<%= optional_field(@person, 'birth_date', labelled_form_field(_('Birth date'), '
' + pick_date(:profile_data, :birth_date, {:start_year => (Date.today.year - 100), :end_year => (Date.today.year - 10)}) + '
')) %> <%= optional_field(@person, 'address', labelled_form_field(_('Address (street and number)'), text_field(:profile_data, :address))) %> <%= optional_field(@person, 'zip_code', labelled_form_field(_('ZIP code'), text_field(:profile_data, :zip_code))) %> <%= optional_field(@person, 'city', f.text_field(:city)) %> diff --git a/db/migrate/059_add_birth_date_to_person.rb b/db/migrate/059_add_birth_date_to_person.rb new file mode 100644 index 0000000..a277db5 --- /dev/null +++ b/db/migrate/059_add_birth_date_to_person.rb @@ -0,0 +1,111 @@ +require File.dirname(__FILE__) + '/../../config/environment' + +class AddBirthDateToPerson < ActiveRecord::Migration + + class ConvertDates + def self.convert(date_string) + return if date_string.blank? + return date_string if date_string.kind_of?(Date) + return unless date_string.kind_of?(String) + return if date_string =~ /[a-zA-Z]/ + + if date_string =~ /^\d\d([^\d]+)\d\d$/ + date_string += $1 + (Date.today.year - 100).to_s + end + + if date_string =~ /[^\d](\d\d)$/ + year = $1.to_i + date_string = date_string[0..-3] + (year > (Date.today.year - 2000) ? year + 1900 : year + 2000).to_s + end + + date_string.gsub!('/', '.') + Date.parse(date_string) + end + end + + class Person < ActiveRecord::Base + set_table_name 'profiles' + serialize :data, Hash + end + + def self.up + add_column :profiles, :birth_date, :date + Person.find(:all).select{|p| p.type = 'Person'}.each do |p| + p.birth_date = ConvertDates.convert(p.data[:birth_date].to_s) + p.save + end + end + + def self.down + remove_column :profiles, :birth_date + end +end + +if $PROGRAM_NAME == __FILE__ + require File.dirname(__FILE__) + '/../../test/test_helper' + + class ConvertDatesTest < Test::Unit::TestCase + + should 'convert with slash' do + date = ConvertDates.convert('10/01/2009') + assert_equal [10, 1, 2009], [date.day, date.month, date.year] + end + + should 'convert with hyphen' do + date = ConvertDates.convert('10-01-2009') + assert_equal [10, 1, 2009], [date.day, date.month, date.year] + end + + should 'convert with dot' do + date = ConvertDates.convert('10.01.2009') + assert_equal [10, 1, 2009], [date.day, date.month, date.year] + end + + should 'convert with slash and space' do + date = ConvertDates.convert('10/ 01/ 2009') + assert_equal [10, 1, 2009], [date.day, date.month, date.year] + end + + should 'convert with empty to nil' do + date = ConvertDates.convert('') + assert_nil date + end + + should 'convert with nil to nil' do + date = ConvertDates.convert(nil) + assert_nil date + end + + should 'convert with two digits 1900' do + date = ConvertDates.convert('10/01/99') + assert_equal [10, 1, 1999], [date.day, date.month, date.year] + end + + should 'convert with two digits 2000' do + date = ConvertDates.convert('10/01/09') + assert_equal [10, 1, 2009], [date.day, date.month, date.year] + end + + should 'convert with two numbers' do + date = ConvertDates.convert('10/01') + assert_equal [10, 1, (Date.today.year - 100)], [date.day, date.month, date.year] + end + + should 'convert to nil if non-numeric date' do + date = ConvertDates.convert('10 de agosto de 2009') + assert_nil date + end + + should 'do nothing if date' do + date = ConvertDates.convert(Date.today) + assert_equal Date.today, date + end + + should 'return nil when not string nor date' do + date = ConvertDates.convert(1001) + assert_nil date + end + + end + +end diff --git a/db/schema.rb b/db/schema.rb index 921d4ed..a4d4c22 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 58) do +ActiveRecord::Schema.define(:version => 59) do create_table "article_versions", :force => true do |t| t.integer "article_id" @@ -232,6 +232,7 @@ ActiveRecord::Schema.define(:version => 58) do t.text "custom_footer" t.string "theme" t.boolean "public_profile", :default => true + t.date "birth_date" end add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id" diff --git a/public/stylesheets/person_data.css b/public/stylesheets/person_data.css index dc343a6..74b52b5 100644 --- a/public/stylesheets/person_data.css +++ b/public/stylesheets/person_data.css @@ -8,3 +8,7 @@ width: 108px; } +#profile-data .select-birth-date select { + width: 70px; +} + diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index 5a74622..c10e09e 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -279,7 +279,7 @@ class ProfileEditorControllerTest < Test::Unit::TestCase should 'render person partial' do person = create_user('test_profile', :environment => Environment.default).person - Person.any_instance.expects(:active_fields).returns(['contact_phone', 'birth_date', 'address']).at_least_once + Person.any_instance.expects(:active_fields).returns(['contact_phone', 'address']).at_least_once get :edit, :profile => person.identifier person.active_fields.each do |field| assert_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" } -- libgit2 0.21.2