Module: Rails::Timeago::Helper
- Defined in:
- lib/rails-timeago/helper.rb
Instance Method Summary collapse
-
#timeago_script_tag(**kwargs) ⇒ Object
Return a JavaScript tag to set jQuery timeago locale.
-
#timeago_tag(time, html_options = {}) ⇒ Object
Create a time tag usable for jQuery timeago plugin.
-
#timeago_tag_content(time, time_options = {}) ⇒ Object
:nodoc:.
Instance Method Details
#timeago_script_tag(**kwargs) ⇒ Object
Return a JavaScript tag to set jQuery timeago locale.
80 81 82 |
# File 'lib/rails-timeago/helper.rb', line 80 def timeago_script_tag(**kwargs) javascript_tag("jQuery.timeago.settings.lang=\"#{I18n.locale}\";", **kwargs) if I18n.locale != 'en' end |
#timeago_tag(time, html_options = {}) ⇒ Object
Create a time tag usable for jQuery timeago plugin.
timeago_tag Time.zone.now
=> "<time datetime="2012-03-10T12:07:07+01:00"
title="Sat, 10 Mar 2012 12:07:07 +0100"
data-time-ago="2012-03-10T12:07:07+01:00">2012-03-10</time>"
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. (default: 4.days.ago)
- :
force
-
Force time ago tag ignoring limit option. (default: false)
- :
default
-
String that will be returned if time is nil. (default: ‘-’)
All other options will be given as options to tag helper.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rails-timeago/helper.rb', line 43 def timeago_tag(time, = {}) = Rails::Timeago. = .merge .extract!(*.keys.select do |k| .include?(k) end) return [:default] if time.nil? [:format] = [:format].call(time, ) if [:format].is_a?(Proc) if [:title] [:title] = [:title].is_a?(Proc) ? [:title].call(time, ) : [:title] end [:limit] = [:limit].call if [:limit].is_a?(Proc) time_range = unless [:limit].nil? now = Time.zone.now limit = [:limit] limit < now ? limit...now : now...limit end if [:force] || time_range.nil? || time_range.cover?(time) ['data-time-ago'] = time.iso8601 end time_tag time, timeago_tag_content(time, ), end |
#timeago_tag_content(time, time_options = {}) ⇒ Object
:nodoc:
70 71 72 73 74 75 76 77 |
# File 'lib/rails-timeago/helper.rb', line 70 def timeago_tag_content(time, = {}) # :nodoc: time = time.to_date if [:date_only] if [:nojs] && ([:limit].nil? || [:limit] < time) return time_ago_in_words(time) end I18n.l time, format: [:format] end |