Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added a controller / helper method to get the url to force the canoni…

…cal version
  • Loading branch information...
commit c6de025880c86075989f5571de6c314e7320883f 1 parent 1700ab2
@matthewrobertson authored
View
18 lib/ress/canonical_version.rb
@@ -19,19 +19,17 @@ def matches?(subdomain)
# Create a tag of this format:
# `<link rel="canonical" href="http://www.example.com/page-1" >`
def link_tag(protocol, fullpath, subdomain, view)
- view.tag :link, :rel => 'canonical', :href => href(protocol, fullpath, subdomain)
+ view.tag :link, :rel => 'canonical', :href => url(protocol, fullpath, subdomain)
end
- private
-
- def href(protocol, fullpath, subdomain)
- fullpath = fullpath[(subdomain.length + 1)..-1] unless subdomain.empty?
- if self.subdomain
- "#{protocol}#{self.subdomain}.#{fullpath}"
- else
- "#{protocol}#{fullpath}"
- end
+ def url(protocol, fullpath, subdomain)
+ fullpath = fullpath[(subdomain.length + 1)..-1] unless subdomain.empty?
+ if self.subdomain
+ "#{protocol}#{self.subdomain}.#{fullpath}"
+ else
+ "#{protocol}#{fullpath}"
end
+ end
end
View
9 lib/ress/controller_additions.rb
@@ -9,6 +9,7 @@ module ClassMethods
def self.included(base)
base.extend ClassMethods
base.helper_method :canonical_request?
+ base.helper_method :force_canonical_url
base.before_filter :prepend_category_view_path
end
@@ -22,6 +23,14 @@ def canonical_request?
Ress.canonical_version.matches?(request.subdomain)
end
+ def force_canonical_url
+ path = "#{request.host_with_port}#{request.fullpath}"
+ url = Ress.canonical_version.url(request.protocol, path, request.subdomain)
+ sep = url.include?('?') ? '&' : '?'
+
+ "#{url}#{sep}force=1"
+ end
+
end
end
View
25 spec/ress/controller_additions_spec.rb
@@ -69,4 +69,29 @@ def self.helper_method(*splat) ; end
end
+ describe '#force_canonical_url' do
+
+ let(:controller) { ActionControllerStub.new }
+
+ before do
+ @request = stub({
+ :subdomain => 'foo',
+ :host_with_port => 'foo.bar.com',
+ :protocol => 'http://'
+ })
+ controller.stub(:request => @request)
+ end
+
+ it 'appends params to the current url properly if there are no GET params' do
+ @request.stub(:fullpath => '/some_place')
+ controller.force_canonical_url.should == 'http://bar.com/some_place?force=1'
+ end
+
+ it 'appends params to the current url properly when there are GET params' do
+ @request.stub(:fullpath => '/some_place?param=something')
+ controller.force_canonical_url.should == 'http://bar.com/some_place?param=something&force=1'
+ end
+
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.