Permalink
Browse files

link tag helper method is working

  • Loading branch information...
1 parent 3b9708d commit a64be8a7fb7143d28e490edb931e9807ab93c45e @matthewrobertson committed Jan 25, 2013
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm gemset use ress
View
@@ -1,6 +1,10 @@
+require "action_view"
+
require "ress/version"
require "ress/device_category"
require "ress/category_collection"
+require "ress/controller_additions"
+require "ress/view_helpers"
module Ress
extend self
@@ -14,6 +14,14 @@ def register_category(options)
categories << category
end
+ def base_host_name(host_with_port)
+ if categories.any? { |cat| cat.matches?(host_with_port) }
+ host_with_port.gsub(/^[^\.]+\./, '')
+ else
+ host_with_port
+ end
+ end
+
private
def categories
@@ -23,6 +23,6 @@ def prepend_category_view_path
if defined? ActionController::Base
ActionController::Base.class_eval do
- include CanCan::ControllerAdditions
+ include Ress::ControllerAdditions
end
end
@@ -19,6 +19,10 @@ def view_path
@view_path || default_view_path
end
+ def add_subdomain(base_url)
+ "#{subdomain}.#{base_url}"
+ end
+
private
def default_view_path
View
@@ -0,0 +1,24 @@
+module Ress
+
+ module ViewHelpers
+
+ # Create a tag of this format:
+ # `<link rel="alternate" href="http://foo.com" id="desktop" media="only screen and (touch-enabled: 0)">`
+ def ress_category_tag(category, base_url)
+ href = "#{request.protocol}#{category.add_subdomain(base_url)}"
+ tag :link, :rel => 'alternate', :href => href, :id => category.id, :media => category.media_query
+ end
+
+ def ress_link_tags
+ Ress.categories.base_host_name(request.host_with_port)
+ request.fullpath
+ Ress.categories.each do |category|
+ ress_category_tag(category)
+ end
+ end
+
+ end
+
+end
+
+ActionView::Base.send :include, Ress::ViewHelpers
View
@@ -17,5 +17,7 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]
+ gem.add_dependency("actionpack", "~> 3.0")
+
gem.add_development_dependency("rspec")
end
@@ -3,7 +3,7 @@
describe Ress::CategoryCollection do
- describe '.register_category' do
+ describe '#register_category' do
let(:collection) { Ress::CategoryCollection.new }
@@ -25,6 +25,21 @@
end
+ describe '#base_host_name' do
+
+ let(:collection) { Ress::CategoryCollection.new }
+
+ it 'returns the paramter if there are no matching categories' do
+ collection.base_host_name('foo.stuff').should == 'foo.stuff'
+ end
+
+ it 'drops the first subdomain if there is a matching category' do
+ collection.register_category(:name => 'foo', :media_query => 'foo')
+ collection.base_host_name('foo.stuff.baz').should == 'stuff.baz'
+ end
+
+ end
+
describe '#each' do
it 'delegates the @categories' do
@@ -56,4 +56,13 @@
end
+ describe '#add_subdomain' do
+
+ let(:category) { Ress::DeviceCategory.new('mobile', 'some media query', :subdomain => 'foo') }
+
+ it 'prepends the subdomain to the url' do
+ category.add_subdomain('bar.com').should == 'foo.bar.com'
+ end
+ end
+
end
@@ -0,0 +1,24 @@
+require 'rspec'
+require_relative '../../lib/ress'
+
+describe ActionView::Base do
+
+ describe '#ress_category_tag' do
+
+ let(:view) { ActionView::Base.new }
+ let(:request) { stub('request', :protocol => 'http://') }
+ let(:category) { stub('category', :id => 'm', :media_query => 'stuff') }
+
+ before do
+ view.stub(:request => request)
+ end
+
+ it 'generates the href from the category' do
+ category.should_receive(:add_subdomain).and_return('m.foo/bar')
+ view.ress_category_tag(category, 'foo/bar').should ==
+ "<link href=\"http://m.foo/bar\" id=\"m\" media=\"stuff\" rel=\"alternate\" />"
+ end
+
+ end
+
+end

0 comments on commit a64be8a

Please sign in to comment.