Module: Rails::Timeago

Defined in:
lib/rails-timeago.rb,
lib/rails-timeago/helper.rb,
lib/rails-timeago/version.rb

Defined Under Namespace

Modules: Helper, VERSION Classes: Engine

Class Method Summary collapse

Class Method Details

.default_options(opts = nil) ⇒ Object

Read or write global rails-timeago default options. If no options are given the current defaults will be returned.

Available options:

:nojs

Add time ago in words as time tag content instead of absolute time. (default: false)

:date_only

Only print date as tag content instead of full time. (default: true)

:format

A time format for localize method used to format static time. (default: :default)

:limit

Set a limit for time ago tags. All dates before given limit will not be converted. Global limit should be given as a block to reevaluate limit each time timeago_tag is called. (default: proc { 4.days.ago })

:force

Force time ago tag ignoring limit option. (default: false)

:default

String that will be returned if time is nil. (default: ā€˜-ā€™)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rails-timeago.rb', line 70

def self.default_options(opts = nil)
  @defaults ||= {
    :nojs      => false,
    :force     => false,
    :format    => :default,
    :limit     => proc { 4.days.ago },
    :date_only => true,
    :default   => '-'
  }
  if opts
    @defaults.merge! opts.extract!(*@defaults.keys.select{|k| opts.include?(k)})
  else
    @defaults
  end
end

.has_locale(locale) ⇒ Object



125
126
127
128
# File 'lib/rails-timeago.rb', line 125

def self.has_locale(locale)
  return locales.include? locale if locales.any?
  return has_locale_file locale
end

.has_locale_file(locale) ⇒ Object



98
99
100
# File 'lib/rails-timeago.rb', line 98

def self.has_locale_file(locale)
  File.exist? locale_file(locale)
end

.locale_file(locale) ⇒ Object



90
91
92
# File 'lib/rails-timeago.rb', line 90

def self.locale_file(locale)
  locale_path + locale_file_name(locale)
end

.locale_file_name(locale) ⇒ Object



94
95
96
# File 'lib/rails-timeago.rb', line 94

def self.locale_file_name(locale)
  'jquery.timeago.' + locale + '.js'
end

.locale_pathObject



86
87
88
# File 'lib/rails-timeago.rb', line 86

def self.locale_path
  File.dirname(__FILE__) + '/../vendor/assets/javascripts/locales/'
end

.localesObject



130
131
132
# File 'lib/rails-timeago.rb', line 130

def self.locales
  @locales ||= []
end

.locales=(*attrs) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/rails-timeago.rb', line 134

def self.locales=(*attrs)
  if attrs[0].kind_of?(Array)
    @locales = attrs[0].map(&:to_s)
  else
    @locales = attrs.map(&:to_s)
  end
end

.lookup_locale(locale = nil) ⇒ Object

Look up a timeago locale. If no locale is given I18nā€™s default locale will be used. Lookup follows the given order:

1) ll-CC     (language and country)
2) ll        (only language)
3) I18n default locale
4) "en"


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rails-timeago.rb', line 109

def self.lookup_locale(locale = nil)
  locale = I18n.locale.to_s unless locale

  if locale =~ /^(\w+)(\-(\w+))?$/
    lang = $1.downcase
    if $3
      ctry = $3.upcase
      return "#{lang}-#{ctry}" if has_locale "#{lang}-#{ctry}"
    end
    return lang if has_locale lang
  end

  return I18n.default_locale.to_s if has_locale I18n.default_locale.to_s
  "en"
end