Skip to content

Commit

Permalink
Move user DOB formats to decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgumberg committed Nov 8, 2023
1 parent dc8f97d commit ecf5697
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
12 changes: 12 additions & 0 deletions app/decorators/user_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/notifications/volunteer_birthday_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/views/shared/_edit_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
<input type="text" placeholder="<%= resource.date_of_birth&.strftime("%Y/%m/%d") %>" autocomplete="off" readonly>
<% else %>
<input type="text" placeholder="<%= resource.decorate.formatted_date_of_birth %>" autocomplete="off" readonly>
<% end %>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<div class="input-style-1">
<%= 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" %>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/initializers/date_formats.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Date::DATE_FORMATS[:short_ordinal] = ->(date) { date.strftime("%B #{date.day.ordinalize}") }
Date::DATE_FORMATS[:slashes] = "%Y/%m/%d"

0 comments on commit ecf5697

Please sign in to comment.