Reputation: 9978
I have a very simple task at hand. If the last time a record was updated has been longer than 15 minutes, display a button. Otherwise, don't display the button.
The field is a datetime.
My view code:
<% if @object.display_button? -%>
my button
<% end -%>
My display button method on that object:
def display_button?
return false if last_updated.nil?
if Time.now - last_updated > 15.minutes
true
else
false
end
end
I also have this unit tested, which are passing, but when it comes to the implementation, it doesn't seem to work.
Is my logic correct or would there be a better way of accomplishing this?
Upvotes: 1
Views: 2036
Reputation: 187004
if last_updated < 15.minutes.ago
The minutes method returns an integer I believe, and subtracting time object yields another time object. So your expression compares an int to a time and does something that you dont expect.
15.minutes.ago
yields a time object that can be directly compared to another time object.
Also, never ever do if (something) return true; else; return false
in ruby. Your method will return the value of that last expression executed in it, so you can vastly simplify that entire method:
def display_button?
last_updated && last_updated < 15.minutes.ago
end
Isn't that easier to read?
Upvotes: 7