meta-tags 1.2.6 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +0 -1
- data/CHANGELOG.md +13 -0
- data/README.md +59 -8
- data/lib/meta_tags/controller_helper.rb +4 -4
- data/lib/meta_tags/version.rb +1 -1
- data/lib/meta_tags/view_helper.rb +27 -9
- data/spec/controller_helper_spec.rb +1 -1
- data/spec/meta_tags_spec.rb +67 -0
- data/spec/spec_helper.rb +10 -7
- metadata +8 -7
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
## 1.3.0 (February 13, 2013)
|
2
|
+
|
3
|
+
Features:
|
4
|
+
|
5
|
+
- Added Hash and Array as possible contents for the meta tags. Check README for details
|
6
|
+
- Added support of string meta tag names
|
7
|
+
- Allow to disable noindex and nofollow using `false` as a value
|
8
|
+
|
9
|
+
Bugfixes:
|
10
|
+
|
11
|
+
- Do not display title HTML tag when title is blank
|
12
|
+
- Do not display OpenGraph tags when content is empty
|
13
|
+
|
1
14
|
## 1.2.6 (March 4, 2012)
|
2
15
|
|
3
16
|
Features:
|
data/README.md
CHANGED
@@ -105,6 +105,29 @@ Further reading:
|
|
105
105
|
* [About rel="canonical"](http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=139394)
|
106
106
|
* [Canonicalization](http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=139066)
|
107
107
|
|
108
|
+
### Hashes
|
109
|
+
|
110
|
+
Any namespace can be built just passing any symbol name and a Hash. For example:
|
111
|
+
|
112
|
+
set_meta_tags :foo => {
|
113
|
+
:bar => "lorem",
|
114
|
+
:baz => {
|
115
|
+
:qux => "ipsum"
|
116
|
+
}
|
117
|
+
}
|
118
|
+
# <meta property="foo:bar" content="lorem"/>
|
119
|
+
# <meta property="foo:baz:qux" content="ipsum"/>
|
120
|
+
|
121
|
+
### Arrays
|
122
|
+
|
123
|
+
Repeated meta tags can be built just using an Array inside a Hash. For example:
|
124
|
+
|
125
|
+
set_meta_tags :og => {
|
126
|
+
:image = ["http://example.com/rock.jpg", "http://example.com/rock2.jpg"]
|
127
|
+
}
|
128
|
+
#<meta property="og:image" content="http://example.com/rock.jpg" />
|
129
|
+
#<meta property="og:image" content="http://example.com/rock2.jpg" />
|
130
|
+
|
108
131
|
### Open Graph
|
109
132
|
|
110
133
|
To turn your web pages into graph objects, you'll need to add Open Graph
|
@@ -112,21 +135,47 @@ protocol `<meta>` tags to your webpages. The tags allow you to specify
|
|
112
135
|
structured information about your web pages. The more information you provide, the more opportunities your web pages can be surfaced within Facebook today
|
113
136
|
and in the future. Here's an example for a movie page:
|
114
137
|
|
115
|
-
set_meta_tags :
|
116
|
-
:title
|
117
|
-
:type
|
118
|
-
:url
|
119
|
-
:image
|
138
|
+
set_meta_tags :og => {
|
139
|
+
:title => 'The Rock',
|
140
|
+
:type => 'video.movie',
|
141
|
+
:url => 'http://www.imdb.com/title/tt0117500/',
|
142
|
+
:image => 'http://ia.media-imdb.com/rock.jpg',
|
143
|
+
:video => {
|
144
|
+
:director => 'http://www.imdb.com/name/nm0000881/',
|
145
|
+
:writer => ['http://www.imdb.com/name/nm0918711/', 'http://www.imdb.com/name/nm0177018/']
|
146
|
+
}
|
120
147
|
}
|
121
148
|
# <meta property="og:title" content="The Rock"/>
|
122
|
-
# <meta property="og:type" content="movie"/>
|
149
|
+
# <meta property="og:type" content="video.movie"/>
|
123
150
|
# <meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/>
|
124
151
|
# <meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
|
152
|
+
# <meta property="og:video:director" content="http://www.imdb.com/name/nm0000881/"/>
|
153
|
+
# <meta property="og:video:writer" content="http://www.imdb.com/name/nm0918711/"/>
|
154
|
+
# <meta property="og:video:writer" content="http://www.imdb.com/name/nm0177018/"/>
|
125
155
|
|
126
156
|
Further reading:
|
127
157
|
|
128
158
|
* [Open Graph protocol](http://developers.facebook.com/docs/opengraph/)
|
129
159
|
|
160
|
+
### Twitter Cards
|
161
|
+
|
162
|
+
Twitter cards make it possible for you to attach media experiences to Tweets that link to your content.
|
163
|
+
There are 3 card types (summary, photo and player). Here's an example for summary:
|
164
|
+
|
165
|
+
set_meta_tags :twitter => {
|
166
|
+
:card => "summary",
|
167
|
+
:site => "@username"
|
168
|
+
}
|
169
|
+
# <meta property="twitter:card" content="summary"/>
|
170
|
+
# <meta property="twitter:site" content="@username"/>
|
171
|
+
|
172
|
+
Take in consideration that if you're already using OpenGraph to describe data on your page, it’s easy to generate a Twitter card without duplicating your tags and data. When the Twitter card processor looks for tags on your page, it first checks for the Twitter property, and if not present, falls back to the supported Open Graph property. This allows for both to be defined on the page independently, and minimizes the amount of duplicate markup required to describe your content and experience.
|
173
|
+
|
174
|
+
Further reading:
|
175
|
+
|
176
|
+
* [Twitter Cards Documentation](https://dev.twitter.com/docs/cards/)
|
177
|
+
|
178
|
+
|
130
179
|
## MetaTags Usage
|
131
180
|
|
132
181
|
First, add this code to your main layout:
|
@@ -205,7 +254,8 @@ Use these options to customize the title format:
|
|
205
254
|
* `:noindex` — add noindex meta tag; when true, 'robots' will be used, otherwise the string will be used;
|
206
255
|
* `:nofollow` — add nofollow meta tag; when true, 'robots' will be used, otherwise the string will be used;
|
207
256
|
* `:canonical` — add canonical link tag;
|
208
|
-
* `:
|
257
|
+
* `:og` — add Open Graph tags (Hash).
|
258
|
+
* `:twitter` — add Twitter tags (Hash).
|
209
259
|
|
210
260
|
And here are a few examples to give you ideas.
|
211
261
|
|
@@ -213,7 +263,7 @@ And here are a few examples to give you ideas.
|
|
213
263
|
<%= display_meta_tags :prefix => false, :separator => ":" %>
|
214
264
|
<%= display_meta_tags :lowercase => true %>
|
215
265
|
<%= display_meta_tags :reverse => true, :prefix => false %>
|
216
|
-
<%= display_meta_tags :
|
266
|
+
<%= display_meta_tags :og => { :title => 'The Rock', :type => 'video.movie' } %>
|
217
267
|
|
218
268
|
### Allowed values
|
219
269
|
|
@@ -281,3 +331,4 @@ There are several plugins influenced me to create this one:
|
|
281
331
|
* Kristoffer Renholm (contributor)
|
282
332
|
* [Jürg Lehni](https://github.com/lehni) (contributor)
|
283
333
|
* [Tom Coleman](https://github.com/tmeasday) (contributor)
|
334
|
+
* [Guille Lopez](https://github.com/guillelopez) (contributor)
|
@@ -32,15 +32,15 @@ module MetaTags
|
|
32
32
|
#
|
33
33
|
# See <tt>MetaTags.set_meta_tags</tt> for details.
|
34
34
|
def set_meta_tags(meta_tags)
|
35
|
-
meta_tags
|
36
|
-
meta_tags[:
|
37
|
-
self.meta_tags.deep_merge!(meta_tags
|
35
|
+
meta_tags = (meta_tags || {}).with_indifferent_access
|
36
|
+
meta_tags[:og] = meta_tags.delete(:open_graph) if meta_tags.key?(:open_graph)
|
37
|
+
self.meta_tags.deep_merge!(meta_tags)
|
38
38
|
end
|
39
39
|
protected :set_meta_tags
|
40
40
|
|
41
41
|
# Get meta tags for the page.
|
42
42
|
def meta_tags
|
43
|
-
@meta_tags ||=
|
43
|
+
@meta_tags ||= HashWithIndifferentAccess.new
|
44
44
|
end
|
45
45
|
protected :meta_tags
|
46
46
|
end
|
data/lib/meta_tags/version.rb
CHANGED
@@ -4,7 +4,7 @@ module MetaTags
|
|
4
4
|
module ViewHelper
|
5
5
|
# Get meta tags for the page.
|
6
6
|
def meta_tags
|
7
|
-
@meta_tags ||=
|
7
|
+
@meta_tags ||= HashWithIndifferentAccess.new
|
8
8
|
end
|
9
9
|
|
10
10
|
# Set meta tags for the page.
|
@@ -157,7 +157,8 @@ module MetaTags
|
|
157
157
|
result = []
|
158
158
|
|
159
159
|
# title
|
160
|
-
|
160
|
+
title = build_full_title(meta_tags)
|
161
|
+
result << content_tag(:title, title) unless title.blank?
|
161
162
|
|
162
163
|
# description
|
163
164
|
description = normalize_description(meta_tags[:description])
|
@@ -175,13 +176,15 @@ module MetaTags
|
|
175
176
|
content = [meta_tags[:noindex] && 'noindex', meta_tags[:nofollow] && 'nofollow'].compact.join(', ')
|
176
177
|
result << tag(:meta, :name => noindex_name, :content => content) unless content.blank?
|
177
178
|
else
|
178
|
-
result << tag(:meta, :name => noindex_name, :content => 'noindex') if meta_tags[:noindex]
|
179
|
-
result << tag(:meta, :name => nofollow_name, :content => 'nofollow') if meta_tags[:nofollow]
|
179
|
+
result << tag(:meta, :name => noindex_name, :content => 'noindex') if meta_tags[:noindex] && meta_tags[:noindex] != false
|
180
|
+
result << tag(:meta, :name => nofollow_name, :content => 'nofollow') if meta_tags[:nofollow] && meta_tags[:nofollow] != false
|
180
181
|
end
|
181
182
|
|
182
|
-
#
|
183
|
-
|
184
|
-
|
183
|
+
# hashes
|
184
|
+
meta_tags.each do |property, content|
|
185
|
+
if content.is_a?(Hash)
|
186
|
+
result.concat process_tree(property, content)
|
187
|
+
end
|
185
188
|
end
|
186
189
|
|
187
190
|
# canonical
|
@@ -191,6 +194,21 @@ module MetaTags
|
|
191
194
|
result.respond_to?(:html_safe) ? result.html_safe : result
|
192
195
|
end
|
193
196
|
|
197
|
+
# Recursive function to process all the hashes and arrays on meta tags
|
198
|
+
def process_tree(property, content)
|
199
|
+
result = []
|
200
|
+
if content.is_a?(Hash)
|
201
|
+
content.each do |key, value|
|
202
|
+
result.concat process_tree("#{property}:#{key}", value)
|
203
|
+
end
|
204
|
+
else
|
205
|
+
Array(content).each do |c|
|
206
|
+
result << tag(:meta, :property => "#{property}", :content => c) unless c.blank?
|
207
|
+
end
|
208
|
+
end
|
209
|
+
result
|
210
|
+
end
|
211
|
+
|
194
212
|
# Returns full page title as a string without surrounding <title> tag.
|
195
213
|
#
|
196
214
|
# The only case when you may need this helper is when you use pjax. This means
|
@@ -237,8 +255,8 @@ module MetaTags
|
|
237
255
|
end
|
238
256
|
|
239
257
|
def normalize_open_graph(meta_tags)
|
240
|
-
meta_tags
|
241
|
-
meta_tags[:
|
258
|
+
meta_tags = (meta_tags || {}).with_indifferent_access
|
259
|
+
meta_tags[:og] = meta_tags.delete(:open_graph) if meta_tags.key?(:open_graph)
|
242
260
|
meta_tags
|
243
261
|
end
|
244
262
|
|
@@ -34,7 +34,7 @@ describe MetaTags::ControllerHelper do
|
|
34
34
|
it 'should set meta tags from instance variables' do
|
35
35
|
subject.index
|
36
36
|
subject.rendered.should be_true
|
37
|
-
subject.meta_tags.should == {
|
37
|
+
subject.meta_tags.should == { 'title' => 'title', 'keywords' => 'key1, key2, key3', 'description' => 'description' }
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
data/spec/meta_tags_spec.rb
CHANGED
@@ -73,6 +73,12 @@ describe MetaTags::ViewHelper do
|
|
73
73
|
end
|
74
74
|
|
75
75
|
context 'displaying title' do
|
76
|
+
it 'should not display title if blank' do
|
77
|
+
subject.display_meta_tags.should == ''
|
78
|
+
subject.title('')
|
79
|
+
subject.display_meta_tags.should == ''
|
80
|
+
end
|
81
|
+
|
76
82
|
it 'should use website name if title is empty' do
|
77
83
|
subject.display_meta_tags(:site => 'someSite').should == '<title>someSite</title>'
|
78
84
|
end
|
@@ -164,6 +170,11 @@ describe MetaTags::ViewHelper do
|
|
164
170
|
end
|
165
171
|
|
166
172
|
context 'displaying description' do
|
173
|
+
it 'should not display description if blank' do
|
174
|
+
subject.description('')
|
175
|
+
subject.display_meta_tags.should == ''
|
176
|
+
end
|
177
|
+
|
167
178
|
it 'should display description when "description" used' do
|
168
179
|
subject.description('someDescription')
|
169
180
|
subject.display_meta_tags(:site => 'someSite').should include('<meta content="someDescription" name="description" />')
|
@@ -197,6 +208,14 @@ describe MetaTags::ViewHelper do
|
|
197
208
|
end
|
198
209
|
|
199
210
|
context 'displaying keywords' do
|
211
|
+
it 'should not display keywords if blank' do
|
212
|
+
subject.keywords('')
|
213
|
+
subject.display_meta_tags.should == ''
|
214
|
+
|
215
|
+
subject.keywords([])
|
216
|
+
subject.display_meta_tags.should == ''
|
217
|
+
end
|
218
|
+
|
200
219
|
it 'should display keywords when "keywords" used' do
|
201
220
|
subject.keywords('some-keywords')
|
202
221
|
subject.display_meta_tags(:site => 'someSite').should include('<meta content="some-keywords" name="keywords" />')
|
@@ -248,6 +267,12 @@ describe MetaTags::ViewHelper do
|
|
248
267
|
it 'should display nothing by default' do
|
249
268
|
subject.display_meta_tags(:site => 'someSite').should_not include('<meta content="noindex"')
|
250
269
|
end
|
270
|
+
|
271
|
+
it "should display nothing if given false" do
|
272
|
+
subject.set_meta_tags(:noindex => false)
|
273
|
+
subject.display_meta_tags(:site => 'someSite').should_not include('<meta content="robots"')
|
274
|
+
subject.display_meta_tags(:site => 'someSite').should_not include('<meta content="noindex"')
|
275
|
+
end
|
251
276
|
end
|
252
277
|
|
253
278
|
context 'displaying nofollow' do
|
@@ -337,6 +362,20 @@ describe MetaTags::ViewHelper do
|
|
337
362
|
end
|
338
363
|
end
|
339
364
|
|
365
|
+
it 'should display meta tags with hashes and arrays' do
|
366
|
+
subject.set_meta_tags(:foo => {
|
367
|
+
:bar => "lorem",
|
368
|
+
:baz => {
|
369
|
+
:qux => ["lorem", "ipsum"]
|
370
|
+
}
|
371
|
+
})
|
372
|
+
subject.display_meta_tags(:site => 'someSite').tap do |content|
|
373
|
+
content.should include('<meta content="lorem" property="foo:bar" />')
|
374
|
+
content.should include('<meta content="lorem" property="foo:baz:qux" />')
|
375
|
+
content.should include('<meta content="ipsum" property="foo:baz:qux" />')
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
340
379
|
it 'should use deep merge when displaying open graph meta tags' do
|
341
380
|
subject.set_meta_tags(:og => { :title => 'Facebook Share Title' })
|
342
381
|
subject.display_meta_tags(:og => { :description => 'Facebook Share Description' }).tap do |content|
|
@@ -344,6 +383,34 @@ describe MetaTags::ViewHelper do
|
|
344
383
|
content.should include('<meta content="Facebook Share Description" property="og:description" />')
|
345
384
|
end
|
346
385
|
end
|
386
|
+
|
387
|
+
it "should not display meta tags without content" do
|
388
|
+
subject.set_meta_tags(:open_graph => {
|
389
|
+
:title => '',
|
390
|
+
:description => ''
|
391
|
+
})
|
392
|
+
subject.display_meta_tags(:site => 'someSite').tap do |content|
|
393
|
+
content.should_not include('<meta content="" property="og:title" />')
|
394
|
+
content.should_not include('<meta content="" property="og:description" />')
|
395
|
+
end
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
context 'while handling string meta tag names' do
|
400
|
+
it 'should work with common parameters' do
|
401
|
+
subject.display_meta_tags('site' => 'someSite', 'title' => 'someTitle').should == '<title>someSite | someTitle</title>'
|
402
|
+
end
|
403
|
+
|
404
|
+
it 'should work with open graph parameters' do
|
405
|
+
subject.set_meta_tags('og' => {
|
406
|
+
'title' => 'facebook title',
|
407
|
+
'description' => 'facebook description'
|
408
|
+
})
|
409
|
+
subject.display_meta_tags(:site => 'someSite').tap do |content|
|
410
|
+
content.should include('<meta content="facebook title" property="og:title" />')
|
411
|
+
content.should include('<meta content="facebook description" property="og:description" />')
|
412
|
+
end
|
413
|
+
end
|
347
414
|
end
|
348
415
|
|
349
416
|
context '.display_title' do
|
data/spec/spec_helper.rb
CHANGED
@@ -11,15 +11,18 @@ shared_examples_for '.set_meta_tags' do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'should use deep merge when updating meta tags' do
|
14
|
-
subject.set_meta_tags(:
|
15
|
-
subject.meta_tags[:
|
14
|
+
subject.set_meta_tags(:og => { :title => 'hello' })
|
15
|
+
subject.meta_tags[:og].should == { 'title' => 'hello' }
|
16
|
+
|
17
|
+
subject.set_meta_tags(:og => { :description => 'world' })
|
18
|
+
subject.meta_tags[:og].should == { 'title' => 'hello', 'description' => 'world' }
|
16
19
|
|
17
|
-
subject.set_meta_tags(:
|
18
|
-
subject.meta_tags[:
|
20
|
+
subject.set_meta_tags(:og => { :admin => { :id => 1 } } )
|
21
|
+
subject.meta_tags[:og].should == { 'title' => 'hello', 'description' => 'world', 'admin' => { 'id' => 1 } }
|
19
22
|
end
|
20
23
|
|
21
|
-
it 'should normalize :
|
22
|
-
subject.set_meta_tags(:
|
23
|
-
subject.meta_tags[:
|
24
|
+
it 'should normalize :open_graph to :og' do
|
25
|
+
subject.set_meta_tags(:open_graph => { :title => 'hello' })
|
26
|
+
subject.meta_tags[:og].should == { 'title' => 'hello' }
|
24
27
|
end
|
25
28
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: meta-tags
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 1.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 1.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dmytro Shteflyuk
|
@@ -15,7 +15,8 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2013-02-13 00:00:00 -05:00
|
19
|
+
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: actionpack
|
@@ -113,6 +114,7 @@ files:
|
|
113
114
|
- spec/controller_helper_spec.rb
|
114
115
|
- spec/meta_tags_spec.rb
|
115
116
|
- spec/spec_helper.rb
|
117
|
+
has_rdoc: true
|
116
118
|
homepage: http://github.com/kpumuk/meta-tags
|
117
119
|
licenses: []
|
118
120
|
|
@@ -142,10 +144,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
144
|
requirements: []
|
143
145
|
|
144
146
|
rubyforge_project:
|
145
|
-
rubygems_version: 1.
|
147
|
+
rubygems_version: 1.5.2
|
146
148
|
signing_key:
|
147
149
|
specification_version: 3
|
148
150
|
summary: Collection of SEO helpers for Ruby on Rails.
|
149
151
|
test_files: []
|
150
152
|
|
151
|
-
has_rdoc:
|