Skip to content

Commit

Permalink
Completed hreview-aggregate, although it's quite human-unfriendly
Browse files Browse the repository at this point in the history
  • Loading branch information
carpodaster committed Apr 20, 2011
1 parent 56d1491 commit f371f12
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
72 changes: 68 additions & 4 deletions lib/microformats_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Microformats Helper is a plugin for generating content-rich tags in HTML files, following Microformats standards. For more information about Microformats, check its website (http://microformats.org).
#
# Currently only the hCard microformat is available.
# Currently only the hCard and hreview-aggregated microformat is available.
#
# Author:: Ricardo Shiota Yasuda
# Author:: Ricardo Shiota Yasuda (contributions by Carsten Zimmermann)
# Copyright:: Copyright (c) 2008 Ricardo Shiota Yasuda
# License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
#
Expand Down Expand Up @@ -122,6 +122,40 @@ def hcard(values, escape = false)
content_tag("span", container_fn + span_adr + span_email + span_tel, html_options.update(:class => classes), escape)
end


# Built according to http://www.google.com/support/webmasters/bin/answer.py?answer=146645
# Currently only intended to be read by machines as the displayed information
# makes little sense without knowledge of its semantical context (ie. the class
# attributes).
#
# === Parameters
# * +values+: a (nested) hash, see below
# * +escape+: passed to content_tag, defaults to false
#
# === Values
# The following keys within the +values+ hash are supported:
# * +html+: additional html attributes to pass to the container content_tag element
# * +class+: additional css class for the container content_tag element
# * +fn+: the name of reviewed item
# * +rating+: hash with keys +average+, +best+ and +worst+, the latter two being
# semantically optional. Corresponding values are integers
# * +count+: Integer. The number of aggregated reviews
# * +votes+: Integer. People who commented without writing a review. Think Facebook likes.
#
# === Example
# hreview_aggregate(:fn => "John Doe's Pizza", :count => 3, :rating => { :average => 4, :best => 10 } )
#
# => <span class="hreview-aggregate">
# <span class="item">
# <span class="fn">John Doe's Pizza</span>
# </span>
# <span class="rating">
# <span class="average">4</span>
# <span class="best">10</span>
# </span>
# <span class="count">3</span>
# </span>
#
def hreview_aggregate(values, escape = false)
values.symbolize_keys!
# support for additional HTML options, e.g. id
Expand All @@ -134,10 +168,40 @@ def hreview_aggregate(values, escape = false)
classes = "hreview-aggregate"
end

fn = values[:fn] ? content_tag("span", values[:fn], {:class => "fn"}, escape) : ""
fn = ""
if values[:fn]
# <span class="item">
# <span class="fn">Marios Pizza</span>
# </span>
fn += content_tag("span", content_tag("span", values[:fn], {:class => "fn"}, escape), {:class => "item"}, escape)
end

rating = ""
if values[:rating]
# <span class="rating">
# <span class="average">9</span>
# <span class="best">10</span>
# </span>
average = content_tag("span", values[:rating][:average], {:class => "average"}, escape) if values[:rating][:average]
best = content_tag("span", values[:rating][:best], {:class => "best"}, escape) if values[:rating][:best]
worst = content_tag("span", values[:rating][:worst], {:class => "worst"}, escape) if values[:rating][:worst]
rating += content_tag("span", [average, best, worst].join("\n"), {:class => "rating"}, escape)
end

count = ""
if values[:count]
# <span class="count">42</span>
count += content_tag("span", values[:count], {:class => "count"}, escape)
end

votes = ""
if values[:votes]
# <span class="votes">4711</span>
votes += content_tag("span", values[:votes], {:class => "votes"}, escape)
end

# glue everything together
content_tag("span", fn, html_options.update(:class => classes), escape)
content_tag("span", [fn, rating, count, votes].join("\n"), html_options.update(:class => classes), escape)

end

Expand Down
40 changes: 39 additions & 1 deletion test/microformats_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,48 @@ def test_hcard_tel
# #######################################################################
# Testing hreview-aggregate

def test_hreview_aggregate_custom_html_optios_support
output = hreview_aggregate(:html => { :id => "foo"} )
assert_select_from output, "span#foo.hreview-aggregate"
end

def test_hreview_aggregate_custom_class_support
output = hreview_aggregate(:class => "invisible robots")
assert_select_from output, "span.hreview-aggregate.invisible.robots"
end

def test_hreview_aggregate_item
output = hreview_aggregate(:fn => "John Doe's Pawn Shop")
assert_select_from output, "span.hreview-aggregate" do
assert_select "span.fn", :text => "John Doe's Pawn Shop"
assert_select "span.item" do
assert_select "span.fn", :text => "John Doe's Pawn Shop"
end
end
end

def test_hreview_aggregate_rating
output = hreview_aggregate(:rating => { :average => 7, :best => 10, :worst => 2 } )
assert_select_from output, "span.hreview-aggregate" do
assert_select "span.rating" do
assert_select "span.average", :text => "7"
assert_select "span.best", :text => "10"
assert_select "span.worst", :text => "2"
end
end
end

def test_hreview_aggregate_count
output = hreview_aggregate(:count => 42)
assert_select_from output, "span.hreview-aggregate" do
assert_select "span.count", :text => "42"
end
end

def test_hreview_aggregate_votes
output = hreview_aggregate(:votes => 4711)
assert_select_from output, "span.hreview-aggregate" do
assert_select "span.votes", :text => "4711"
end
end

end

0 comments on commit f371f12

Please sign in to comment.