jt-rails-meta 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +3 -0
- data/LICENSE +19 -0
- data/README.md +105 -0
- data/jt-rails-meta.gemspec +13 -0
- data/lib/generators/rails_meta/USAGE +3 -0
- data/lib/generators/rails_meta/install_generator.rb +11 -0
- data/lib/generators/rails_meta/templates/meta.yml +21 -0
- data/lib/jt-rails-meta.rb +154 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 28d40d524f437254f790d03e11d624832e9aca78
|
4
|
+
data.tar.gz: 58ee97c8f8bea3f9524beaa5596335e11a210170
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c8a9eb16ad7119f6191432e5b0da4573efad1c904a33b7d739632f59db1bf0e5ec1c930357e8174fdadc7d6c14f6009a9fe064f1825551e40d97985625629be
|
7
|
+
data.tar.gz: eca6440a137c5e9841e4e7c1c5e1d3c72bf07ec4dde447f5d2ac16080752166abbb831fd6df0ab3029c56fc6b8b45c2c771c342690cfd1786f09a38ec3f953b4
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2015 Jonathan Tribouharet
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# JTRailsMeta
|
2
|
+
|
3
|
+
JTRailsMeta help you to manage HTML meta tags like title, description, keywords used in Search Engine Optimization (SEO).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
JTRailsMeta is distributed as a gem, which is how it should be used in your app.
|
8
|
+
|
9
|
+
Include the gem in your Gemfile:
|
10
|
+
|
11
|
+
gem 'jt-rails-meta', '~> 1.0'
|
12
|
+
|
13
|
+
Create a `meta.yml` file for the translation:
|
14
|
+
|
15
|
+
rails g rails_meta:install
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
### Basic usage
|
20
|
+
|
21
|
+
Include `JTRailsMeta` in your `ApplicationController`:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class ApplicationController < ActionController::Base
|
25
|
+
include JTRailsMeta
|
26
|
+
...
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Call `meta_tags` in your layout:
|
31
|
+
|
32
|
+
```html
|
33
|
+
<!DOCTYPE html>
|
34
|
+
<head>
|
35
|
+
<meta charset="UTF-8" />
|
36
|
+
<%= meta_tags %>
|
37
|
+
</head>
|
38
|
+
<body>
|
39
|
+
</body>
|
40
|
+
```
|
41
|
+
|
42
|
+
You have also access to `meta_title`, `meta_description`, `meta_keywords` methods.
|
43
|
+
|
44
|
+
### Pass parameters to tags
|
45
|
+
|
46
|
+
In your controller:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class PostsController < ApplicationController
|
50
|
+
def show
|
51
|
+
@post = Post.find(params[:id])
|
52
|
+
|
53
|
+
set_meta_title({ title: @post.title })
|
54
|
+
set_meta_description({ title: @post.title, author: @post.author })
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
In your `meta.yml` file:
|
60
|
+
|
61
|
+
```yaml
|
62
|
+
en:
|
63
|
+
meta:
|
64
|
+
posts:
|
65
|
+
show:
|
66
|
+
title: "%{title}"
|
67
|
+
description: "Post about %{title} by %{author}"
|
68
|
+
```
|
69
|
+
|
70
|
+
### Add more tags
|
71
|
+
|
72
|
+
You can add more tags with `add_meta_extra` and `add_meta_link` methods:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
add_meta_extra 'robots' => 'noindex,nofollow'
|
76
|
+
add_meta_extra {
|
77
|
+
twitter: {
|
78
|
+
site: '@mywebsite',
|
79
|
+
domain: 'mywebsite.com',
|
80
|
+
title: meta_title,
|
81
|
+
description: meta_description,
|
82
|
+
image: [
|
83
|
+
http://mywebsite.com/image_1.jpg'),
|
84
|
+
http://mywebsite.com/image_2.jpg')
|
85
|
+
]
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
add_meta_link 'author', 'https://github.com/jonathantribouharet'
|
90
|
+
add_meta_link 'publisher', 'https://github.com/jonathantribouharet'
|
91
|
+
```
|
92
|
+
|
93
|
+
There is some methods already created using `add_meta_link` method:
|
94
|
+
- `add_meta_link_canonical` which is equivalent to `add_meta_link 'canonical'`
|
95
|
+
- `add_meta_link_author` which is equivalent to `add_meta_link 'author'`
|
96
|
+
- `add_meta_link_publisher` which is equivalent to `add_meta_link 'publisher'`
|
97
|
+
- `add_meta_link_alternate` which is equivalent to `add_meta_link 'alternate'`
|
98
|
+
|
99
|
+
## Author
|
100
|
+
|
101
|
+
- [Jonathan Tribouharet](https://github.com/jonathantribouharet) ([@johntribouharet](https://twitter.com/johntribouharet))
|
102
|
+
|
103
|
+
## License
|
104
|
+
|
105
|
+
JTRailsMeta is released under the MIT license. See the LICENSE file for more info.
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'jt-rails-meta'
|
3
|
+
s.summary = "Manage HTML meta tags for Ruby On Rails"
|
4
|
+
s.description = "JTRailsMeta help you to manage HTML meta tags like title, description, keywords used in Search Engine Optimization (SEO)."
|
5
|
+
s.homepage = 'https://github.com/jonathantribouharet/jt-rails-meta'
|
6
|
+
s.version = '1.0.0'
|
7
|
+
s.files = `git ls-files`.split("\n")
|
8
|
+
s.require_paths = ['lib']
|
9
|
+
s.authors = ['Jonathan TRIBOUHARET']
|
10
|
+
s.email = 'jonathan.tribouharet@gmail.com'
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.platform = Gem::Platform::RUBY
|
13
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
en:
|
2
|
+
meta:
|
3
|
+
|
4
|
+
# in general you use either prefx or suffix
|
5
|
+
# prefix or suffix are not applied on default title
|
6
|
+
prefix: "My Website - " # Optional
|
7
|
+
suffix: " - My Website" # Optional
|
8
|
+
|
9
|
+
# default meta used if no meta are found for a page
|
10
|
+
default:
|
11
|
+
title: My WebSite
|
12
|
+
description: My super website is about something magnificent
|
13
|
+
keywords: "website, some keywords"
|
14
|
+
|
15
|
+
# Exemple of meta for the controller home and the action index
|
16
|
+
# title, description and keywords are optional
|
17
|
+
home:
|
18
|
+
index:
|
19
|
+
title: Home
|
20
|
+
description: Description of homepage
|
21
|
+
keywords: home
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module JTRailsMeta
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
include ActionView::Helpers::TagHelper
|
5
|
+
|
6
|
+
included do
|
7
|
+
before_action :create_meta_hash
|
8
|
+
|
9
|
+
helper_method :meta_tags, :meta_title, :meta_description, :meta_keywords
|
10
|
+
end
|
11
|
+
|
12
|
+
# Generate HTML tags title, description, keywords and others meta
|
13
|
+
def meta_tags
|
14
|
+
output = ""
|
15
|
+
|
16
|
+
output += content_tag 'title', meta_title
|
17
|
+
output += "\n"
|
18
|
+
output += tag 'meta', name: 'description', content: meta_description
|
19
|
+
output += "\n"
|
20
|
+
output += tag 'meta', name: 'keywords', content: meta_keywords
|
21
|
+
output += "\n"
|
22
|
+
|
23
|
+
for link in @meta[:links]
|
24
|
+
output += tag 'link', link[:options]
|
25
|
+
output += "\n"
|
26
|
+
end
|
27
|
+
|
28
|
+
for extra_params in @meta[:extra]
|
29
|
+
output += tag 'meta', name: extra_params[:name], content: extra_params[:content]
|
30
|
+
output += "\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
output.html_safe
|
34
|
+
end
|
35
|
+
|
36
|
+
# Content of meta title
|
37
|
+
def meta_title
|
38
|
+
@meta[:title] ||= set_meta_title
|
39
|
+
end
|
40
|
+
|
41
|
+
# Content of meta description
|
42
|
+
def meta_description
|
43
|
+
@meta[:description] ||= set_meta_description
|
44
|
+
end
|
45
|
+
|
46
|
+
# Content of meta keywords
|
47
|
+
def meta_keywords
|
48
|
+
@meta[:keywords] ||= set_meta_keywords
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generate meta title
|
52
|
+
# Use meta.default.title if no meta found for the current controller/action
|
53
|
+
# Params:
|
54
|
+
# +options+:: options passed to I18n
|
55
|
+
def set_meta_title(options = {})
|
56
|
+
@meta[:title] = I18n.translate("#{meta_key}.title", options)
|
57
|
+
|
58
|
+
if !have_translation?(@meta[:title])
|
59
|
+
@meta[:title] = I18n.translate('meta.default.title')
|
60
|
+
else
|
61
|
+
@meta[:title] = "#{@meta[:prefix]}#{I18n.translate("#{meta_key}.title", options)}#{@meta[:suffix]}"
|
62
|
+
end
|
63
|
+
|
64
|
+
@meta[:title]
|
65
|
+
end
|
66
|
+
|
67
|
+
# Generate meta description
|
68
|
+
# Use meta.default.description if no meta found for the current controller/action
|
69
|
+
# Params:
|
70
|
+
# +options+:: options passed to I18n
|
71
|
+
def set_meta_description(options = {})
|
72
|
+
@meta[:description] = I18n.translate("#{meta_key}.description", options)
|
73
|
+
@meta[:description] = I18n.translate('meta.default.description') if !have_translation?(@meta[:description])
|
74
|
+
@meta[:description]
|
75
|
+
end
|
76
|
+
|
77
|
+
# Generate meta keywords
|
78
|
+
# Use meta.default.keywords if no meta found for the current controller/action
|
79
|
+
# Params:
|
80
|
+
# +options+:: options passed to I18n
|
81
|
+
def set_meta_keywords(options = {})
|
82
|
+
@meta[:keywords] = I18n.translate("#{meta_key}.keywords", options)
|
83
|
+
@meta[:keywords] = I18n.translate('meta.default.keywords') if !have_translation?(@meta[:keywords])
|
84
|
+
@meta[:keywords]
|
85
|
+
end
|
86
|
+
|
87
|
+
# Add meta other than title, description, keywords
|
88
|
+
# Params:
|
89
|
+
# +extra_params+:: hash containing the meta(s) wanted
|
90
|
+
def add_meta_extra(extra_params, previous_key = nil)
|
91
|
+
for key, value in extra_params
|
92
|
+
current_key = previous_key ? "#{previous_key}:#{key}" : key
|
93
|
+
|
94
|
+
if value.is_a?(String) || value.is_a?(Symbol)
|
95
|
+
@meta[:extra] << { name: current_key, content: value.to_s }
|
96
|
+
elsif value.is_a?(Hash)
|
97
|
+
add_meta_extra(value, current_key)
|
98
|
+
elsif value.is_a?(Array)
|
99
|
+
for v in value
|
100
|
+
@meta[:extra] << { name: current_key, content: v.to_s }
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Add links to meta, used for add 'canonicalæ or 'publisher' links
|
107
|
+
def add_meta_link(rel, href, options = {})
|
108
|
+
options.merge!(rel: rel, href: href)
|
109
|
+
@meta[:links] << {options: options}
|
110
|
+
end
|
111
|
+
|
112
|
+
# Helpers
|
113
|
+
|
114
|
+
def add_meta_link_canonical(url)
|
115
|
+
add_meta_link 'canonical', url
|
116
|
+
end
|
117
|
+
|
118
|
+
def add_meta_link_author(url)
|
119
|
+
add_meta_link 'author', url
|
120
|
+
end
|
121
|
+
|
122
|
+
def add_meta_link_publisher(url)
|
123
|
+
add_meta_link 'publisher', url
|
124
|
+
end
|
125
|
+
|
126
|
+
def add_meta_link_alternate(url, lang)
|
127
|
+
add_meta_link 'alternate', url, hreflang: lang
|
128
|
+
end
|
129
|
+
|
130
|
+
private
|
131
|
+
|
132
|
+
def create_meta_hash
|
133
|
+
@meta = {
|
134
|
+
extra: [],
|
135
|
+
links: []
|
136
|
+
}
|
137
|
+
|
138
|
+
@meta[:prefix] = I18n.translate('meta.prefix')
|
139
|
+
@meta[:prefix] = "" if !have_translation?(@meta[:prefix])
|
140
|
+
|
141
|
+
@meta[:suffix] = I18n.translate('meta.suffix')
|
142
|
+
@meta[:suffix] = "" if !have_translation?(@meta[:suffix])
|
143
|
+
end
|
144
|
+
|
145
|
+
# Key used by I18n for the current controller/action
|
146
|
+
def meta_key
|
147
|
+
"meta.#{request[:controller].sub('/', '.')}.#{request[:action]}"
|
148
|
+
end
|
149
|
+
|
150
|
+
def have_translation?(text)
|
151
|
+
!text.start_with?('translation missing')
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jt-rails-meta
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan TRIBOUHARET
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: JTRailsMeta help you to manage HTML meta tags like title, description,
|
14
|
+
keywords used in Search Engine Optimization (SEO).
|
15
|
+
email: jonathan.tribouharet@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- jt-rails-meta.gemspec
|
25
|
+
- lib/generators/rails_meta/USAGE
|
26
|
+
- lib/generators/rails_meta/install_generator.rb
|
27
|
+
- lib/generators/rails_meta/templates/meta.yml
|
28
|
+
- lib/jt-rails-meta.rb
|
29
|
+
homepage: https://github.com/jonathantribouharet/jt-rails-meta
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.0.14
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Manage HTML meta tags for Ruby On Rails
|
53
|
+
test_files: []
|