An open API service indexing awesome lists of open source software.

https://github.com/paxa/green_monkey

Rails 4 & 5 microdata helpers
https://github.com/paxa/green_monkey

haml microdata rails ruby

Last synced: 3 months ago
JSON representation

Rails 4 & 5 microdata helpers

Awesome Lists containing this project

README

          

# Green Monkey

## About

This Gem allows you to make html-layout with microdata properties easier. It works with ruby 2.2+, rails 4, rails 5, haml 5+.

![Green Monkey](http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Bijilo-Portrait-of-a-Callithrix-Monkey.jpg/320px-Bijilo-Portrait-of-a-Callithrix-Monkey.jpg)

## Install

Add to Gemfile

```ruby
gem "green_monkey", '0.2.2'
```

## API

Extends `ActiveModel`:

```ruby
class Post < ActiveRecord::Base
html_schema_type :BlogPosting
end

Post.html_schema_type # => Mida::SchemaOrg::BlogPosting
Post.new.html_schema_type # => Mida::SchemaOrg::BlogPosting

class User < ActiveRecord::Base
html_schema_type "http://example.com/User"
end

User.html_schema_type # => "http://example.com/User"
```

Extends rails' view helpers:

```ruby
# add correct support for itemscope attribute
content_tag(:div, :itemscope) # => "

" (instead of

# time_tag with datetime attribute
time_tag(Time.now) # =>

# time_tag with microdata-compatible time interval
time_tag_interval(time, 1.hour + 20.minutes) # =>

# time interval iso8601 helper
time_to_iso8601(1.hour + 20.minutes) # => "PT1H20M"

# shortcut to build "itemscope" and "itemtype" attributes
mida_scope(:Airline) # => ' itemscope itemtype="http://schema.org/Airline"'

# build a link with itemtype="http://data-vocabulary.org/Breadcrumb"
breadcrumb_link_to("Home", "/") # => Home
```

## Examples

### helper `time_tag`

It almost the same with rails' time_tag but make `datetime` attribute in iso8601 format, according to Microdata specifications.
Also it accepts Numeric values as duration for time intervals

Haml & HTML:

```haml
= time_tag post.created_at
= time_tag post.created_at, itemprop: "datePublished"
= time_tag 3.hours + 30.minutes
```

```html



```

### 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


```

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

```ruby
class User < ActiveRecord::Base
html_schema_type :Person
end

User.html_schema_type #=> Mida::SchemaOrg::Person
User.find(1).html_schema_type #=> Mida::SchemaOrg::Person
```

### Haml magic

Attribute `itemprop`

```haml
%span[:name]= item.name
Item name
```

`itemscope` and `iteptype` attributes

```haml
%article[Mida(:Event)] # =>


%article[Mida(:Event, :DrinkUp)] # =>


%article[@user] # =>

```

### Real examples
Haml:

```haml
%article[post]
= link_to "/posts/#{post.id}", :itemprop => "url" do
%h3[:name]>= post.title
.post_body[:articleBody]= post.body.html_safe
= time_tag(post.created_at, :itemprop => "datePublished")
```

Output:

```html


Hello world!



Some text

```

Haml:

```haml
%article[project]
%header
= link_to project.url, itemprop: 'url', target: "_blank" do
%h3[:name]<>= project.title
= " "
- if project.source_code.present?
= link_to "(source code)", project.source_code, class: "source_link", target: "_blank"
%section[:description]
= simple_format project.description

%ul
- for item_type in project.item_types.split(" ")
%li[Mida(:WebPageElement, :ItemType), :mentions]
%span[:name]= item_type
```

Output:

```html



Mida - A Microdata extractor/parser library for Ruby



(source code)



A Ruby Microdata parser/extractor






  • http://schema.org/Blog

```