Skip to content

Commit

Permalink
More human time periods on event show title
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankocienski committed Oct 3, 2024
1 parent 8c826e5 commit a503c49
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
38 changes: 38 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,42 @@ def nav_link_to(page, path, route = {})
active = route.all? { |key, value| params[key] == value }
link_to(page, path, class: ('active' if active)).html_safe
end

def fancy_time_format(time)
time.strftime(time.min == 0 ? '%l%P' : '%l.%M%P')
end

def fancy_date_format(time)
day_s = time.day.ordinalize

time.strftime "%A #{day_s} %B, %Y"
end

def fancy_time_period_format(start_at, end_at)
# minutes
delta = ((end_at - start_at) / 60).floor
return 'zero minutes' if delta < 1
return 'all day' if delta >= 1410 # 11 hours, 30 minutes

hour_v = delta / 60
minute_v = delta % 60

minute_s = pluralize(minute_v, 'minute')

if hour_v == 0
return minute_v == 30 ? 'half an hour' : minute_s
end

hour_s = "#{hour_v} hours"
if minute_v == 0
return hour_v == 1 ? 'an hour' : hour_s
end

if minute_v == 30
return "#{hour_v == 1 ? 'one' : hour_v} and a half hours"
end

"#{hour_s} and #{minute_s}"
end
# hmm: why am i not using `distance_of_time_in_words`?
end
10 changes: 0 additions & 10 deletions app/helpers/events_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ def link_to_day(date)
link_to fancy_date_format(date), events_path(day: date.strftime('%Y-%m-%d'))
end

def fancy_time_format(time)
time.strftime(time.min == 0 ? '%l%P' : '%l.%M%P')
end

def fancy_date_format(time)
day_s = time.day.ordinalize

time.strftime "%A #{day_s} %B, %Y"
end

def link_to_event_instance_with_time_and_partner(event_instance)
event = event_instance.event
time_part = fancy_time_format(event_instance.starts_at)
Expand Down
2 changes: 1 addition & 1 deletion app/views/events/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
%h2
- if @event_instance.ends_at.present?
From <em>#{@event_instance.starts_at.strftime(EventsHelper::DATE_TIME_FORMAT)}</em>
to <em>#{@event_instance.ends_at.strftime(EventsHelper::DATE_TIME_FORMAT)}</em>
for <em title="#{@event_instance.ends_at.strftime(EventsHelper::DATE_TIME_FORMAT)}">#{fancy_time_period_format(@event_instance.starts_at, @event_instance.ends_at)}</em>
- else
At <em>#{@event_instance.starts_at.strftime(EventsHelper::DATE_TIME_FORMAT)}</em>
on #{fancy_date_format(@event_instance.starts_at)}
Expand Down
26 changes: 26 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rails_helper'

RSpec.describe ApplicationHelper, type: :helper do
context 'fancy_time_period_format' do
it 'gives correct text for a given input' do
times = [
{ hour: 0, minute: 0, text: "zero minutes" },
{ hour: 23, minute: 59, text: 'all day' },
{ hour: 0, minute: 15, text: '15 minutes' },
{ hour: 0, minute: 30, text: 'half an hour' },
{ hour: 1, minute: 0, text: 'an hour' },
{ hour: 2, minute: 0, text: '2 hours' },
{ hour: 3, minute: 30, text: '3 and a half hours' },
{ hour: 4, minute: 45, text: '4 hours and 45 minutes' }
]

times.each do |check|
starts_at = Time.new(2000, 1, 1, 0, 0, 0)
ends_at = Time.new(2000, 1, 1, check[:hour], check[:minute], 0)

out = fancy_time_period_format(starts_at, ends_at)
expect(out).to eq check[:text]
end
end
end
end

0 comments on commit a503c49

Please sign in to comment.