Created
August 25, 2011 07:20
-
-
Save Paxa/1170153 to your computer and use it in GitHub Desktop.
microdata attributes support for rails helpers and haml (requires gem mida)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ActiveRecord::Base | |
html_schema_type "http://schema.org/BlogPosting" | |
end | |
%article[post, :blogPosts] | |
= link_to post, :itemprop => "url" do | |
%h3[:name]>= post.title | |
.post_body[:articleBody]= post.body.html_safe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActionView | |
module Helpers | |
module TagHelper | |
BOOLEAN_ATTRIBUTES.merge(['itemscope', :itemscope]) | |
private | |
def tag_options(options, escape = true) | |
#p [options, escape, BOOLEAN_ATTRIBUTES] | |
unless options.blank? | |
attrs = [] | |
options.each_pair do |key, value| | |
if BOOLEAN_ATTRIBUTES.include?(key) | |
attrs << key.to_s if value | |
elsif !value.nil? | |
final_value = value.is_a?(Array) ? value.join(" ") : value | |
final_value = html_escape(final_value) if escape | |
attrs << %(#{key}="#{final_value}") | |
end | |
end | |
" #{attrs.sort * ' '}".html_safe unless attrs.empty? | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveRecord::Base | |
def self.html_schema_type(value = nil) | |
return @html_schema_type unless value | |
@html_schema_type = value | |
end | |
def html_schema_type | |
self.class.html_schema_type | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this hack looks at active-record object's :html_schema_type field and adds itemscope, itemid and itemtype to element | |
# example: | |
# | |
# %article[post] | |
# => <article class="post" itemscope itemtype="http://schema.org/BlogPosting" itemid="1"> | |
# | |
# %section[Mida(:Blog)] | |
# => <section itemscope itemtype="http://schema.org/Blog"> | |
# | |
# %span[:title] Hello | |
# => <span itemprop="title">Hello</span> | |
class Haml::Buffer | |
def parse_object_ref(ref) | |
options = {} | |
ref.each do |obj| | |
self.class.merge_attrs(options, process_object_ref(obj)) | |
end | |
options | |
end | |
def process_object_ref(obj) | |
return {} if !obj | |
if obj.is_a?(Symbol) | |
# symbol => "itemprop" attribute | |
return {'itemprop' => obj.to_s} | |
elsif obj.kind_of?(Mida::Vocabulary) | |
# Mida::Vocabulary => itemprop and itemtype | |
return {:itemscope => true, :itemtype => obj.itemtype.source} | |
else | |
options = {} | |
options[:class] = obj.respond_to?(:haml_object_ref) ? obj.haml_object_ref : underscore(obj.class) | |
options[:id] = "#{options[:class]}_#{obj.id || 'new'}" if obj.respond_to?(:id) | |
# my hack for microdata attributes | |
if obj.respond_to?(:html_schema_type) | |
options[:itemscope] = true | |
options[:itemid] = obj.id | |
raise "No vocabulary found (#{ref.html_schema_type})" unless Mida::Vocabulary.find(obj.html_schema_type) | |
options[:itemtype] = obj.html_schema_type | |
end | |
return options | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mida::Document | |
attr_reader :doc | |
end | |
class Mida::Item | |
def get_prop(prop_name) | |
@properties[prop_name.to_s] | |
end | |
def get_string_prop(prop_name) | |
get_prop(prop_name).map(&:to_s).join(" ") | |
end | |
end | |
def Mida(itemtype) | |
if itemtype.is_a?(Symbol) | |
itemtype = "http://schema.org/#{itemtype}" | |
end | |
Mida::Vocabulary.find(itemtype) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment