Skip to content

Commit

Permalink
use TimeDifference for baby age > 24 weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Hawksley committed Feb 5, 2024
1 parent 53720fd commit 0be6440
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem "puma"
gem "rails", "~> 7.1"
gem "slim"
gem "tenkit", git: "https://github.com/joelhawksley/tenkit", branch: "add-alerts"
gem "time_difference"
gem "view_component"

group :development, :test do
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ GEM
temple (0.10.3)
thor (1.3.0)
tilt (2.3.0)
time_difference (0.5.0)
activesupport
timeout (0.4.1)
trailblazer-option (0.1.2)
tzinfo (2.0.6)
Expand Down Expand Up @@ -353,6 +355,7 @@ DEPENDENCIES
sqlite3
standard
tenkit!
time_difference
vcr
view_component
webmock
Expand Down
30 changes: 20 additions & 10 deletions app/models/calendar_feed.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
# frozen_string_literal: true

class CalendarFeed
def self.baby_age_event(birthdate = Date.parse(Timeframe::Application.config.local["birthdate"]))
day_count = Date.today - birthdate
week_count = (day_count / 7).to_i
remainder = (day_count % 7).to_i

summary =
if remainder > 0
if week_count > 0
"#{week_count}w#{remainder}d"
if week_count > 24
time_difference = TimeDifference.between(birthdate, Date.today).in_general
months = time_difference[:months]
weeks = time_difference[:weeks]
days = time_difference[:days]

summary = ""
summary << "#{months}m" if months > 0
summary << "#{weeks}w" if weeks > 0
summary << "#{days}d" if days > 0
else
summary =
if remainder > 0
if week_count > 0
"#{week_count}w#{remainder}d"
else
"#{remainder}d"
end
else
"#{remainder}d"
"#{week_count}w"
end
else
"#{week_count}w"
end
end

CalendarEvent.new(
id: "_baby_age",
Expand Down

0 comments on commit 0be6440

Please sign in to comment.