metamagic 2.0.6 → 3.0.0.beta1

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: 04b70fb6125a815b71e21070fa5f2d6f194c5d9e
4
- data.tar.gz: 10fd03aa3c6d4fa93674a043bf2427fb3a387621
3
+ metadata.gz: db6ed555cf9378801321e62f86e74a5bd55924a6
4
+ data.tar.gz: 5f77f984c6dc4edbc9ccc9a3f846caeaab8f1f08
5
5
  SHA512:
6
- metadata.gz: b05aa42d50cf9f6aeb943a488d5e91dbaf61335db2f548301919e91757512316df4c4e86c232bac9bba2a597342fc247c3d7b822dec45c7d7f45af65a54ce347
7
- data.tar.gz: d047c2431ecd66370bfcb1661e2ddbfc1f73ac1593ed42512460257932ac6461c6975ea5306a49c80bad97918c9a621e618b60df52ae1b97161dc7e02cff0487
6
+ metadata.gz: dcd1a96e0f49eed0ea2809cd593a5ffe59228dd71c24cdf5f967a0d7770acf5a14a3dc1faaf42a46c5824ceff5656128766fa49c2475c51679845b84846751d7
7
+ data.tar.gz: 85928f766fca9e6e661130a83045036d9958d892e52d4993017e845e83f95e0d351eb8e835f68cf3af74e360770dfc14fb1e2f30324fbf088bdaa264344d56fc
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+
3
+ ## Version 3.0.0
4
+
5
+ * Rewritten to simplify Open Graph (Facebook), Twitter Cards, and custom tags.
6
+ Some features are incompatible with 2.x but are easy to rewrite.
data/README.md CHANGED
@@ -4,6 +4,9 @@ Metamagic
4
4
  =========
5
5
 
