Permalink
Browse files

Add a configuration option to replace the canoncial subdomain with al…

…t subdomains. Fix for issue #1
  • Loading branch information...
1 parent 328bf63 commit 0a00aae48379e8fa2344f086a01af6a7fd76639f @matthewrobertson committed Feb 26, 2013
Showing with 42 additions and 7 deletions.
  1. +4 −0 lib/ress.rb
  2. +1 −1 lib/ress/config.rb
  3. +1 −0 lib/ress/view_helpers.rb
  4. +5 −0 spec/ress/subdomain_spec.rb
  5. +15 −6 spec/ress/view_helpers_spec.rb
  6. +16 −0 spec/ress_spec.rb
View
@@ -31,6 +31,10 @@ def include_modernizr?
config.include_modernizr
end
+ def replace_canonical_subdomain?
+ config.replace_canonical_subdomain
+ end
+
def configure
yield(config)
end
View
@@ -2,7 +2,7 @@ module Ress
class Config
- attr_accessor :include_modernizr
+ attr_accessor :include_modernizr, :replace_canonical_subdomain
attr_reader :canonical_version, :alternate_versions
def initialize
@@ -5,6 +5,7 @@ module ViewHelpers
def ress_annotation_tags
path = "#{request.host_with_port}#{request.fullpath}"
html = if canonical_request?
+ path.gsub!("#{request.subdomain}.", '') if Ress.replace_canonical_subdomain?
Ress.alternate_versions.map do |category|
category.link_tag(request.protocol, path, self)
end.join
@@ -69,6 +69,11 @@
'http://foo.bar.com/some/stuff'
end
+ it 'replaces alternate subdomains with the cannonical subdomain' do
+ subdomain.url('http://', 'm.bar.com/some/stuff', 'm').should ==
+ 'http://foo.bar.com/some/stuff'
+ end
+
end
end
@@ -37,18 +37,27 @@
let(:request) { stub('request', :protocol => 'http://', :host_with_port => 'foo.com', :fullpath => '/bar', :subdomain => '') }
before { view.stub(:canonical_request? => true) }
- it 'returns an empty string if there are no registered categories' do
- view.ress_annotation_tags.should == ''
+ context 'with no alternate versions' do
+ it 'returns an empty string' do
+ view.ress_annotation_tags.should == ''
+ end
end
- it 'generates the link tags when there is one category' do
+ context 'with one alternate version' do
+ before(:all) { Ress.configure { |r| r.add_alternate :name => 'm', :media => 'stuff' } }
- Ress.configure do |r|
- r.add_alternate :name => 'm', :media => 'stuff'
+ it 'generates the link tags when there is one category' do
+ view.ress_annotation_tags.should ==
+ "<link href=\"http://m.foo.com/bar\" id=\"m\" media=\"stuff\" rel=\"alternate\" />"
end
- view.ress_annotation_tags.should ==
+ it 'replaces the canonical subdomain when configured' do
+ request.stub(:host_with_port => 'www.foo.com', :subdomain => 'www')
+ Ress.stub(:replace_canonical_subdomain? => true )
+ view.ress_annotation_tags.should ==
"<link href=\"http://m.foo.com/bar\" id=\"m\" media=\"stuff\" rel=\"alternate\" />"
+ end
+
end
end
View
@@ -51,4 +51,20 @@
end
+ describe '.replace_canonical_subdomain?' do
+
+ it 'defaults to false' do
+ Ress.replace_canonical_subdomain?.should be_false
+ end
+
+ it 'can be altered through Ress.configure' do
+ Ress.configure { |r| r.replace_canonical_subdomain = true }
+ Ress.replace_canonical_subdomain?.should be_true
+
+ Ress.configure { |r| r.replace_canonical_subdomain = false }
+ Ress.replace_canonical_subdomain?.should be_false
+ end
+
+ end
+
end

0 comments on commit 0a00aae

Please sign in to comment.