Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #2 from carpodaster/master

Support for hreview-aggregate
  • Loading branch information...
commit b89778ac20dc29821eac7ede528d163d77ac754d 2 parents d5eb316 + 6ea2ff9
Ricardo Shiota Yasuda authored
2  .gitignore
... ...
@@ -0,0 +1,2 @@
  1
+nbproject
  2
+*.swp
7  Gemfile
... ...
@@ -0,0 +1,7 @@
  1
+source "http://rubygems.org"
  2
+
  3
+group :test do
  4
+	gem 'activesupport', '>= 2.3', :require => "active_support"
  5
+	gem 'actionpack',    '>= 2.3', :require => "action_controller"
  6
+	gem 'redgreen' # soz for the noise, but I like it pretty ^^
  7
+end
38  Gemfile.lock
... ...
@@ -0,0 +1,38 @@
  1
+GEM
  2
+  remote: http://rubygems.org/
  3
+  specs:
  4
+    abstract (1.0.0)
  5
+    actionpack (3.0.7)
  6
+      activemodel (= 3.0.7)
  7
+      activesupport (= 3.0.7)
  8
+      builder (~> 2.1.2)
  9
+      erubis (~> 2.6.6)
  10
+      i18n (~> 0.5.0)
  11
+      rack (~> 1.2.1)
  12
+      rack-mount (~> 0.6.14)
  13
+      rack-test (~> 0.5.7)
  14
+      tzinfo (~> 0.3.23)
  15
+    activemodel (3.0.7)
  16
+      activesupport (= 3.0.7)
  17
+      builder (~> 2.1.2)
  18
+      i18n (~> 0.5.0)
  19
+    activesupport (3.0.7)
  20
+    builder (2.1.2)
  21
+    erubis (2.6.6)
  22
+      abstract (>= 1.0.0)
  23
+    i18n (0.5.0)
  24
+    rack (1.2.2)
  25
+    rack-mount (0.6.14)
  26
+      rack (>= 1.0.0)
  27
+    rack-test (0.5.7)
  28
+      rack (>= 1.0)
  29
+    redgreen (1.2.2)
  30
+    tzinfo (0.3.26)
  31
+
  32
+PLATFORMS
  33
+  ruby
  34
+
  35
+DEPENDENCIES
  36
+  actionpack (>= 2.3)
  37
+  activesupport (>= 2.3)
  38
+  redgreen
130  lib/microformats_helper.rb
... ...
@@ -1,8 +1,8 @@
1 1
 # 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).
2 2
 # 
3  
-# Currently only the hCard microformat is available.
  3
+# Currently only the hCard and hreview-aggregated microformat is available.
4 4
 # 
5  
-# Author:: Ricardo Shiota Yasuda
  5
+# Author:: Ricardo Shiota Yasuda (contributions by Carsten Zimmermann)
6 6
 # Copyright:: Copyright (c) 2008 Ricardo Shiota Yasuda
7 7
 # License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
8 8
 #
@@ -37,7 +37,7 @@ module MicroformatsHelper
37 37
   # * +tel+ - Provide a hash with the phone types and numbers
38 38
   # * +url+ - Add a link to a site in the name
39 39
   # * +email+ - Add a link to a mailto: address
40  
-  def hcard(values)
  40
+  def hcard(values, escape = false)
41 41
 
42 42
     # support for additional HTML options, e.g. id
43 43
     html_options = (values[:html] || {})
@@ -51,59 +51,60 @@ def hcard(values)
51 51
 
52 52
     # Figure out the name. Either FN or combination of family, additional, given.
53 53
     unless fn = values[:fn]
  54
+      fn = ""
54 55
       if prefix = values[:prefix]
55  
-        fn = content_tag("span", prefix, :class => "honorific-prefix")
  56
+        fn += content_tag("span", prefix, {:class => "honorific-prefix"}, escape)
56 57
       end
57  
-      fn = "" unless fn
58 58
       if org = values[:org]
59  
-        fn += " " + content_tag("span", org, :class => "org")
  59
+        fn += " " + content_tag("span", org, {:class => "org"}, escape)
60 60
       end
61 61
       if given = values[:given]
62  
-        fn += " " + content_tag("span", given, :class => "given-name")
  62
+        fn += " " + content_tag("span", given, {:class => "given-name"}, escape)
63 63
       end
64 64
       if additional = values[:additional]
