-1
  def readable
    uptime = (Time.now - self).to_i
    case uptime
    when 0 then 'just now'
    when 1 then 'uptime second ago'
    when 2..59 then uptime.to_s + ' seconds ago'
    when 60..119 then 'uptime minute ago' # 120 = 2 minutes
    when 120..3540 then (uptime / 60).to_i.to_s + ' minutes ago'
    when 3541..7100 then 'an hour ago' # 3600 = 1 hour
    when 7101..82_800 then ((uptime + 99) / 3600).to_i.to_s + ' hours ago'
    when 82_801..172_000 then 'uptime day ago' # 86400 = 1 day
    else ((uptime + 800) / 86_400).to_i.to_s + ' days ago'
    end
  end

Linter speaks of the following mistakes, how can this be fixed?

  • 3
    https://codereview.stackexchange.com/ is the proper place for this kind of question. – Aleksei Matiushkin Sep 18 '17 at 09:46
  • 2
    I'm voting to close this question as off-topic because it is working code, and should instead be posted on [Code Review](http://codereview.stackexchange.com) – Mark Thomas Sep 18 '17 at 11:52
  • This is a case where I temporarily disable the rubocop rules b/c I agree it's a pretty concise implementation. – Joe Sep 18 '17 at 14:25

1 Answers1

0

Take a look at time_ago_in_words and feel free to use it or its code.

About code metrics - your code is pretty simple, you should extract only uptime method.

Akzhan Abdulin
  • 993
  • 6
  • 8
  • 1
    The [implementation](https://github.com/rails/rails/blob/v5.1.4/actionview/lib/action_view/helpers/date_helper.rb#L93-L156) is not that much simpler, is it? – Stefan Sep 18 '17 at 10:28
  • Of course. There is no chances to write it simpler. I was referred to it just thinking about Don't repeat Rails :) – Akzhan Abdulin Sep 18 '17 at 10:30