Skip to content

Commit

Permalink
time_tag_interval helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Paxa committed Nov 22, 2011
1 parent 331110f commit f6d506c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 24 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -35,6 +35,20 @@ Haml & HTML:
<time datetime="PT3H30M" title="3 hours 30 minutes">about 4 hours</time>
```

### helper `time_tag_interval`

The same with `time_tag` but made for time intervals

```haml
= time_tag_interval Time.parse("14 March 1879"), Time.parse("18 April 1955"), :format => '%d %h %Y'
= time_tag_interval Time.parse("14 March 1989"), 150.hours, :format => :short
```

```html
<time datetime="1879-03-14T00:00:00+07:30/1955-04-18T00:00:00+07:30">14 Mar 1879 - 14 Mar 1879</time>
<time datetime="1989-05-06T00:00:00+08:00/P6DT6H">06 May 00:00 in 6d 6h</time>
```

### ActiveRecord::Base#html\_schema\_type

```ruby
Expand Down
46 changes: 38 additions & 8 deletions lib/green_monkey/ext/view_helper.rb
Expand Up @@ -10,21 +10,51 @@ module ViewHelper
# = 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(date_or_time, *args)
def time_tag(time, *args)
options = args.extract_options!
format = options.delete(:format) || :long
datetime = time_to_iso8601(time)

if date_or_time.acts_like?(:time)

if time.acts_like?(:time)
title = nil
content = args.first || I18n.l(date_or_time, format: format)
datetime = date_or_time.iso8601(10)
elsif date_or_time.kind_of?(Numeric)
title = ChronicDuration.output(date_or_time, :format => format)
content = distance_of_time_in_words(date_or_time)
datetime = ChronicDuration.output(date_or_time, :format => :iso8601)
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
48 changes: 32 additions & 16 deletions spec/haml_spec.rb
Expand Up @@ -34,22 +34,6 @@ def render_haml(template, options = {})
render_haml("%b[:title] Dada").should =~ /itemprop=.?title/
end

it "should run time_tag with time" do
time = Time.now
str = render_haml("= time_tag(time)", time: time)

str.should =~ /<time.+datetime=.?#{Regexp.escape time.iso8601(10)}/
end

it "should run time_tag with duration" do
str = render_haml("= time_tag(time)", time: 3.hours + 30.minutes)
str.should =~ /datetime=.PT3H30M/
end

it "should run time_tag with time interval" do
# TODO
end

it "should generate valid microdata layout" do
post = Post.create(title: "Post 1", body: "Some text")
tpl = Pathname.new(__FILE__).dirname.join("post.haml")
Expand All @@ -66,4 +50,36 @@ def render_haml(template, options = {})
doc.items[0].id.should == post.id.to_s
doc.items[0].type.should == post.html_schema_type.itemtype.source
end

describe 'time_tag' do
it "should produce itemprop if specified" do
str = render_haml("= time_tag(Time.now, itemprop: 'time')")
str.should =~ /itemprop="time"/
end

it "should run with time" do
time = Time.now
str = render_haml("= time_tag(time)", time: time)

str.should =~ /<time.+datetime=.?#{Regexp.escape time.iso8601}/
end

it "should run with duration" do
str = render_haml("= time_tag(time)", time: 3.hours + 30.minutes)
str.should =~ /datetime=.PT3H30M/
end

it "should run with time interval of 2 dates" do
time = [Time.parse("14 March 1879"), Time.parse("18 April 1955")]

str = render_haml("= time_tag_interval(*time, :format => '%d %h %Y')", time: time)
str.should =~ /<time.+datetime=.?#{Regexp.escape time[0].iso8601}\/#{Regexp.escape time[1].iso8601}/
end

it "should run with date and duration" do
time = [Time.parse("6 May 1989"), 150.hours]
str = render_haml("= time_tag_interval(*time, :format => :short)", time: time)
str.should =~ /<time.+datetime=.?#{Regexp.escape time[0].iso8601}\/P6DT6H/
end
end
end

0 comments on commit f6d506c

Please sign in to comment.