canonical_dude 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ # RubyMine
7
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in canonical_dude.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Rudolf Schmidt
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,96 @@
1
+ = canonical_dude
2
+
3
+ canonical_dude ist a small helper for Rails applications to make use of the Canonical URL Tag. Use it to set your
4
+ preferred version of a URL.
5
+
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'
13
+
14
+ === The Plugin Version
15
+ script/plugin install http://github.com/rudionrails/canonical_dude.git
16
+
17
+
18
+ == Usage
19
+ canonical_dude is really easy to use. You only need to put the following into your HTML head:
20
+ <%= canonical_link_tag %>
21
+
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
+ request.url. Probably, this will not quite be what you are looking for since you want to define your canonical url by
24
+ yourself. Not a problem. You also have a controller method available to custom define your canonical URL with some
25
+ extra whiz-bang!
26
+
27
+ Let's assume you have a Products model. Given we are in the ProductsController, you can specify the canonical in the
28
+ following way (taking the #show action as example):
29
+
30
+
31
+ def show
32
+ @product = Product.find ( params[:id] )
33
+ canonical_url @product
34
+ end
35
+
36
+ This is simply defining the canonical tag via the :url_for method provided by Rails, so if you have your products
37
+ resource defined in routes.rb, then you're good to go. Also, the following examples result in exactly the same
38
+ canonical URL as shown above:
39
+
40
+ # wrapped into url_for
41
+ canonical_url url_for(@product)
42
+
43
+ # giving a hash
44
+ canonical_url :action => "show", :id => @product.id
45
+
46
+ Now you might ask yourself: When it's _just_ using url_for, why do I need this gem altogether? Well, canonical_dude
47
+ gives you some freedom in defining your canonical urls by checking for special methods. Taking the above example again
48
+ with a standard rasource approach, you'll have the following URL to the products detail page: /products/:id.
49
+ Also, you have :product_path and :product_url defined as route helpers. Now if there's another product class defined
50
+ called AwesomeProduct ( which inherits from Product but obviously does extra cool stuff ;-), you might have another
51
+ URL for that, too. Sometimes, you have even more of those cases with all different kinds of URLs.
52
+
53
+ Being a good SEO Dude you can, of course, always exactly specify `canonical_url product_url(@product)`. Or you could
54
+ have it always in one place and just use `canonical_url @product` in all those controllers. All you need now is a helper
55
+ which could look like so:
56
+
57
+ module CanonicalHelper
58
+
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
61
+
62
+ end
63
+
64
+
65
+ 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
+
70
+ def canonical_product_path
71
+ # ...
72
+ end
73
+
74
+ # and as the _grand_ fallback
75
+ def canonical_url
76
+ # ...
77
+ end
78
+
79
+
80
+ If you have none of those methods defined, canonical_dude will, fallback again to whatevery :url_for gives you.
81
+ Also worth noting is, that, if you defined any custom canonical_* method which returns nil, canonical_dude will not
82
+ render the canonical_link_tag.
83
+
84
+ Last but not least, you can control when to show your canonical_link_tag with the canonical_url? helper method:
85
+
86
+ # in your HTML head of layouts/application.html.erb
87
+ <%= canonical_link_tag if canonical_url? %>
88
+
89
+
90
+ canonical_url? will return true if you have explicitly defined :canonical_url in your controller. If not, it returns
91
+ false. So if you only want a canonical tag on specific placed, you have a basic method for it.
92
+
93
+
94
+ == Copyright
95
+
96
+ Copyright (c) 2011 Rudolf Schmidt See LICENSE.txt for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "canonical_dude/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "canonical_dude"
7
+ s.version = CanonicalDude::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Rudolf Schmidt"]
10
+
11
+ s.homepage = "http://github.com/rudionrails/canonical_dude"
12
+ s.summary = %q{Easy canonical URL generation}
13
+ s.description = %q{canonical_dude is a Rails plugin to easily set your preferred version of a URL via the canonical tag}
14
+
15
+ s.rubyforge_project = "canonical_dude"
16
+
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) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency 'rspec', '~> 2.5.0'
23
+ end
data/init.rb ADDED
@@ -0,0 +1,6 @@
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
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,38 @@
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
@@ -0,0 +1,3 @@
1
+ module CanonicalDude
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ module CanonicalDude
2
+
3
+ autoload :ControllerMethods, File.dirname(__FILE__) + '/canonical_dude/controller_methods'
4
+ autoload :HelperMethods, File.dirname(__FILE__) + '/canonical_dude/helper_methods'
5
+
6
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canonical_dude
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Rudolf Schmidt
14
+ autorequire:
15
+ bindir: bin
16
+ 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
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
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
38
+ email:
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - LICENSE.txt
49
+ - README.rdoc
50
+ - Rakefile
51
+ - canonical_dude.gemspec
52
+ - init.rb
53
+ - lib/canonical_dude.rb
54
+ - lib/canonical_dude/controller_methods.rb
55
+ - lib/canonical_dude/helper_methods.rb
56
+ - lib/canonical_dude/version.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/rudionrails/canonical_dude
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ 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
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: canonical_dude
87
+ rubygems_version: 1.5.0
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Easy canonical URL generation
91
+ test_files: []
92
+