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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c4ae0f611d5f7dec90907c68faf99e703cc5aa7
4
- data.tar.gz: 0fb30052abf1a39809c3a07a83efd461a25cba05
3
+ metadata.gz: bdbc421bf342a3f37d8b951f46c9af8b84afcb21
4
+ data.tar.gz: d05a56ec576418e073965cfefc233cb9fef5fe31
5
5
  SHA512:
6
- metadata.gz: be3af05172de70bb573bca5615e28406075594b88233639ee022761632d6e758e03e69736aa8375b16377bcea5141eccdd7b768e3944fa331302779363d8c9b9
7
- data.tar.gz: c3318cd1a5a4c700ea5015a47e33776a423ebbea4ddfb747f3184e2c13f3dc666bb432976ade915a66f5d295d0ddbd26cab5183325f3e3fa2857d836bbc4d427
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:
@@ -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
@@ -3,7 +3,7 @@ module Metamagic
3
3
  attr_reader :context, :key, :value
4
4
 
5
5
  def initialize(context, key, value)
6
- @context, @key, @value = context, key, value
6
+ @context, @key, @value = context, key.to_s, value
7
7
  @key = @key.gsub /^[^:]+:/, "" if remove_prefix?
8
8
  end
9
9
 
@@ -1,7 +1,22 @@
1
1
  module Metamagic
2
2
  class TitleTag < Tag
3
3
  def to_html
4
- content_tag(:title, value) if value
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
@@ -1,3 +1,3 @@
1
1
  module Metamagic
2
- VERSION = "3.0.3"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -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
@@ -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.3
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-06-27 00:00:00.000000000 Z
11
+ date: 2014-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails