meta-tags-helpers 0.1.4 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # meta-tags-helpers
1
+ # Rails meta tags helpers
2
2
 
3
- ## Rails meta tags helpers
3
+ ## Seo and future-proof meta tags for Rails
4
4
 
5
- Seo and future-proof meta-tags for Rails, with good customizable defaults that also provide support for open-graph and csrf: all your meta tags in a single call.
5
+ The `meta-tags-helpers` gem consists of a set of helpers to setup and render html meta tags in a simple way. It has good customizable defaults that also come with built-in support for open-graph and csrf: all your meta tags in a single call.
6
6
 
7
7
  ### Install
8
8
 
@@ -12,6 +12,15 @@ gem 'meta-tags-helpers'
12
12
 
13
13
  ### Examples
14
14
 
15
+ You could use the default meta tags (see below) inserting the following snippet of `erb` code in your layout.
16
+
17
+ ``` erb
18
+ <%= meta_tags %>
19
+ ```
20
+
21
+ if you don't like the defaults you can override them passing other values as a parameter to the `meta_tags` helper.
22
+
23
+
15
24
  ``` erb
16
25
  <%= meta_tags(
17
26
  :title => "MyBlog - This is a Blog",
@@ -24,11 +33,9 @@ gem 'meta-tags-helpers'
24
33
  %>
25
34
  ```
26
35
 
27
- or using defaults with setters (see below) just:
36
+ You can further override these values in your controller, views and partials using the `set_meta` method (see below).
28
37
 
29
- ``` erb
30
- <%= meta_tags %>
31
- ```
38
+ **NOTE:** You can set namespaced keys (eg. `og:type`) either as key-value pairs (eg. `:"og:type" => "..."`) or as nested hashes (eg. `:og => {:type => "..."}`), both of the syntaxes would address the same meta tag.
32
39
 
33
40
  ### What it generates?
34
41
 
@@ -52,9 +59,31 @@ The first example above will produce the following html:
52
59
 
53
60
  **NOTE:** namespaced meta (eg. `og:title`) are supposed to be RDF properties and so they are marked using a `property` attribute.
54
61
 
55
- ### Setting meta tags from controller/partials/other views
62
+ ### Setting and overriding meta tags from controller/partials/other views
63
+
64
+ You can set any meta tag from controller, partials or views via the `set_meta` method:
56
65
 
57
- You can customize some of defautls (see below) through handy helpers within controllers or views:
66
+ ``` rhtml
67
+ <!-- application.html.erb: the global settings -->
68
+ <%= meta_tags :og => { :type => "website" } %>
69
+ ```
70
+
71
+ Override it in a controller:
72
+
73
+ ``` rb
74
+ def show
75
+ set_meta "og:type" => "article"
76
+ end
77
+ ```
78
+ In a view:
79
+
80
+ ``` rhtml
81
+ <% set_meta "og:type" => "article" %>
82
+ ```
83
+
84
+ **NOTE:** `set_meta(:og => { :type => "article" })` is exactly the same of `set_meta(:"og:type" => "article")` so they will both override the current/default `og:type` meta tag.
85
+
86
+ You can also customize and read some of defautls (see below) through handy helpers within controllers or views:
58
87
 
59
88
  ``` rb
60
89
  meta_title(value = nil)
@@ -69,24 +98,23 @@ meta_type(value = nil)
69
98
  This is the default options hash:
70
99
 
71
100
  ``` rb