6
6
  Metamagic is a simple [Ruby on Rails](http://rubyonrails.org) plugin for creating meta tags.
7
+ It supports regular meta tags, [OpenGraph](http://ogp.me/) (Facebook), [Twitter Cards](https://dev.twitter.com/docs/cards/types/summary-card), and custom tags.
8
+
9
+ See the [changelog](https://github.com/lassebunk/metamagic/blob/master/CHANGELOG.md) for changes in version 3.0.
7
10
 
8
11
  Installation
9
12
  ------------
@@ -11,7 +14,7 @@ Installation
11
14
  In your *Gemfile*:
12
15
 
13
16
  ```ruby
14
- gem 'metamagic'
17
+ gem 'metamagic', '3.0.0.beta1'
15
18
  ```
16
19
 
17
20
  Then run `bundle install`.
@@ -34,9 +37,9 @@ Then, at the top of your view, e.g. *app/views/posts/show.html.erb*:
34
37
 
35
38
  ```erb
36
39
  <%
37
- meta :title => "My title",
38
- :description => "My description",
39
- :keywords => %w(keyword1 keyword2 keyword3)
40
+ meta title: "My Title",
41
+ description: "My description",
42
+ keywords: %w(keyword1 keyword2 keyword3)
40
43
  %>
41
44
  ```
42
45
 
@@ -44,7 +47,7 @@ This will generate the following:
44
47
 
45
48
  ```html
46
49
  <head>
47
- <title>My title</title>
50
+ <title>My Title</title>
48
51
  <meta content="My description" name="description" />
49
52
  <meta content="keyword1, keyword2, keyword3" name="keywords" />
50
53
  ...
@@ -57,7 +60,7 @@ It's possible to specify default values to be shown if a view doesn't specify it
57
60
 
58
61
  ```erb
59
62
  <head>
60
- <%= metamagic :title => "My default title", :description => "My default description.", :keywords => %w(keyword1 keyword2 keyword3) %>
63
+ <%= metamagic title: "My default title", description: "My default description.", keywords: %w(keyword1 keyword2 keyword3) %>
61
64
  ...
62
65
  </head>
63
66
  ```
@@ -66,11 +69,11 @@ These values are then inserted if a view doesn't set others.
66
69
 
67
70
  ### Custom meta tags
68
71
 
69
- For custom meta tags, just call it like this in the top of your view:
72
+ For custom meta tags, you can use it like this:
70
73
 
71
74
  ```erb
72
75
  <%
73
- meta :my_custom_tag => "My custom value"
76
+ meta my_custom_name: "My custom value"
74
77
  %>
75
78
  ```
76
79
 
@@ -79,18 +82,20 @@ This will generate the following:
79
82
  ```html
80
83
  <head>
81
84
  ...
82
- <meta content="My custom value" name="my_custom_tag" />
85
+ <meta content="My custom value" name="my_custom_name" />
83
86
  ...
84
87
  </head>
85
88
  ```
86
89
 
87
- ### Custom properties (like Open Graph)
90
+ ### Custom properties
88
91
 
89
- With custom properties:
92
+ #### OpenGraph (Facebook)
90
93
 
91
94
  ```erb
92
95
  <%
93
- meta [:property => "og:image", :content => "http://mydomain.com/images/my_image.jpg"]
96
+ meta og: {
97
+ image: "http://mydomain.com/images/my_image.jpg"
98
+ }
94
99
  %>
95
100
  ```
96
101
 
@@ -104,6 +109,58 @@ This will generate the following:
104
109
  </head>
105
110
  ```
106
111
 
112
+ #### Twitter Cards
113
+
114
+ ```erb
115
+ <%
116
+ meta twitter: {
117
+ card: "summary",
118
+ site: "@flickr"
119
+ }
120
+ %>
121
+ ```
122
+
123
+ This will generate the following:
124
+
125
+ ```html
126
+ <head>
127
+ ...
128
+ <meta content="summary" property="twitter:card" />
129
+ <meta content="@flickr" property="twitter:site" />
130
+ ...
131
+ </head>
132
+ ```
133
+
134
+ ### Custom tags
135
+
136
+ You can add custom rendering for tag prefixes you specify.
137
+
138
+ In *config/initializers/metamagic.rb*:
139
+
140
+ ```ruby
141
+ Metamagic::Renderer.register_tag_type :custom, ->(key, value) { tag(:custom_tag, first: key, second: value) }
142
+ ```
143
+
144
+ In your view:
145
+
146
+ ```erb
147
+ <%
148
+ meta title: "My Title",
149
+ custom: {
150
+ key_one: "My first key",
151
+ key_two: "My second key"
152
+ }
153
+ %>
154
+ ```
155
+
156
+ This will render the following:
157
+
158
+ ```html
159
+ <title>My Title</title>
160
+ <custom_tag first="custom:key_one" second="My first key" />
161
+ <custom_tag first="custom:key_two" second="My second key" />
162
+ ```
163
+
107
164
  Requirements
108
165
  ------------
109
166
 
@@ -120,14 +177,14 @@ Contributing
120
177
 
121
178
  1. Fork the project
122
179
  2. Create your feature branch (`git checkout -b my-new-feature`)
123
- 3. Commit your changes (`git commit -am 'Add new feature'`)
124
- 4. Push to the branch (`git push origin my-new-feature`)
125
- 5. Create new pull r
126
- equest
180
+ 3. Make your changes and make sure the tests pass (run `rake`)
181
+ 4. Commit your changes (`git commit -am 'Add new feature'`)
182
+ 5. Push to the branch (`git push origin my-new-feature`)
183
+ 6. Create new pull request
127
184
 
128
185
  Contributors
129
186
  ------------
130
187
 
131
188
  * [See the list of contributors](https://github.com/lassebunk/metamagic/graphs/contributors)
132
189
 
133
- Copyright (c) 2010-2013 [Lasse Bunk](http://lassebunk.dk), released under the MIT license
190
+ Copyright (c) 2010-2014 [Lasse Bunk](http://lassebunk.dk), released under the MIT license
data/lib/metamagic.rb CHANGED
@@ -1,4 +1,12 @@
1
- require "metamagic/version"
2
- require 'metamagic/helper_methods'
1
+ %w{
2
+ version
3
+ tag
4
+ tags/meta_tag
5
+ tags/title_tag
6
+ tags/property_tag
7
+ tags/custom_tag
8
+ renderer
9
+ helper_methods
10
+ }.each { |f| require "metamagic/#{f}" }
3
11
 
4
12
  ActionView::Base.send :include, Metamagic::HelperMethods
@@ -1,58 +1,18 @@
1
1
  module Metamagic
2
2
  module HelperMethods
3
- def meta_tags
4
- @meta_tags ||= []
3
+ def meta(hash = {})
4
+ metamagic_renderer.add hash
5
5
  end
6
6
 
7
- def meta(*options)
8
- # add page specific meta tags
9
- add_meta_tags options
7
+ def metamagic(hash = {})
8
+ metamagic_renderer.add hash
9
+ metamagic_renderer.render
10
10
  end
11
11
 
12
- def add_tag_if_not_existing(new_tag)
13
- # add meta tag if there's not an existing one with the same name or property attribute
14
- return if meta_tags.find { |tag| tag[:name] && new_tag[:name] && tag[:name] == new_tag[:name] }
15
- return if meta_tags.find { |tag| tag[:property] && new_tag[:property] && tag[:property] == new_tag[:property] }
16
- meta_tags << new_tag
17
- end
18
-
19
- def add_meta_tags(options)
20
- options.each do |option|
21
- if option.is_a?(Hash)
22
- option.each_pair do |key, value|
23
- if value.is_a?(Array)
24
- add_tag_if_not_existing :name => key, :content => value.join(", ")
25
- else
26
- add_tag_if_not_existing :name => key, :content => value
27
- end
28
- end
29
- elsif option.is_a?(Array)
30
- option.each do |tag|
31
- add_tag_if_not_existing tag
32
- end
33
- else
34
- raise TypeError, "Unknown tag type #{tag.class.name}. Use either Hash or Array."
35
- end
36
- end
37
- end
38
-
39
- def metamagic(*options)
40
- # apply default meta tags if they don't exist
41
- add_meta_tags options
42
-
43
- # loop through the added tags
44
- out = []
45
- meta_tags.each do |tag|
46
- if tag[:name] == :title
47
- out << content_tag(:title, tag[:content])
48
- else
49
- # add tag
50
- out << tag(:meta, tag)
51
- end
52
- end
12
+ private
53
13
 
54
- # return tags
55
- out.join("\n").html_safe
14
+ def metamagic_renderer
15
+ @metamagic_renderer ||= Renderer.new(self)
56
16
  end
57
17
  end
58
18
  end
@@ -0,0 +1,69 @@
1
+ module Metamagic
2
+ class Renderer
3
+ DEFAULT_TAG_TYPES = {
4
+ title: TitleTag,
5
+ og: PropertyTag,
6
+ twitter: PropertyTag
7
+ }
8
+
9
+ class << self
10
+ def tag_types
11
+ @tag_types ||= DEFAULT_TAG_TYPES.dup
12
+ end
13
+
14
+ def register_tag_type(prefix, klass)
15
+ tag_types[prefix.to_sym] = klass
16
+ end
17
+
18
+ def tag_type_for_key(key)
19
+ prefix = key.split(":").first
20
+ tag_types[prefix.to_sym] || MetaTag
21
+ end
22
+ end
23
+
24
+ attr_reader :context
25
+
26
+ def initialize(context)
27
+ @context = context
28
+ end
29
+
30
+ def tags
31
+ @tags ||= []
32
+ end
33
+
34
+ def add(hash = {})
35
+ transform_hash(hash).each do |k, v|
36
+ klass = self.class.tag_type_for_key(k)
37
+ next if tags.any? { |t| t.class == klass && t.key == k }
38
+ tags << if klass.is_a?(Proc)
39
+ CustomTag.new(self, k, v, klass)
40
+ else
41
+ klass.new(self, k, v)
42
+ end
43
+ end
44
+ end
45
+
46
+ def render
47
+ tags.map(&:to_html).join("\n").html_safe
48
+ end
49
+
50
+ def method_missing(*args)
51
+ context.send(*args)
52
+ end
53
+
54
+ private
55
+
56
+ # Transforms a nested hash into meta property keys.
57
+ def transform_hash(hash, path = "")
58
+ hash.each_with_object({}) do |(k, v), ret|
59
+ key = path + k.to_s
60
+
61
+ if v.is_a?(Hash)
62
+ ret.merge! transform_hash(v, "#{key}:")
63
+ else
64
+ ret[key] = v
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,17 @@
1
+ module Metamagic
2
+ class Tag
3
+ attr_reader :context, :key, :value
4
+
5
+ def initialize(context, key, value)
6
+ @context, @key, @value = context, key, value
7
+ end
8
+
9
+ def to_html
10
+ raise "#{self.class.name}#to_html must be overridden to render tag"
11
+ end
12
+
13
+ def method_missing(*args)
14
+ context.send(*args)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module Metamagic
2
+ class CustomTag < Tag
3
+ attr_reader :render_proc
4
+
5
+ def initialize(context, key, value, render_proc)
6
+ super(context, key, value)
7
+ @render_proc = render_proc
8
+ end
9
+
10
+ def to_html
11
+ instance_exec key, value, &render_proc
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Metamagic
2
+ class MetaTag < Tag
3
+ def to_html
4
+ tag(:meta, name: key, content: Array(value).join(", "))
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Metamagic
2
+ class PropertyTag < Tag
3
+ def to_html
4
+ Array(value).map { |value| tag(:meta, property: key, content: value) }.join("\n").html_safe
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Metamagic
2
+ class TitleTag < Tag
3
+ def to_html
4
+ content_tag(:title, value)
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Metamagic
2
- VERSION = "2.0.6"
2
+ VERSION = "3.0.0.beta1"
3
3
  end
@@ -31,19 +31,40 @@ class HelperMethodsTest < ActionView::TestCase
31
31
  metamagic
32
32
  end
33
33
 
34
- test "meta tags using property attribute rather than name" do
35
- meta [property: "og:url", content: "http://test.url"]
34
+ test "open graph" do
35
+ meta title: "Test Title",
36
+ og: {
37
+ image: {
38
+ url: "http://test.com/image.jpg",
39
+ type: "image/png"
40
+ }
41
+ }
36
42
 
37
- assert_equal %{<meta content="http://test.url" property="og:url" />},
43
+ assert_equal %{<title>Test Title</title>\n<meta content="http://test.com/image.jpg" property="og:image:url" />\n<meta content="image/png" property="og:image:type" />},
38
44
  metamagic
39
45
  end
40
46
 
41
- test "overriding default meta tags if the property attribute matches" do
42
- meta [property: "og:url", content: "http://override.url"]
47
+ test "twitter cards" do
48
+ meta title: "Test Title",
49
+ twitter: {
50
+ card: :summary,
51
+ site: "@flickr"
52
+ }
43
53
 
44
- assert_equal %{<meta content="http://override.url" property="og:url" />},
45
- metamagic([property: "og:url", content: "http://default.url"])
54
+ assert_equal %{<title>Test Title</title>\n<meta content="summary" property="twitter:card" />\n<meta content="@flickr" property="twitter:site" />},
55
+ metamagic
46
56
  end
47
57
 
58
+ test "custom tags" do
59
+ Metamagic::Renderer.register_tag_type :custom, ->(key, value) { tag(:custom_tag, one: key, two: value) }
48
60
 
61
+ meta title: "Test Title",
62
+ custom: {
63
+ first: "This is the first",
64
+ second: "This is the second"
65
+ }
66
+
67
+ assert_equal %{<title>Test Title</title>\n<custom_tag one="custom:first" two="This is the first" />\n<custom_tag one="custom:second" two="This is the second" />},
68
+ metamagic
69
+ end
49
70
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metamagic
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.6
4
+ version: 3.0.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lasse Bunk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-30 00:00:00.000000000 Z
11
+ date: 2014-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Metamagic is a simple Ruby on Rails plugin for creating meta tags.
@@ -45,14 +45,21 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - .gitignore
49
- - .travis.yml
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - CHANGELOG.md
50
51
  - Gemfile
51
52
  - LICENSE.txt
52
53
  - README.md
53
54
  - Rakefile
54
55
  - lib/metamagic.rb
55
56
  - lib/metamagic/helper_methods.rb
57
+ - lib/metamagic/renderer.rb
58
+ - lib/metamagic/tag.rb
59
+ - lib/metamagic/tags/custom_tag.rb
60
+ - lib/metamagic/tags/meta_tag.rb
61
+ - lib/metamagic/tags/property_tag.rb
62
+ - lib/metamagic/tags/title_tag.rb
56
63
  - lib/metamagic/version.rb
57
64
  - metamagic.gemspec
58
65
  - test/dummy/README.rdoc
@@ -94,7 +101,6 @@ files:
94
101
  - test/dummy/public/422.html
95
102
  - test/dummy/public/500.html
96
103
  - test/dummy/public/favicon.ico
97
- - test/helper_methods_test.rb
98
104
  - test/metamagic_test.rb
99
105
  - test/test_helper.rb
100
106
  homepage: http://github.com/lassebunk/metamagic
@@ -107,17 +113,17 @@ require_paths:
107
113
  - lib
108
114
  required_ruby_version: !ruby/object:Gem::Requirement
109
115
  requirements:
110
- - - '>='
116
+ - - ">="
111
117
  - !ruby/object:Gem::Version
112
118
  version: '0'
113
119
  required_rubygems_version: !ruby/object:Gem::Requirement
114
120
  requirements:
115
- - - '>='
121
+ - - ">"
116
122
  - !ruby/object:Gem::Version
117
- version: '0'
123
+ version: 1.3.1
118
124
  requirements: []
119
125
  rubyforge_project:
120
- rubygems_version: 2.1.10
126
+ rubygems_version: 2.2.1
121
127
  signing_key:
122
128
  specification_version: 4
123
129
  summary: Simple Ruby on Rails plugin for creating meta tags.
@@ -161,6 +167,5 @@ test_files:
161
167
  - test/dummy/public/422.html
162
168
  - test/dummy/public/500.html
163
169
  - test/dummy/public/favicon.ico
164
- - test/helper_methods_test.rb
165
170
  - test/metamagic_test.rb
166
171
  - test/test_helper.rb
File without changes