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.
@@ -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
- == Installation
7
- == The Gem Version
8
- # for system wide usage
9
- gem install 'canonical_dude'
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
- === The Plugin Version
15
- script/plugin install http://github.com/rudionrails/canonical_dude.git
10
+ # or just in your Gemfile
11
+ gem 'canonical_dude'
12
+ ```
16
13
 
17
14
 
18
- == Usage
15
+ ## Usage
19
16
  canonical_dude is really easy to use. You only need to put the following into your HTML head:
20
- <%= canonical_link_tag %>
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
- def show
32
- @product = Product.find ( params[:id] )
33
- canonical_url @product
34
- end
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
- # wrapped into url_for
41
- canonical_url url_for(@product)
41
+ ```ruby
42
+ # wrapped into url_for
43
+ canonical_url url_for(@product)
42
44
 
43
- # giving a hash
44
- canonical_url :action => "show", :id => @product.id
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
- module CanonicalHelper
60
+ ```ruby
61
+ module CanonicalHelper
58
62
 
59
- # alias the AwesomeProduct's canonical url to our initial product url without affecting the awesome_product_url
60
- alias_method :canonical_awesome_product_url, :product_url
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
- end
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
- def canonical_product_path
71
- # ...
72
- end
72
+ ```ruby
73
+ def canonical_product_url
74
+ # ...
75
+ end
76
+
77
+ def canonical_product_path
78
+ # ...
79
+ end
73
80
 
74
- # and as the _grand_ fallback
75
- def canonical_url
76
- # ...
77
- end
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
- # in your HTML head of layouts/application.html.erb
87
- <%= canonical_link_tag if canonical_url? %>
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.
@@ -1,23 +1,23 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
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("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
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.add_development_dependency 'rspec', '~> 2.5.0'
21
+ s.add_dependency "rails", "~> 3.2"
23
22
  end
23
+
@@ -0,0 +1,9 @@
1
+ module CanonicalDude
2
+ module ActionControllerMethods
3
+
4
+ def canonical_url( url_for_options = {} )
5
+ self.instance_variable_set( "@_canonical_url_for_options", url_for_options )
6
+ end
7
+
8
+ end
9
+ end
@@ -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
+
@@ -1,3 +1,3 @@
1
1
  module CanonicalDude
2
- VERSION = "1.0.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -1,6 +1,21 @@
1
1
  module CanonicalDude
2
2
 
3
- autoload :ControllerMethods, File.dirname(__FILE__) + '/canonical_dude/controller_methods'
4
- autoload :HelperMethods, File.dirname(__FILE__) + '/canonical_dude/helper_methods'
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
- hash: 23
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
- date: 2011-03-28 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: rspec
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
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
- requirements:
26
+ requirements:
27
27
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 27
30
- segments:
31
- - 2
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.rdoc
40
+ - README.md
50
41
  - Rakefile
51
42
  - canonical_dude.gemspec
52
- - init.rb
53
43
  - lib/canonical_dude.rb
54
- - lib/canonical_dude/controller_methods.rb
55
- - lib/canonical_dude/helper_methods.rb
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
- hash: 3
72
- segments:
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
- hash: 3
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.5.0
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,6 +0,0 @@
1
- require 'canonical_dude'
2
-
3
- if defined?( Rails )
4
- ActionController::Base.send :include, CanonicalDude::ControllerMethods
5
- ActionView::Base.send :include, CanonicalDude::HelperMethods
6
- end
@@ -1,7 +0,0 @@
1
- module CanonicalDude::ControllerMethods
2
-
3
- def canonical_url( url_for_options = {} )
4
- self.instance_variable_set( "@_canonical_url_for_options", url_for_options )
5
- end
6
-
7
- end
@@ -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