forked from Paxa/green_monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view_helper.rb
60 lines (49 loc) · 1.76 KB
/
view_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# coding: utf-8
require "chronic_duration"
module GreenMonkey
module ViewHelper
# it shortcut for this
#%time{:datetime => post.published_at.iso8601(10) }= post.published_at.strftime("%d %h %Y")
# = time_tag post.created_at
# = time_tag post.created_at, format: "%d %h %Y %R%p"
# = time_tag post.created_at, itemprop: "datePublished"
def time_tag(time, *args)
options = args.extract_options!
format = options.delete(:format) || :long
datetime = time_to_iso8601(time)
if time.acts_like?(:time)
title = nil
content = args.first || I18n.l(time, format: format)
elsif time.kind_of?(Numeric)
title = ChronicDuration.output(time, :format => format)
content = args.first || distance_of_time_in_words(time)
end
content_tag(:time, content, options.reverse_merge(datetime: datetime, title: title))
end
def time_tag_interval(from, to, *args)
options = args.extract_options!
format = options.delete(:format) || :long
datetime = [from, to].map(&method(:time_to_iso8601)).join("/")
content = args.first || [from, to].map do |time|
if time.acts_like?(:time)
I18n.l(from, format: format)
else
ChronicDuration.output(time, :format => format)
end
end
if to.acts_like?(:time)
content = content.join(" - ")
else
content = content.join(" in ")
end
content_tag(:time, content, options.reverse_merge(datetime: datetime))
end
def time_to_iso8601(time)
if time.acts_like?(:time)
time.iso8601
elsif time.kind_of?(Numeric)
ChronicDuration.output(time, :format => :iso8601)
end
end
end
end