Friday, 16 August 2013

How can I make this conditional riddled instance method more Ruby idiomatic?

How can I make this conditional riddled instance method more Ruby idiomatic?

def short_remaining_time
difference = Time.diff(Time.now, created_at + 7.days, '%d - %H - %N')
# To display the short remaining time in an auction listing.
if difference[:day] == 0 and difference[:hour] >= 1
"#{difference[:minute]} minutos"
elsif difference[:day] == 0 and difference[:hour] >= 23
"#{difference[:hour]} horas"
else
if difference[:day] != 1
"#{difference[:day]} dias"
else
"#{difference[:day]} dia"
end
end
end
This method is inside my auction.rb model in my Rails application.
In one of my views, I am listing all auctions in the system, and I also
display how much time is remaining before the auction closes.
Depending on the amount of time, I either show the days hours or minutes.
The code is working fine, just looks and feels very clunky. Is there a way
to spruce this up a bit?

No comments:

Post a Comment