65  
-        fn += " " + content_tag("span", additional, :class => 'additional-name')
  65
+        fn += " " + content_tag("span", additional, {:class => 'additional-name'}, escape)
66 66
       end
67 67
       if family = values[:family]
68  
-        fn += " " + content_tag("span", family, :class => "family-name")
  68
+        fn += " " + content_tag("span", family, {:class => "family-name"}, escape)
69 69
       end
70 70
       if suffix = values[:suffix]
71  
-        fn += ", " + content_tag("span", suffix, :class => "honorific-suffix")
  71
+        fn += ", " + content_tag("span", suffix, {:class => "honorific-suffix"}, escape)
72 72
       end
73 73
     end
74 74
 
  75
+
75 76
     # Create link or span. Support passing url_for options.
76 77
     if url = values[:url]
77  
-      container_fn = link_to(fn, url, html_options.update(:class=>"fn n url"))
  78
+      container_fn = link_to(fn, url, html_options.update(:class=>"fn n url"), escape)
78 79
     else
79  
-      container_fn = "\n" + content_tag("span", fn, :class => "fn n") + "\n"
  80
+      container_fn = "\n" + content_tag("span", fn, {:class => "fn n"}, escape) + "\n"
80 81
     end
81 82
 
  83
+    adr = ""
82 84
     if street = values[:street]
83 85
       address = true
84  
-      adr = content_tag("span", street, :class => "street-address")
  86
+      adr += content_tag("span", street, {:class => "street-address"}, escape)
85 87
     end
86  
-    adr = "" unless adr
87 88
     if locality = values[:locality]
88 89
       address = true
89  
-      adr += " " + content_tag("span", locality, :class => "locality")
  90
+      adr += " " + content_tag("span", locality, {:class => "locality"}, escape)
90 91
     end
91 92
     if region = values[:region]
92 93
       address = true
93  
-      adr += " - " + content_tag("span", region, :class => "region")
  94
+      adr += " - " + content_tag("span", region, {:class => "region"}, escape)
94 95
     end
95 96
     if postal_code = values[:postal_code]
96 97
       address = true
97  
-      adr += " " + content_tag("span", postal_code, :class => "postal-code")
  98
+      adr += " " + content_tag("span", postal_code, {:class => "postal-code"}, escape)
98 99
     end
99 100
     if country = values[:country]
100 101
       address = true
101  
-      adr += " " + content_tag("span", country, :class => "country")
  102
+      adr += " " + content_tag("span", country, {:class => "country"}, escape)
102 103
     end
103  
-    span_adr = (address == true) ? "\n" + content_tag("span", adr, :class => "adr") + "\n" : ""
  104
+    span_adr = (address == true) ? "\n" + content_tag("span", adr, {:class => "adr"}, escape) + "\n" : ""
104 105
 
105 106
     if email = values[:email]
106  
-      span_email = "\n" + link_to(email, "mailto:#{email}", :class => "email") + "\n"
  107
+      span_email = "\n" + link_to(email, "mailto:#{email}", {:class => "email"}, escape) + "\n"
107 108
     else
108 109
       span_email = ""
109 110
     end
@@ -111,14 +112,97 @@ def hcard(values)
111 112
     if tel = values[:tel]
112 113
       tel_values = ""
113 114
       tel.sort.reverse.each do |k,v|
114  
-        tel_values += content_tag("span", "#{k.capitalize}: ", :class => "tel-label-#{k} type") + content_tag("span", v, :class => "value") + "\n"
  115
+        tel_values += content_tag("span", "#{k.capitalize}: ", {:class => "tel-label-#{k} type"}, escape) + content_tag("span", v, {:class => "value"}, escape) + "\n"
115 116
       end
116  
-      span_tel = "\n" + content_tag("span", tel_values, :class => "tel") + "\n"
  117
+      span_tel = "\n" + content_tag("span", tel_values, {:class => "tel"}, escape) + "\n"
117 118
     else
118 119
       span_tel = ""
119 120
     end
120 121
 
121  
-    content_tag("span", container_fn + span_adr + span_email + span_tel, html_options.update(:class => classes))
  122
+    content_tag("span", container_fn + span_adr + span_email + span_tel, html_options.update(:class => classes), escape)
  123
+  end
  124
+
  125
+
  126
+  # Built according to http://www.google.com/support/webmasters/bin/answer.py?answer=146645
  127
