Module: MicroformatsHelper::Helpers

Defined in:
lib/microformats_helper/helpers.rb

Instance Method Summary collapse

Instance Method Details

#hcard(values, escape = false) ⇒ Object

Options

All fields are optional but the name, you are required to provide at least one of the names below.

Name

  • fn - Formal Name: should be used when no other name is provided

  • given - Given Name

  • family - Family Name

  • additional - Additional Name: goes between given and family names

  • prefix - Honorific Prefix: for titles like Dr. or Sir

  • suffix - Honorific Suffix: for titles like M.D. or Jr

  • org - Organization name

Address

  • street - Street Address

  • locality - The city or similar

  • region - The state, county or similar

  • postal_code - ZIP number

  • country - The country

Contact

  • tel - Provide a hash with the phone types and numbers

  • url - Add a link to a site in the name

  • email - Add a link to a mailto: address



40
41
42
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/microformats_helper/helpers.rb', line 40

def hcard(values, escape = false)

  # support for additional HTML options, e.g. id
  html_options = (values[:html] || {})

  # support for more regional divider styles to seperate "locality" and "region" fields
  values[:divider] ||= " - "

  # support for additional classes
  if classes = values[:class]
    classes << " vcard"
  else
    classes = "vcard"
  end

  # Figure out the name. Either FN or combination of family, additional, given.
  unless fn = values[:fn]
    fn = ""
    if prefix = values[:prefix]
      fn += ("span", prefix, {:class => "honorific-prefix"}, escape)
    end
    if org = values[:org]
      fn += " " + ("span", org, {:class => "org"}, escape)
    end
    if given = values[:given]
      fn += " " + ("span", given, {:class => "given-name"}, escape)
    end
    if additional = values[:additional]
      fn += " " + ("span", additional, {:class => 'additional-name'}, escape)
    end
    if family = values[:family]
      fn += " " + ("span", family, {:class => "family-name"}, escape)
    end
    if suffix = values[:suffix]
      fn += ", " + ("span", suffix, {:class => "honorific-suffix"}, escape)
    end
  end


  # Create link or span. Support passing url_for options.
  if url = values[:url]
    container_fn = link_to(fn, url, html_options.update(:class=>"fn n url"), escape)
  else
    container_fn = "\n" + ("span", fn, {:class => "fn n"}, escape) + "\n"
  end

  adr = ""
  if street = values[:street]
    address = true
    adr += ("span", street, {:class => "street-address"}, escape)
  end
  if locality = values[:locality]
    address = true
    adr += " " + ("span", locality, {:class => "locality"}, escape)
  end
  if region = values[:region]
    address = true
    adr += values[:divider] + ("span", region, {:class => "region"}, escape)
  end
  if postal_code = values[:postal_code]
    address = true
    adr += " " + ("span", postal_code, {:class => "postal-code"}, escape)
  end
  if country = values[:country]
    address = true
    adr += " " + ("span", country, {:class => "country"}, escape)
  end
  span_adr = (address == true) ? "\n" + ("span", adr, {:class => "adr"}, escape) + "\n" : ""

  if email = values[:email]
    span_email = "\n" + link_to(email, "mailto:#{email}", {:class => "email"}, escape) + "\n"
  else
    span_email = ""
  end

  if tel = values[:tel]
    tel_values = ""
    tel.sort.reverse.each do |k,v|
      tel_values += ("span", "#{k.capitalize}: ", {:class => "tel-label-#{k} type"}, escape) + ("span", v, {:class => "value"}, escape) + "\n"
    end
    span_tel = "\n" + ("span", tel_values, {:class => "tel"}, escape) + "\n"
  else
    span_tel = ""
  end

  ("span", container_fn + span_adr + span_email + span_tel, html_options.update(:class => classes), escape)
end

#hreview_aggregate(values, escape = false) ⇒ Object

Built according to 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>


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/microformats_helper/helpers.rb', line 162

def hreview_aggregate(values, escape = false)
  values.symbolize_keys!
  # support for additional HTML options, e.g. id
  html_options = (values[:html] || {})

  # support for additional classes
  if classes = values[:class]
    classes << " hreview-aggregate"
  else
    classes = "hreview-aggregate"
  end

  fn = ""
  if values[:fn]
    # <span class="item">
    #   <span class="fn">Marios Pizza</span>
    # </span>
    fn += ("span", ("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 = ("span", values[:rating][:average],  {:class => "average"}, escape) if values[:rating][:average]
    best    = ("span", values[:rating][:best],     {:class => "best"},    escape) if values[:rating][:best]
    worst   = ("span", values[:rating][:worst],    {:class => "worst"},   escape) if values[:rating][:worst]
    rating += ("span", [average, best, worst].join("\n"), {:class => "rating"},  escape)
  end

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

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

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

end