item 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +21 -12
- data/lib/item/version.rb +1 -1
- data/lib/item/view_helpers.rb +26 -6
- data/test/view_helpers_test.rb +25 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f659a6a1b8a33fbe1c3b78fb764927699ce50dd1
|
4
|
+
data.tar.gz: d1f938c0a4a94e67044e6ffb0a48846f91c2b992
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9e50b82f80724ae893663fb0616522c28ed326d8cd37f774f313c43c7a2de8fda7305ff5b07b3eb669b0acb3404da9f166af45a9e439b8cde999c0bda4ca4a0
|
7
|
+
data.tar.gz: 0105194be5e2f59c2913d4f83f14157545ab22331413a69e717d4bd10a715760f7cd349613353d751e69e803dab205637ab493fb482245be46ae94dcb1eb888e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -11,7 +11,6 @@ This can help attract visitors to your site:
|
|
11
11
|
|
12
12
|
**The problem with Microdata:** Normally, implementing these Microdata can make your views quite messy because you have a lot of "extra" content other than your regular view content.
|
13
13
|
|
14
|
-
|
15
14
|
**Item:** Item solves the problem by adding two helpers, `scope` and `prop`, that you can use in your views.
|
16
15
|
This makes it easy to add microdata scopes and properties without cluttering the view. Item is made to maximize ease of writing (and joy, of course – it *is* Ruby), while minimizing the code needed to add microdata to your views.
|
17
16
|
|
@@ -36,13 +35,11 @@ $ bundle
|
|
36
35
|
To define an `itemscope`:
|
37
36
|
|
38
37
|
```erb
|
39
|
-
|
38
|
+
<%= scope :product do %>
|
40
39
|
...
|
41
40
|
<% end %>
|
42
41
|
```
|
43
42
|
|
44
|
-
You don't need `<%=` before scopes, because we see it as just a definition, and not something that is inserted into the view, even though this is done behind the scenes.
|
45
|
-
|
46
43
|
The above will generate the following HTML:
|
47
44
|
|
48
45
|
```html
|
@@ -51,6 +48,14 @@ The above will generate the following HTML:
|
|
51
48
|
</div>
|
52
49
|
```
|
53
50
|
|
51
|
+
If you need the scope to be another tag than `<div>` or e.g. need to set a class, you can do it like this:
|
52
|
+
|
53
|
+
```erb
|
54
|
+
<%= scope :product, tag: :span, class: "a-class" do %>
|
55
|
+
...
|
56
|
+
<% end %>
|
57
|
+
```
|
58
|
+
|
54
59
|
### Properties
|
55
60
|
|
56
61
|
To define an `itemprop` span:
|
@@ -65,18 +70,22 @@ This will generate the following HTML:
|
|
65
70
|
<span itemprop="name">My Product Name</span>
|
66
71
|
```
|
67
72
|
|
73
|
+
If you need the property to be another tag than span `<span>` or e.g. need to set a class, you can do it like this:
|
74
|
+
|
75
|
+
```erb
|
76
|
+
<%= prop :name, "My Product Name", tag: :div, class: "a-class" %>
|
77
|
+
```
|
78
|
+
|
68
79
|
#### Scoped properties
|
69
80
|
|
70
81
|
To define a property that is also a scope:
|
71
82
|
|
72
83
|
```erb
|
73
|
-
|
84
|
+
<%= prop :review do %>
|
74
85
|
<%= prop :name, "Pete Anderson" %>
|
75
86
|
<% end %>
|
76
87
|
```
|
77
88
|
|
78
|
-
You don't need `<%=` before scoped properties, because we see this as a definition of a scope. The HTML is inserted automatically.
|
79
|
-
|
80
89
|
The above will generate the following HTML:
|
81
90
|
|
82
91
|
```html
|
@@ -90,7 +99,7 @@ The above will generate the following HTML:
|
|
90
99
|
Sometimes you need to define a type on scoped properties when this cannot be inferred from the property name. To set a type:
|
91
100
|
|
92
101
|
```erb
|
93
|
-
|
102
|
+
<%= prop :review_rating, type: :rating do %>
|
94
103
|
...
|
95
104
|
<% end %>
|
96
105
|
```
|
@@ -141,12 +150,12 @@ The following is based on the [Product example](http://schema.org/Product) on [S
|
|
141
150
|
<%= itemprop :name, "Kenmore White 17\" Microwave" %>
|
142
151
|
<%= image_tag "kenmore-microwave-17in.jpg" %>
|
143
152
|
|
144
|
-
|
153
|
+
<%= prop :aggregate_rating do %>
|
145
154
|
Rated <%= prop :rating_value, 3.5 %>/5
|
146
155
|
based on <%= prop :review_count, 11 %> custom reviews
|
147
156
|
<% end %>
|
148
157
|
|
149
|
-
|
158
|
+
<%= prop :offers, type: :offer do %>
|
150
159
|
<%= prop :price, "$55.00" %>
|
151
160
|
<%= prop :availability, :in_stock %>
|
152
161
|
In Stock
|
@@ -158,12 +167,12 @@ The following is based on the [Product example](http://schema.org/Product) on [S
|
|
158
167
|
Add-A-Minute and Child Lock." %>
|
159
168
|
|
160
169
|
Customer reviews:
|
161
|
-
|
170
|
+
<%= prop :review do %>
|
162
171
|
<%= prop :name, "Not a happy camper" %> -
|
163
172
|
by <%= prop :author, "Ellie" %>,
|
164
173
|
<%= prop date_published: "2011-04-01" %>
|
165
174
|
April 1, 2011
|
166
|
-
|
175
|
+
<%= prop :review_rating, type: :rating do %>
|
167
176
|
<%= prop worst_rating: 1 %>
|
168
177
|
<%= prop :rating_value, 1 %> /
|
169
178
|
<%= prop :best_rating, 5 %> stars
|
data/lib/item/version.rb
CHANGED
data/lib/item/view_helpers.rb
CHANGED
@@ -1,27 +1,47 @@
|
|
1
1
|
module Item
|
2
2
|
module ViewHelpers
|
3
3
|
def scope(key, options = {}, &block)
|
4
|
-
|
4
|
+
tag = options.delete(:tag) || :div
|
5
|
+
tag_options = { itemscope: true, itemtype: Util.itemtype(key) }.merge(options)
|
6
|
+
content_tag(tag, tag_options, &block)
|
5
7
|
end
|
6
8
|
|
7
9
|
def prop(key, value = nil, options = {}, &block)
|
8
10
|
if key.is_a?(Hash)
|
9
|
-
key.map
|
11
|
+
key.map do |key, value|
|
12
|
+
if value.is_a?(Symbol)
|
13
|
+
link_prop(key, value)
|
14
|
+
else
|
15
|
+
meta_prop(key, value)
|
16
|
+
end
|
17
|
+
end.join("\n").html_safe
|
10
18
|
elsif value.is_a?(Symbol)
|
11
|
-
|
19
|
+
link_prop(key, value)
|
12
20
|
else
|
13
21
|
options, value = value, nil if value.is_a?(Hash)
|
14
22
|
itemprop = Util.itemprop(key)
|
15
23
|
|
16
24
|
if block
|
17
|
-
|
18
|
-
|
25
|
+
tag = options.delete(:tag) || :div
|
26
|
+
itemtype = Util.itemtype(options.delete(:type) || key)
|
27
|
+
tag_options = { itemprop: itemprop, itemscope: true, itemtype: itemtype }.merge(options)
|
28
|
+
content_tag(tag, tag_options, &block)
|
19
29
|
else
|
20
|
-
|
30
|
+
tag = options.delete(:tag) || :span
|
31
|
+
tag_options = { itemprop: itemprop }.merge(options)
|
32
|
+
content_tag(tag, value, tag_options)
|
21
33
|
end
|
22
34
|
end
|
23
35
|
end
|
24
36
|
|
37
|
+
def link_prop(key, value)
|
38
|
+
tag(:link, itemprop: Util.itemprop(key), href: Util.href(value))
|
39
|
+
end
|
40
|
+
|
41
|
+
def meta_prop(key, value)
|
42
|
+
tag(:meta, itemprop: Util.itemprop(key), content: value)
|
43
|
+
end
|
44
|
+
|
25
45
|
module Util
|
26
46
|
class << self
|
27
47
|
def itemtype(name)
|
data/test/view_helpers_test.rb
CHANGED
@@ -12,6 +12,15 @@ class ViewHelpersTest < ActionView::TestCase
|
|
12
12
|
content
|
13
13
|
end
|
14
14
|
|
15
|
+
test "scope options" do
|
16
|
+
content = scope :aggregate_rating, tag: :something, class: "some-class" do
|
17
|
+
"content"
|
18
|
+
end
|
19
|
+
|
20
|
+
assert_equal %{<something class="some-class" itemscope="itemscope" itemtype="http://schema.org/AggregateRating">content</something>},
|
21
|
+
content
|
22
|
+
end
|
23
|
+
|
15
24
|
test "prop" do
|
16
25
|
content = prop(:aggregate_rating, "content")
|
17
26
|
|
@@ -19,6 +28,13 @@ class ViewHelpersTest < ActionView::TestCase
|
|
19
28
|
content
|
20
29
|
end
|
21
30
|
|
31
|
+
test "prop options" do
|
32
|
+
content = prop(:aggregate_rating, "content", tag: :something, class: "some-class")
|
33
|
+
|
34
|
+
assert_equal %{<something class="some-class" itemprop="aggregateRating">content</something>},
|
35
|
+
content
|
36
|
+
end
|
37
|
+
|
22
38
|
test "hidden prop" do
|
23
39
|
content = prop(aggregate_rating: "Rating content", title: "Title content")
|
24
40
|
|
@@ -42,6 +58,15 @@ class ViewHelpersTest < ActionView::TestCase
|
|
42
58
|
content
|
43
59
|
end
|
44
60
|
|
61
|
+
test "prop block with options" do
|
62
|
+
content = prop :aggregate_rating, tag: :something, class: "some-class" do
|
63
|
+
"content"
|
64
|
+
end
|
65
|
+
|
66
|
+
assert_equal %{<something class="some-class" itemprop="aggregateRating" itemscope="itemscope" itemtype="http://schema.org/AggregateRating">content</something>},
|
67
|
+
content
|
68
|
+
end
|
69
|
+
|
45
70
|
test "prop block with type" do
|
46
71
|
content = prop :aggregate_rating, type: :other_type do
|
47
72
|
"content"
|