From ecf5697262c29098266f1d05d71111a6d3d3ded6 Mon Sep 17 00:00:00 2001 From: David Gumberg Date: Tue, 7 Nov 2023 22:51:57 -0500 Subject: [PATCH] Move user DOB formats to decorator --- app/decorators/user_decorator.rb | 12 ++++++++++++ app/notifications/volunteer_birthday_notification.rb | 2 +- app/views/shared/_edit_form.html.erb | 6 +++--- app/views/users/edit.html.erb | 2 +- config/initializers/date_formats.rb | 1 + 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/decorators/user_decorator.rb b/app/decorators/user_decorator.rb index bf498f7733..a4234c9937 100644 --- a/app/decorators/user_decorator.rb +++ b/app/decorators/user_decorator.rb @@ -44,4 +44,16 @@ def formatted_reset_password_sent_at def formatted_invitation_sent_at formatted_timestamp(:invitation_sent_at) end + + def formatted_birthday + return "" unless object.date_of_birth.respond_to?(:strftime) + + object.date_of_birth.to_date.to_fs(:short_ordinal) + end + + def formatted_date_of_birth + return "" unless object.date_of_birth.respond_to?(:strftime) + + object.date_of_birth.to_date.to_fs(:slashes) + end end diff --git a/app/notifications/volunteer_birthday_notification.rb b/app/notifications/volunteer_birthday_notification.rb index f92cac6c92..83e2878acd 100644 --- a/app/notifications/volunteer_birthday_notification.rb +++ b/app/notifications/volunteer_birthday_notification.rb @@ -16,7 +16,7 @@ class VolunteerBirthdayNotification < BaseNotification # Define helper methods to make rendering easier. def message - "🎉 🎂 #{params[:volunteer].display_name}'s birthday is on #{params[:volunteer].date_of_birth.to_date.to_fs(:short_ordinal)}!" + "🎉 🎂 #{params[:volunteer].display_name}'s birthday is on #{params[:volunteer].decorate.formatted_birthday}!" end def title diff --git a/app/views/shared/_edit_form.html.erb b/app/views/shared/_edit_form.html.erb index d5d2810756..bce764fa69 100644 --- a/app/views/shared/_edit_form.html.erb +++ b/app/views/shared/_edit_form.html.erb @@ -32,11 +32,11 @@ <%= f.label :date_of_birth, "Date of birth" %> <% if policy(resource).update_user_setting? %> <%= f.text_field :date_of_birth, - value: resource.date_of_birth&.strftime("%Y/%m/%d"), + value: resource.decorate.formatted_date_of_birth, data: {provide: "datepicker", date_format: "yyyy/mm/dd"}, class: "form-control label-font-weight" %> - <% else %> - " autocomplete="off" readonly> + <% else %> + <% end %> diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index e481b6ebcf..b332de1c42 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -26,7 +26,7 @@
<%= form.label :date_of_birth, "Date of birth" %> <%= form.text_field :date_of_birth, - value: @user.date_of_birth&.strftime("%Y/%m/%d"), + value: @user.decorate.formatted_date_of_birth, data: {provide: "datepicker", date_format: "yyyy/mm/dd"}, class: "form-control label-font-weight" %>
diff --git a/config/initializers/date_formats.rb b/config/initializers/date_formats.rb index 40434ef897..0b40d84ab0 100644 --- a/config/initializers/date_formats.rb +++ b/config/initializers/date_formats.rb @@ -1 +1,2 @@ Date::DATE_FORMATS[:short_ordinal] = ->(date) { date.strftime("%B #{date.day.ordinalize}") } +Date::DATE_FORMATS[:slashes] = "%Y/%m/%d"