rails 7.days to human readable string

ruby-on-rails, ruby-on-rails-3

Solution

So there is no built in solution in Rails for this. I decided to go with

7.days.inspect => "7 days"

and later on, when project will get translated I'll extend `ActiveSupport::Duration` with something meaningful that will translate these

however I recommend to look at Robert`s comments to this question. I agree with the solution to hold value in database e.g.: "7 days" and from that do stuff. Like translate the unit value

document = Document.new
document.expire_in = "7 days"

document.translated_day

in Document model (or decorator)

class Document < ActiveRecord::Base
  #....

  def translated_day
    timeline = expire_in.split(' ')
    "#{timeline.first} #{I18n.t("timeline.${timeline.last}")}"
  end
  #..
end


#config/locales/svk.yml
svk:
  timeline:
    days: "dni"

Problem

I know this looks trivial but let say that in Ruby on Rails I have ``` document.expire_in = 7.days ``` how can I print human readable version of the expiry mesages ? ``` "Document will expire in #{document.expire_in}" => Document will expire in 7 days ``` maybe something that works with `I18n.t` or `I18n.l` the only way this works is 7.days.inspect => "7 days" is this the only way ?? I'm looking at ActiveSupport::Duration and don't see an answer thx

Original source