canonical-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
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.
@@ -0,0 +1,24 @@
1
+ CanonicalRails
2
+ ==============
3
+
4
+ This project rocks and uses MIT-LICENSE, foh sho.
5
+
6
+ See The [Google Webmaster Blog Page About Specifying Canonical](http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html) or the [Google Support About rel="canonical"](http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394) as a primer.
7
+
8
+ ## Challenge
9
+
10
+ I've seen a lot of folks do more harm by neglecting canonicalization altogether than by applying to narrowly and conservatively, so here is an attempt to let people start modeslty without spending too much time on it and whitelist parameters as they need to.
11
+
12
+ ## Install
13
+
14
+ gem 'canonical-rails', :git => 'git://github.com/jumph4x/rails-canonical.git'
15
+
16
+ ## Usage
17
+
18
+ First, generate the config
19
+
20
+ rails g canonical_rails:install
21
+
22
+ Then find it in config/initializers/ as canonical_rails.rb
23
+
24
+ Finally, include the canonical_tag helper in your views.
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'CanonicalRails'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rspec/core/rake_task'
29
+
30
+ RSpec::Core::RakeTask.new(:spec)
31
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ module CanonicalRails
2
+ module TagHelper
3
+
4
+ def trailing_slash_needed?
5
+ CanonicalRails.sym_collection_actions.include? request.params['action'].to_sym
6
+ end
7
+
8
+ def trailing_slash_if_needed
9
+ "/" if trailing_slash_needed? and request.path != '/'
10
+ end
11
+
12
+ def canonical_host
13
+ CanonicalRails.host || request.host
14
+ end
15
+
16
+ def canonical_href
17
+ "#{request.protocol}#{canonical_host}#{request.path}#{trailing_slash_if_needed}#{whitelisted_query_string}"
18
+ end
19
+
20
+ def canonical_tag
21
+ tag(:link, :href => canonical_href, :rel => 'canonical')
22
+ end
23
+
24
+ def whitelisted_params
25
+ request.params.select do |key, value|
26
+ value.present? and
27
+ CanonicalRails.sym_whitelisted_parameters.include? key.to_sym
28
+ end
29
+ end
30
+
31
+ def whitelisted_query_string
32
+ "?#{whitelisted_params.to_a.join('=')}" if whitelisted_params.present?
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+
3
+ end
@@ -0,0 +1,34 @@
1
+ require "canonical-rails/engine"
2
+
3
+ module CanonicalRails
4
+
5
+ # Default way to setup CanonicalRails. Run `rails g canonical_rails:install` to create
6
+ # a fresh initializer with all configuration values.
7
+ #
8
+ # the config\setup concept politely observed at and borrowed from Devise: https://github.com/plataformatec/devise/blob/master/lib/devise.rb
9
+
10
+ def self.setup
11
+ yield self
12
+ end
13
+
14
+ mattr_accessor :host
15
+ @@host = nil
16
+
17
+ mattr_accessor :collection_actions
18
+ @@collection_actions = [:index]
19
+
20
+ mattr_accessor :member_actions
21
+ @@member_actions = [:show]
22
+
23
+ mattr_accessor :whitelisted_parameters
24
+ @@whitelisted_parameters = []
25
+
26
+ def self.sym_collection_actions
27
+ @@sym_collection_actions ||= self.collection_actions.map(&:to_sym)
28
+ end
29
+
30
+ def self.sym_whitelisted_parameters
31
+ @@sym_whitelisted_actions ||= self.whitelisted_parameters.map(&:to_sym)
32
+ end
33
+
34
+ end
@@ -0,0 +1,9 @@
1
+ module CanonicalRails
2
+ class Engine < ::Rails::Engine
3
+
4
+ initializer 'canonical_rails.add_helpers' do |app|
5
+ ActionView::Base.send :include, CanonicalRails::TagHelper
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module CanonicalRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ module CanonicalRails
2
+ class InstallGenerator < Rails::Generators::Base
3
+
4
+ def self.source_paths
5
+ paths = []
6
+ paths << File.expand_path('../templates', "../../#{__FILE__}")
7
+ paths << File.expand_path('../templates', "../#{__FILE__}")
8
+ paths << File.expand_path('../templates', __FILE__)
9
+ paths.flatten
10
+ end
11
+
12
+ def add_files
13
+ template 'canonical_rails.rb', 'config/initializers/canonical_rails.rb'
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ # Do yourself a favor and set these up right when you install the engine.
2
+
3
+ CanonicalRails.setup do |config|
4
+
5
+ # This is the main host, not just the TLD, omit slashes and protocol. If you have more than one, pick the one you want to rank in search results.
6
+
7
+ config.host# = 'www.mywebstore.com'
8
+
9
+ # http://en.wikipedia.org/wiki/URL_normalization
10
+ # Trailing slash represents semantics of a directory, ie a collection view - implying an :index get route;
11
+ # otherwise we have to assume semantics of an instance of a resource type, a member view - implying a :show get route
12
+ #
13
+ # Acts as a whitelist for routes to have trailing slashes
14
+
15
+ config.collection_actions# = [:index]
16
+
17
+ # Parameter spamming can cause index dilution by creating seemingly different URLs with identical or near-identical content.
18
+ # Unless whitelisted, these parameters will be omitted
19
+
20
+ config.whitelisted_parameters# = []
21
+
22
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :canonical-rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canonical-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Denis Ivanov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-02 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.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.9'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.9'
62
+ description: ! 'Configurable, but assumes a conservative strategy by default with
63
+ a goal to solve many search engine index problems: multiple hostnames, inbound links
64
+ with arbitrary parameters, trailing slashes. '
65
+ email:
66
+ - visible@jumph4x.net
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - app/helpers/canonical_rails/tag_helper.rb
72
+ - config/routes.rb
73
+ - lib/canonical-rails/version.rb
74
+ - lib/canonical-rails/engine.rb
75
+ - lib/tasks/canonical-rails_tasks.rake
76
+ - lib/canonical-rails.rb
77
+ - lib/generators/canonical_rails/install/install_generator.rb
78
+ - lib/generators/canonical_rails/install/templates/canonical_rails.rb
79
+ - MIT-LICENSE
80
+ - Rakefile
81
+ - README.md
82
+ homepage: http://jumph4x.net
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.24
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Simple and configurable Rails canonical ref tag helper
106
+ test_files: []