+  # Currently only intended to be read by machines as the displayed information
  128
+  # makes little sense without knowledge of its semantical context (ie. the class
  129
+  # attributes).
  130
+  # 
  131
+  # === Parameters
  132
+  # * +values+: a (nested) hash, see below
  133
+  # * +escape+: passed to content_tag, defaults to false
  134
+  # 
  135
+  # === Values
  136
+  # The following keys within the +values+ hash are supported:
  137
+  # * +html+: additional html attributes to pass to the container content_tag element
  138
+  # * +class+: additional css class for the container content_tag element
  139
+  # * +fn+: the name of reviewed item
  140
+  # * +rating+: hash with keys +average+, +best+ and +worst+, the latter two being
  141
+  #             semantically optional. Corresponding values are integers
  142
+  # * +count+: Integer. The number of aggregated reviews
  143
+  # * +votes+: Integer. People who commented without writing a review. Think Facebook likes.
  144
+  # 
  145
+  # === Example
  146
+  # hreview_aggregate(:fn => "John Doe's Pizza", :count => 3, :rating => { :average => 4, :best => 10 } )
  147
+  #
  148
+  # => <span class="hreview-aggregate">
  149
+  #      <span class="item">
  150
+  #        <span class="fn">John Doe's Pizza</span>
  151
+  #      </span>
  152
+  #      <span class="rating">
  153
+  #        <span class="average">4</span>
  154
+  #        <span class="best">10</span>
  155
+  #      </span>
  156
+  #      <span class="count">3</span>
  157
+  #    </span>
  158
+  #
  159
+  def hreview_aggregate(values, escape = false)
  160
+    values.symbolize_keys!
  161
+    # support for additional HTML options, e.g. id
  162
+    html_options = (values[:html] || {})
  163
+
  164
+    # support for additional classes
  165
+    if classes = values[:class]
  166
+      classes << " hreview-aggregate"
  167
+    else
  168
+      classes = "hreview-aggregate"
  169
+    end
  170
+
  171
+    fn = ""
  172
+    if values[:fn]
  173
+      # <span class="item">
  174
+      #   <span class="fn">Marios Pizza</span>
  175
+      # </span>
  176
+      fn += content_tag("span", content_tag("span", values[:fn], {:class => "fn"}, escape), {:class => "item"}, escape)
  177
+    end
  178
+
  179
+    rating = ""
  180
+    if values[:rating]
  181
+      # <span class="rating">
  182
+      #   <span class="average">9</span>
  183
+      #   <span class="best">10</span>
  184
+      # </span>
  185
+      average = content_tag("span", values[:rating][:average],  {:class => "average"}, escape) if values[:rating][:average]
  186
+      best    = content_tag("span", values[:rating][:best],     {:class => "best"},    escape) if values[:rating][:best]
  187
+      worst   = content_tag("span", values[:rating][:worst],    {:class => "worst"},   escape) if values[:rating][:worst]
  188
+      rating += content_tag("span", [average, best, worst].join("\n"), {:class => "rating"},  escape)
  189
+    end
  190
+
  191
+    count = ""
  192
+    if values[:count]
  193
+      # <span class="count">42</span>
  194
+      count += content_tag("span", values[:count], {:class => "count"}, escape)
  195
+    end
  196
+    
  197
+    votes = ""
  198
+    if values[:votes]
  199
+      # <span class="votes">4711</span>
  200
+      votes += content_tag("span", values[:votes], {:class => "votes"}, escape)
  201
+    end
  202
+
  203
+    # glue everything together
  204
+    content_tag("span", [fn, rating, count, votes].join("\n"), html_options.update(:class => classes), escape)
  205
+
122 206
   end
123 207
 
124 208
 end
72  test/microformats_helper_test.rb
... ...
@@ -1,18 +1,15 @@
1  
-# Rubygems is where Rails is located
2  
-require 'rubygems'
3  
-require File.dirname(__FILE__) + '/../lib/microformats_helper'
4  
-# Here's the helper file we need
5  
-require 'test/unit'
6  
-require 'action_controller'
7  
-require 'action_controller/test_process'
  1
+require File.join(File.dirname(__FILE__), 'test_helper')
8 2
 
9  
-class MicroformatsHelperTest < Test::Unit::TestCase
  3
+class MicroformatsHelperTest < ActionController::TestCase
10 4
 
11 5
   # This is the helper with the 'tag' method
