metamagic 3.0.3 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +39 -0
- data/lib/metamagic/renderer.rb +12 -0
- data/lib/metamagic/tag.rb +1 -1
- data/lib/metamagic/tags/title_tag.rb +16 -1
- data/lib/metamagic/version.rb +1 -1
- data/lib/metamagic/view_helper.rb +6 -0
- data/test/title_tag_test.rb +27 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bdbc421bf342a3f37d8b951f46c9af8b84afcb21
|
4
|
+
data.tar.gz: d05a56ec576418e073965cfefc233cb9fef5fe31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aad55c49bd9634bc878e0c7491f9d4a222f73daf06a176d5b60537f0b784bfb93f1871467723651d76d9988ebb57f176535011795d8f33d2182728d307daf8c2
|
7
|
+
data.tar.gz: d600bc8dbe537dbe48fe945f095c0508b30076f042206375af09c5e0d43260680918445dfe698163777f50648128f235e248bb264cae74e36c26b4095ec80e24
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## Version 3.1.0
|
4
|
+
|
5
|
+
* Adds title templates.
|
6
|
+
|
7
|
+
## Version 3.0.3
|
8
|
+
|
9
|
+
* Minor fix.
|
10
|
+
|
11
|
+
## Version 3.0.2
|
12
|
+
|
13
|
+
* Add relation links.
|
14
|
+
|
15
|
+
## Version 3.0.1
|
16
|
+
|
17
|
+
* Handle nil meta values.
|
18
|
+
|
3
19
|
## Version 3.0.0
|
4
20
|
|
5
21
|
* Rewritten to simplify Open Graph (Facebook), Twitter Cards, and custom tags.
|
data/README.md
CHANGED
@@ -56,6 +56,45 @@ This will generate the following:
|
|
56
56
|
</head>
|
57
57
|
```
|
58
58
|
|
59
|
+
### Adding a title template
|
60
|
+
|
61
|
+
Title templates can be used to automatically insert the name of your site into the meta title.
|
62
|
+
|
63
|
+
In your template:
|
64
|
+
|
65
|
+
```erb
|
66
|
+
<%
|
67
|
+
meta title: "My Title"
|
68
|
+
%>
|
69
|
+
```
|
70
|
+
|
71
|
+
In your layout:
|
72
|
+
|
73
|
+
```erb
|
74
|
+
<%
|
75
|
+
metamagic site: "My Site", title_template: ":title — :site"
|
76
|
+
%>
|
77
|
+
```
|
78
|
+
|
79
|
+
This will render the following:
|
80
|
+
|
81
|
+
```html
|
82
|
+
<head>
|
83
|
+
<title>My Title — My Site</title>
|
84
|
+
...
|
85
|
+
</head>
|
86
|
+
```
|
87
|
+
|
88
|
+
You can also use a proc to enable custom processing:
|
89
|
+
|
90
|
+
```erb
|
91
|
+
<%
|
92
|
+
metamagic site: "My Site", title_template: -> { title.include?(site) ? title : "#{title} — #{site}" }
|
93
|
+
%>
|
94
|
+
```
|
95
|
+
|
96
|
+
This will insert the site name only if it is not already present in the title.
|
97
|
+
|
59
98
|
### Shortcut helpers
|
60
99
|
|
61
100
|
For easy setting of meta tags, you can use the shortcut helpers like this:
|
data/lib/metamagic/renderer.rb
CHANGED
@@ -53,6 +53,18 @@ module Metamagic
|
|
53
53
|
self.class.tag_types.has_key?(prefix)
|
54
54
|
end
|
55
55
|
|
56
|
+
def title_template
|
57
|
+
@title_template ||= ":title"
|
58
|
+
end
|
59
|
+
|
60
|
+
attr_writer :title_template
|
61
|
+
|
62
|
+
def site
|
63
|
+
@site or raise "Metamagic site not set. Please use `metamagic site: 'My Site'` to set it."
|
64
|
+
end
|
65
|
+
|
66
|
+
attr_writer :site
|
67
|
+
|
56
68
|
def render
|
57
69
|
tags.sort.map(&:to_html).compact.join("\n").html_safe
|
58
70
|
end
|
data/lib/metamagic/tag.rb
CHANGED
@@ -1,7 +1,22 @@
|
|
1
1
|
module Metamagic
|
2
2
|
class TitleTag < Tag
|
3
3
|
def to_html
|
4
|
-
|
4
|
+
interpolated_title = if title_template.is_a?(Proc)
|
5
|
+
instance_exec(&title_template)
|
6
|
+
else
|
7
|
+
title_template.gsub(/:\w+/) do |key|
|
8
|
+
if key == ":title"
|
9
|
+
value
|
10
|
+
else
|
11
|
+
send(key[1..-1])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
content_tag(:title, interpolated_title) if interpolated_title.present?
|
16
|
+
end
|
17
|
+
|
18
|
+
def title
|
19
|
+
value
|
5
20
|
end
|
6
21
|
|
7
22
|
def sort_order
|
data/lib/metamagic/version.rb
CHANGED
@@ -5,6 +5,12 @@ module Metamagic
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def metamagic(hash = {})
|
8
|
+
# Loop through special options
|
9
|
+
hash.slice(:title_template, :site).each do |key, value|
|
10
|
+
metamagic_renderer.send("#{key}=", value)
|
11
|
+
hash.delete key
|
12
|
+
end
|
13
|
+
|
8
14
|
metamagic_renderer.add hash
|
9
15
|
metamagic_renderer.render
|
10
16
|
end
|
data/test/title_tag_test.rb
CHANGED
@@ -24,4 +24,31 @@ class TitleTagTest < ActionView::TestCase
|
|
24
24
|
assert_equal %{<meta content="Test description" name="description" />},
|
25
25
|
metamagic
|
26
26
|
end
|
27
|
+
|
28
|
+
test "title template" do
|
29
|
+
title "Test Title"
|
30
|
+
|
31
|
+
assert_equal %{<title>Test Title — My Site</title>},
|
32
|
+
metamagic(site: "My Site", title_template: ":title — :site")
|
33
|
+
end
|
34
|
+
|
35
|
+
test "title template with default title" do
|
36
|
+
assert_equal %{<title>Test Title — My Site</title>},
|
37
|
+
metamagic(site: "My Site", title: "Test Title", title_template: ":title — :site")
|
38
|
+
end
|
39
|
+
|
40
|
+
test "title template with nil site" do
|
41
|
+
title "Test Title"
|
42
|
+
|
43
|
+
assert_raises RuntimeError do
|
44
|
+
metamagic(title_template: ":title — :site")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
test "title template proc" do
|
49
|
+
title "Test Title"
|
50
|
+
|
51
|
+
assert_equal %{<title>Site: My Site — Title: Test Title</title>},
|
52
|
+
metamagic(site: "My Site", title_template: -> { "Site: #{site} — Title: #{title}" })
|
53
|
+
end
|
27
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metamagic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lasse Bunk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|