ActiveSupport: extensions & time
This course covers a set of ActiveSupport extensions as they appear in this app's own code. It starts with blank?, present? and presence, including why 0 counts as present and false counts as blank. It compares Time.current with Time.now and reads the Rails source behind them. It explains ActiveSupport::Duration and the beginning_of_day, beginning_of_week and beginning_of_month helpers used in reporting queries. It also covers to_sentence for error messages. It ends with try and the split between core Ruby's except and ActiveSupport's except!.
What you'll learn
- ✓How blank?, present? and presence treat nil, strings, numbers, booleans and collections
- ✓Why Time.current returns UTC in a Rails app while Time.now uses the server's local zone
- ✓How ActiveSupport::Duration works with .days, .hours and .ago, including calendar-aware arithmetic
- ✓Snapping times to day, week and month boundaries, including weeks that start on Sunday
- ✓Turning error arrays into readable text with to_sentence and its connector options
- ✓When try fits better than &., and why except is core Ruby while except! is ActiveSupport
blank? and present?: the two cases that surprise everyone
app/models/identity/user.rb, this app's model:
Read the full lesson free →Curriculum
7 lessonsblank? and present?: the two cases that surprise everyone
The blank? truth table across twelve values, with presence for fallbacks and the surprising 0 and false cases.
Time.current vs Time.now, different on the machine that ran them
Why Time.current and Time.now differ by hours, and how Rails defaults Time.zone to UTC.
5.days is an object, not an integer: ActiveSupport::Duration
Why 5.minutes is an ActiveSupport::Duration object, used with .ago in guards and calendar-aware date arithmetic.
beginning_of_day, end_of_week, and this app's reporting query
How this app's reporting queries chain .ago into beginning_of_day, beginning_of_week and beginning_of_month, including Sunday weeks.
to_sentence: nine controllers turning an error array into prose
How to_sentence joins error arrays into prose, its Oxford comma default, and its I18n-backed connector options.
try, and except: the one that became core Ruby
How try differs from safe navigation, and why except is core Ruby while except! remains in ActiveSupport.
Putting it together: extensions checked against how they behave
A review of the six methods, each traced to a file in this app, plus a plain Ruby blank? challenge.