canonical_dude 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{README.rdoc → README.md} +48 -40
- data/canonical_dude.gemspec +8 -8
- data/lib/canonical_dude/action_controller_methods.rb +9 -0
- data/lib/canonical_dude/action_view_methods.rb +46 -0
- data/lib/canonical_dude/railtie.rb +22 -0
- data/lib/canonical_dude/version.rb +1 -1
- data/lib/canonical_dude.rb +17 -2
- metadata +39 -58
- data/init.rb +0 -6
- data/lib/canonical_dude/controller_methods.rb +0 -7
- data/lib/canonical_dude/helper_methods.rb +0 -38
data/{README.rdoc → README.md}
RENAMED
@@ -1,23 +1,23 @@
|
|
1
|
-
= canonical_dude
|
2
|
-
|
3
1
|
canonical_dude ist a small helper for Rails applications to make use of the Canonical URL Tag. Use it to set your
|
4
2
|
preferred version of a URL.
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# or just in your Gemfile
|
12
|
-
gem 'canonical_dude'
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
# for system wide usage
|
8
|
+
gem install 'canonical_dude'
|
13
9
|
|
14
|
-
|
15
|
-
|
10
|
+
# or just in your Gemfile
|
11
|
+
gem 'canonical_dude'
|
12
|
+
```
|
16
13
|
|
17
14
|
|
18
|
-
|
15
|
+
## Usage
|
19
16
|
canonical_dude is really easy to use. You only need to put the following into your HTML head:
|
20
|
-
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
<%= canonical_link_tag %>
|
20
|
+
```
|
21
21
|
|
22
22
|
This is it, you'll now see the canonical URL tag for your current page, because by default, canonical_dude will use the
|
23
23
|
request.url. Probably, this will not quite be what you are looking for since you want to define your canonical url by
|
@@ -27,21 +27,24 @@ extra whiz-bang!
|
|
27
27
|
Let's assume you have a Products model. Given we are in the ProductsController, you can specify the canonical in the
|
28
28
|
following way (taking the #show action as example):
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
```ruby
|
31
|
+
def show
|
32
|
+
@product = Product.find ( params[:id] )
|
33
|
+
canonical_url @product
|
34
|
+
end
|
35
|
+
```
|
35
36
|
|
36
37
|
This is simply defining the canonical tag via the :url_for method provided by Rails, so if you have your products
|
37
38
|
resource defined in routes.rb, then you're good to go. Also, the following examples result in exactly the same
|
38
39
|
canonical URL as shown above:
|
39
40
|
|
40
|
-
|
41
|
-
|
41
|
+
```ruby
|
42
|
+
# wrapped into url_for
|
43
|
+
canonical_url url_for(@product)
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
+
# giving a hash
|
46
|
+
canonical_url :action => "show", :id => @product.id
|
47
|
+
```
|
45
48
|
|
46
49
|
Now you might ask yourself: When it's _just_ using url_for, why do I need this gem altogether? Well, canonical_dude
|
47
50
|
gives you some freedom in defining your canonical urls by checking for special methods. Taking the above example again
|
@@ -54,27 +57,32 @@ Being a good SEO Dude you can, of course, always exactly specify `canonical_url
|
|
54
57
|
have it always in one place and just use `canonical_url @product` in all those controllers. All you need now is a helper
|
55
58
|
which could look like so:
|
56
59
|
|
57
|
-
|
60
|
+
```ruby
|
61
|
+
module CanonicalHelper
|
58
62
|
|
59
|
-
|
60
|
-
|
63
|
+
# alias the AwesomeProduct's canonical url to our initial product url without affecting the awesome_product_url
|
64
|
+
alias_method :canonical_awesome_product_url, :product_url
|
61
65
|
|
62
|
-
|
66
|
+
end
|
67
|
+
```
|
63
68
|
|
64
69
|
|
65
70
|
You can define the following helper methods for any class and canonical_dude will attempt to look for those:
|
66
|
-
def canonical_product_url
|
67
|
-
# ...
|
68
|
-
end
|
69
71
|
|
70
|
-
|
71
|
-
|
72
|
-
|
72
|
+
```ruby
|
73
|
+
def canonical_product_url
|
74
|
+
# ...
|
75
|
+
end
|
76
|
+
|
77
|
+
def canonical_product_path
|
78
|
+
# ...
|
79
|
+
end
|
73
80
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
81
|
+
# and as the _grand_ fallback
|
82
|
+
def canonical_url
|
83
|
+
# ...
|
84
|
+
end
|
85
|
+
```
|
78
86
|
|
79
87
|
|
80
88
|
If you have none of those methods defined, canonical_dude will, fallback again to whatevery :url_for gives you.
|
@@ -83,14 +91,14 @@ render the canonical_link_tag.
|
|
83
91
|
|
84
92
|
Last but not least, you can control when to show your canonical_link_tag with the canonical_url? helper method:
|
85
93
|
|
86
|
-
|
87
|
-
|
94
|
+
```ruby
|
95
|
+
# in your HTML head of layouts/application.html.erb
|
96
|
+
<%= canonical_link_tag if canonical_url? %>
|
97
|
+
```
|
88
98
|
|
89
99
|
|
90
100
|
canonical_url? will return true if you have explicitly defined :canonical_url in your controller. If not, it returns
|
91
101
|
false. So if you only want a canonical tag on specific placed, you have a basic method for it.
|
92
102
|
|
93
103
|
|
94
|
-
== Copyright
|
95
|
-
|
96
104
|
Copyright (c) 2011 Rudolf Schmidt See LICENSE.txt for details.
|
data/canonical_dude.gemspec
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require "canonical_dude/version"
|
2
|
+
require File.expand_path('../lib/canonical_dude/version', __FILE__)
|
4
3
|
|
5
4
|
Gem::Specification.new do |s|
|
6
5
|
s.name = "canonical_dude"
|
7
6
|
s.version = CanonicalDude::VERSION
|
8
|
-
s.platform = Gem::Platform::RUBY
|
9
7
|
s.authors = ["Rudolf Schmidt"]
|
10
|
-
|
8
|
+
|
11
9
|
s.homepage = "http://github.com/rudionrails/canonical_dude"
|
12
10
|
s.summary = %q{Easy canonical URL generation}
|
13
11
|
s.description = %q{canonical_dude is a Rails plugin to easily set your preferred version of a URL via the canonical tag}
|
14
12
|
|
15
13
|
s.rubyforge_project = "canonical_dude"
|
16
14
|
|
17
|
-
s.files = `git ls-files`.split(
|
18
|
-
s.
|
19
|
-
s.
|
15
|
+
s.files = `git ls-files`.split($\)
|
16
|
+
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
18
|
+
|
20
19
|
s.require_paths = ["lib"]
|
21
20
|
|
22
|
-
s.
|
21
|
+
s.add_dependency "rails", "~> 3.2"
|
23
22
|
end
|
23
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module CanonicalDude
|
2
|
+
module ActionViewMethods
|
3
|
+
|
4
|
+
# tag to include within your HTML header, e.g.:
|
5
|
+
# <%= canonical_link_tag %>
|
6
|
+
def canonical_link_tag( url_for_options = nil )
|
7
|
+
url = canonical_url_from( url_for_options || @_canonical_url_for_options || request.url )
|
8
|
+
tag( :link, :rel => 'canonical', :href => url ) if url # custom url methods may sometimes return nil --R
|
9
|
+
end
|
10
|
+
|
11
|
+
# returns true if canonical_url has been explicitly set
|
12
|
+
def canonical_link_tag?
|
13
|
+
!!@_canonical_url_for_options
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def canonical_url_from( url_for_options )
|
20
|
+
case url_for_options
|
21
|
+
when Hash
|
22
|
+
url_for( url_for_options )
|
23
|
+
when String
|
24
|
+
url_for_options
|
25
|
+
else
|
26
|
+
# could be an AR instance, so let's try some custom methods
|
27
|
+
# will turn a User instance into 'user'
|
28
|
+
canonical_method_name = url_for_options.class.name.downcase.gsub( '::', '_' )
|
29
|
+
|
30
|
+
custom_canonical_method_name = [
|
31
|
+
"canonical_#{canonical_method_name}_url",
|
32
|
+
"canonical_#{canonical_method_name}_path",
|
33
|
+
"canonical_url_for"
|
34
|
+
].find { |m| respond_to? m }
|
35
|
+
|
36
|
+
if custom_canonical_method_name
|
37
|
+
send( custom_canonical_method_name, url_for_options )
|
38
|
+
else
|
39
|
+
url_for( url_for_options )
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
module CanonicalDude
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
|
6
|
+
initializer "canonical_dude" do |app|
|
7
|
+
ActiveSupport.on_load :action_controller do
|
8
|
+
require 'canonical_dude/action_controller_methods'
|
9
|
+
|
10
|
+
::ActionController::Base.send :include, CanonicalDude::ActionControllerMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveSupport.on_load :action_view do
|
14
|
+
require 'canonical_dude/action_view_methods'
|
15
|
+
|
16
|
+
ActionView::Base.send :include, CanonicalDude::ActionViewMethods
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
data/lib/canonical_dude.rb
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
module CanonicalDude
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
def self.setup_action_controller( base )
|
4
|
+
base.class_eval do
|
5
|
+
include CanonicalDude::ControllerMethods
|
6
|
+
end
|
7
|
+
end
|
5
8
|
|
9
|
+
def self.setup_action_view( base )
|
10
|
+
base.class_eval do
|
11
|
+
include CanonicalDude::ViewMethods
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
if defined?(Rails::Railtie)
|
17
|
+
require 'canonical_dude/railtie'
|
18
|
+
elsif defined?(Rails::Initializer)
|
19
|
+
raise "canonical_dude is not compatible with Rails 2.3 or older"
|
6
20
|
end
|
21
|
+
|
metadata
CHANGED
@@ -1,92 +1,73 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: canonical_dude
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 0
|
9
|
-
- 0
|
10
|
-
version: 1.0.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Rudolf Schmidt
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
12
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.2'
|
22
|
+
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
|
-
requirements:
|
26
|
+
requirements:
|
27
27
|
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
- 5
|
33
|
-
- 0
|
34
|
-
version: 2.5.0
|
35
|
-
type: :development
|
36
|
-
version_requirements: *id001
|
37
|
-
description: canonical_dude is a Rails plugin to easily set your preferred version of a URL via the canonical tag
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
description: canonical_dude is a Rails plugin to easily set your preferred version
|
31
|
+
of a URL via the canonical tag
|
38
32
|
email:
|
39
33
|
executables: []
|
40
|
-
|
41
34
|
extensions: []
|
42
|
-
|
43
35
|
extra_rdoc_files: []
|
44
|
-
|
45
|
-
files:
|
36
|
+
files:
|
46
37
|
- .gitignore
|
47
38
|
- Gemfile
|
48
39
|
- LICENSE.txt
|
49
|
-
- README.
|
40
|
+
- README.md
|
50
41
|
- Rakefile
|
51
42
|
- canonical_dude.gemspec
|
52
|
-
- init.rb
|
53
43
|
- lib/canonical_dude.rb
|
54
|
-
- lib/canonical_dude/
|
55
|
-
- lib/canonical_dude/
|
44
|
+
- lib/canonical_dude/action_controller_methods.rb
|
45
|
+
- lib/canonical_dude/action_view_methods.rb
|
46
|
+
- lib/canonical_dude/railtie.rb
|
56
47
|
- lib/canonical_dude/version.rb
|
57
|
-
has_rdoc: true
|
58
48
|
homepage: http://github.com/rudionrails/canonical_dude
|
59
49
|
licenses: []
|
60
|
-
|
61
50
|
post_install_message:
|
62
51
|
rdoc_options: []
|
63
|
-
|
64
|
-
require_paths:
|
52
|
+
require_paths:
|
65
53
|
- lib
|
66
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
55
|
none: false
|
68
|
-
requirements:
|
69
|
-
- -
|
70
|
-
- !ruby/object:Gem::Version
|
71
|
-
|
72
|
-
|
73
|
-
- 0
|
74
|
-
version: "0"
|
75
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
61
|
none: false
|
77
|
-
requirements:
|
78
|
-
- -
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
|
81
|
-
segments:
|
82
|
-
- 0
|
83
|
-
version: "0"
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
84
66
|
requirements: []
|
85
|
-
|
86
67
|
rubyforge_project: canonical_dude
|
87
|
-
rubygems_version: 1.
|
68
|
+
rubygems_version: 1.8.25
|
88
69
|
signing_key:
|
89
70
|
specification_version: 3
|
90
71
|
summary: Easy canonical URL generation
|
91
72
|
test_files: []
|
92
|
-
|
73
|
+
has_rdoc:
|
data/init.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
module CanonicalDude::HelperMethods
|
2
|
-
|
3
|
-
# tag to include within your HTML header, e.g.:
|
4
|
-
# <%= canonical_link_tag %>
|
5
|
-
def canonical_link_tag( url_for_options = nil )
|
6
|
-
url = canonical_url_from( url_for_options || @_canonical_url_for_options || request.url )
|
7
|
-
tag( :link, :rel => 'canonical', :href => url ) if url # custom url methods may sometimes return nil --R
|
8
|
-
end
|
9
|
-
|
10
|
-
# returns true if canonical_url has been explicitly set
|
11
|
-
def canonical_url?
|
12
|
-
!!@_canonical_url_for_options
|
13
|
-
end
|
14
|
-
|
15
|
-
|
16
|
-
private
|
17
|
-
|
18
|
-
def canonical_url_from( url_for_options )
|
19
|
-
case url_for_options
|
20
|
-
when Hash then url_for( url_for_options )
|
21
|
-
when String then url_for_options
|
22
|
-
else
|
23
|
-
# could be an AR instance, so let's try some custom methods
|
24
|
-
# will turn a User instance into 'user'
|
25
|
-
canonical_method_name = url_for_options.class.name.downcase.gsub( '::', '_' )
|
26
|
-
|
27
|
-
custom_canonical_method_name = [
|
28
|
-
"canonical_#{canonical_method_name}_url",
|
29
|
-
"canonical_#{canonical_method_name}_path",
|
30
|
-
"canonical_url_for"
|
31
|
-
].find { |m| respond_to? m }
|
32
|
-
|
33
|
-
return url_for( url_for_options ) unless custom_canonical_method_name
|
34
|
-
send( custom_canonical_method_name, url_for_options )
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|