72
- default = {
73
- :charset => "utf-8",
101
+ default = {
102
+ :charset => "utf-8",
74
103
  :"X-UA-Compatible" => "IE=edge,chrome=1",
75
- :viewport => "width=device-width",
76
- :title => meta_title,
77
- :description => meta_description,
78
- :og => {
79
- :url => "#{request.url}",
80
- :type => meta_type || "article",
81
- :title => opts[:title] || meta_title,
82
- :description => opts[:description] || meta_description,
83
- :image => (opts[:og] && opts[:og][:image]) || meta_image
84
- },
85
- :"csrf-param" => request_forgery_protection_token,
86
- :"csrf-token" => form_authenticity_token
104
+ :viewport => "width=device-width",
105
+ :"og:url" => "#{request.url}",
106
+ :"og:type" => "article",
107
+ :"og:title" => opts[:title],
108
+ :"og:description" => opts[:description],
109
+ :"og:image" => opts[:"og:image"],
110
+ :"csrf-param" => request_forgery_protection_token,
111
+ :"csrf-token" => form_authenticity_token
87
112
  }
113
+
88
114
  ```
89
115
 
116
+ **NOTE:** You should supply at least the `title` and the `description`, using `set_meta` or directly as `meta_tags` argument.
117
+
90
118
  ---
91
119
 
92
120
  Copyright (c) 2012 mcasimir
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.7
@@ -22,44 +22,26 @@
22
22
 
23
23
 
24
24
  module MetaTagsHelpers
25
-
26
25
  module ActionViewExtension
27
26
 
28
- def meta_tags(*args)
29
- opts = args.extract_options!
30
- default = {
31
- :charset => "utf-8",
27
+ def meta_tags(opts = {})
28
+
29
+ opts = normalize_meta_hash(opts)
30
+
31
+ default = {
32
+ :charset => "utf-8",
32
33
  :"X-UA-Compatible" => "IE=edge,chrome=1",
33
- :viewport => "width=device-width",
34
- :title => meta_title,
35
- :description => meta_description,
36
- :og => {
37
- :url => "#{request.url}",
38
- :type => meta_type || "article",
39
- :title => opts[:title] || meta_title,
40
- :description => opts[:description] || meta_description,
41
- :image => (opts[:og] && opts[:og][:image]) || meta_image
42
- },
43
- :"csrf-param" => request_forgery_protection_token,
44
- :"csrf-token" => form_authenticity_token
45
- }
46
-
47
- meta_hash = default.deep_merge(opts)
48
-
49
- # separates namespaced keys
50
- namespaces = meta_hash.select { |k,v| v.is_a?(Hash) }
51
-
52
- # delete nil/false/namespaced keys
53
- meta_hash.delete_if { |k,v| v.blank? || v == false || v.is_a?(Hash)}
54
-
55
- namespaces.each { |ns, namespaced|
56
- namespaced.delete_if { |k,v|
57
- v.blank? || v == false || v.is_a?(Hash)
58
- }
59
- namespaced.each {|k,v|
60
- meta_hash[:"#{ns}:#{k}"] = v
61
- }
34
+ :viewport => "width=device-width",
35
+ :"og:url" => "#{request.url}",
36
+ :"og:type" => "article",
37
+ :"og:title" => opts[:title],
38
+ :"og:description" => opts[:description],
39
+ :"og:image" => opts[:"og:image"],
40
+ :"csrf-param" => request_forgery_protection_token,
41
+ :"csrf-token" => form_authenticity_token
62
42
  }
43
+
44
+ meta_hash = default.deep_merge(opts).deep_merge(@_meta_tags_hash || {})
63
45
 
64
46
  html = ""
65
47
  html << "<title>#{h(meta_hash.delete(:title)) }</title>\n"
@@ -73,44 +55,78 @@ module MetaTagsHelpers
73
55
  html.html_safe
74
56
  end
75
57
 
76
- end
58
+ end #~ ActionViewExtension
77
59
 
78
60
  module ActionControllerExtension
79
61
  extend ActiveSupport::Concern
80
62
  included do
81
- helper_method :meta_title, :meta_description, :meta_image, :meta_type
63
+ helper_method :set_meta, :meta_title, :meta_description, :meta_image, :meta_type, :normalize_meta_hash
64
+ end
65
+
66
+ def _meta_tags_hash
67
+ @_meta_tags_hash ||= {}
68
+ end
69
+
70
+ def set_meta(options)
71
+ _meta_tags_hash.deep_merge(normalize_meta_hash(options))
82
72
  end
83
73
 
84
- def meta_title(*args)
85
- if title = args.first
86
- @meta_title = title
87
- else
88
- @meta_title
74
+ def meta_title(val = nil)
75
+ if val
76
+ @_meta_title = val
77
+ set_meta(:title => val)
89
78
  end
79
+
80
+ @_meta_title
90
81
  end
91
82
 
92
- def meta_description(*args)
93
- if desc = args.first
94
- @meta_description = desc
95
- else
96
- @meta_description
83
+ def meta_description(val = nil)
84
+ if val
85
+ @_meta_description = val
86
+ set_meta(:description => val)
97
87
  end
88
+ @_meta_description
98
89
  end
99
90
 
100
- def meta_image(*args)
101
- if img = args.first
102
- @meta_image = img
103
- else
104
- @meta_image
91
+ def meta_image(val = nil)
92
+ if val
93
+ @_meta_image = val
94
+ set_meta(:og => { :image => val })
105
95
  end
96
+ @_meta_image
106
97
  end
107
98
 
108
- def meta_type(*args)
109
- if t = args.first
110
- @meta_type = t
111
- else
112
- @meta_type
99
+ def meta_type(val = nil)
100
+ if val
101
+ @_meta_type = val
102
+ set_meta(:og => { :type => val })
113
103
  end
104
+ @_meta_type
105
+ end
106
+
107
+ protected
108
+
109
+ def normalize_meta_hash(hash)
110
+
111
+ meta_hash = hash.dup
112
+
113
+ # separates namespaced keys
114
+ namespaces = meta_hash.select { |k,v| v.is_a?(Hash) }
115
+
116
+ # delete nil/false/namespaced keys
117
+ meta_hash.delete_if { |k,v| v.blank? || v == false || v.is_a?(Hash)}
118
+
119
+ namespaces.each { |ns, namespaced|
120
+ namespaced.delete_if { |k,v|
121
+ v.blank? || v == false || v.is_a?(Hash)
122
+ }
123
+ namespaced.each {|k,v|
124
+ meta_hash[:"#{ns}:#{k}"] = v
125
+ }
126
+ }
127
+
128
+ meta_hash
129
+
114
130
  end
115
131
 
116
132
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "meta-tags-helpers"
8
- s.version = "0.1.4"
8
+ s.version = "0.1.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["mcasimir"]
12
- s.date = "2013-01-18"
12
+ s.date = "2013-06-01"
13
13
  s.description = "Rails meta tags helpers"
14
14
  s.email = "maurizio.cas@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.homepage = "http://github.com/mcasimir/kaminari-bootstrap"
29
29
  s.licenses = ["MIT"]
30
30
  s.require_paths = ["lib"]
31
- s.rubygems_version = "1.8.24"
31
+ s.rubygems_version = "1.8.25"
32
32
  s.summary = "Rails meta tags helpers"
33
33
 
34
34
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meta-tags-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-18 00:00:00.000000000 Z
12
+ date: 2013-06-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -89,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  segments:
91
91
  - 0
92
- hash: -3024929928964013503
92
+ hash: 2398148642369134804
93
93
  required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  version: '0'
99
99
  requirements: []
100
100
  rubyforge_project:
101
- rubygems_version: 1.8.24
101
+ rubygems_version: 1.8.25
102
102
  signing_key:
103
103
  specification_version: 3
104
104
  summary: Rails meta tags helpers