I'd use a helper:
def morning?(t = Time.current)
t.between? t.change(hour: 7), t.change(hour: 14)
end
t
is a time instance, defaulting to the current time, for example:
t = Time.current
#=> Tue, 04 Jul 2017 16:30:09 CEST +02:00
change
sets one or more of the time's elements. If only :hour
is specified, the "smaller" units like minutes and seconds are set to 0:
t.change(hour: 7) #=> Tue, 04 Jul 2017 07:00:00 CEST +02:00
t.change(hour: 14) #=> Tue, 04 Jul 2017 14:00:00 CEST +02:00
between?
finally checks if t
is between these values.
Passing the current time as an argument makes it easier to test the method:
morning? Time.parse('06:59') #=> false
morning? Time.parse('07:00') #=> true
morning? Time.parse('14:00') #=> true
morning? Time.parse('14:01') #=> false
Usage:
<% if morning? %>
Good Morning <%= current_user.nome %>!
<% else %>
Good Afternoon <%= current_user.nome %>!
<% end %>