12 6
   include ActionView::Helpers::TagHelper
13 7
   include ActionView::Helpers::UrlHelper
14 8
   include MicroformatsHelper
15 9
 
  10
+  # #######################################################################
  11
+  # Testing hcard
  12
+
16 13
   def test_hcard_fn
17 14
     output = hcard(:fn => "Ricardo Yasuda")
18 15
     assert_equal "<span class=\"vcard\">\n<span class=\"fn n\">Ricardo Yasuda</span>\n</span>", output
@@ -57,4 +54,61 @@ def test_hcard_tel
57 54
     output = hcard(:fn => "John Doe", :tel => { "home" => "555-5555", "fax" => "544-5544" })
58 55
     assert_equal "<span class=\"vcard\">\n<span class=\"fn n\">John Doe</span>\n\n<span class=\"tel\"><span class=\"tel-label-home type\">Home: </span><span class=\"value\">555-5555</span>\n<span class=\"tel-label-fax type\">Fax: </span><span class=\"value\">544-5544</span>\n</span>\n</span>", output
59 56
   end
60  
-end
  57
+
  58
+  def test_hcard_html_safeness
  59
+    assert hcard(:fn => "John Doe").html_safe?
  60
+  end
  61
+
  62
+
  63
+  # #######################################################################
  64
+  # Testing hreview-aggregate
  65
+
  66
+  def test_hreview_aggregate_custom_html_optios_support
  67
+    output = hreview_aggregate(:html => { :id => "foo"} )
  68
+    assert_select_from output, "span#foo.hreview-aggregate"
  69
+  end
  70
+
  71
+  def test_hreview_aggregate_custom_class_support
  72
+    output = hreview_aggregate(:class => "invisible robots")
  73
+    assert_select_from output, "span.hreview-aggregate.invisible.robots"
  74
+  end
  75
+
  76
+  def test_hreview_aggregate_item
  77
+    output = hreview_aggregate(:fn => "John Doe's Pawn Shop")
  78
+    assert_select_from output, "span.hreview-aggregate" do
  79
+      assert_select "span.item" do
  80
+        assert_select "span.fn", :text => "John Doe's Pawn Shop"
  81
+      end
  82
+    end
  83
+  end
  84
+
  85
+  def test_hreview_aggregate_rating
  86
+    output = hreview_aggregate(:rating => { :average => 7, :best => 10, :worst => 2 } )
  87
+    assert_select_from output, "span.hreview-aggregate" do
  88
+      assert_select "span.rating" do
  89
+        assert_select "span.average", :text => "7"
  90
+        assert_select "span.best",    :text => "10"
  91
+        assert_select "span.worst",   :text => "2"
  92
+      end
  93
+    end
  94
+  end
  95
+
  96
+  def test_hreview_aggregate_count
  97
+    output = hreview_aggregate(:count => 42)
  98
+    assert_select_from output, "span.hreview-aggregate" do
  99
+      assert_select "span.count", :text => "42"
  100
+    end
  101
+  end
  102
+
  103
+  def test_hreview_aggregate_votes
  104
+    output = hreview_aggregate(:votes => 4711)
  105
+    assert_select_from output, "span.hreview-aggregate" do
  106
+      assert_select "span.votes", :text => "4711"
  107
+    end
  108
+  end
  109
+
  110
+  def test_hreview_aggregate_html_safeness
  111
+    assert hreview_aggregate(:fn => "John Doe's Pawn Shop").html_safe?
  112
+  end
  113
+
  114
+end
18  test/test_helper.rb
... ...
@@ -0,0 +1,18 @@
  1
+begin
  2
+  require "rubygems"
  3
+  require "bundler/setup"
  4
+rescue LoadError
  5
+  raise "Could not load the bundler gem. Install it with `gem install bundler` and do a `bundle install`."
  6
+end
  7
+# Here's the helper file we need
  8
+require File.dirname(__FILE__) + '/../lib/microformats_helper'
  9
+require 'test/unit'
  10
+Bundler.require(:test)
  11
+
  12
+class ActiveSupport::TestCase
  13
+  def assert_select_from(text, *args)
  14
+    @selected = HTML::Document.new(text).root.children
  15
+    assert_select(*args)
  16
+    yield @selected if block_given?
  17
+  end
  18
+end

0 notes on commit b89778a

Please sign in to comment.
Something went wrong with that request. Please try again.