Skip to content

Commit

Permalink
Autotest is now working, added specs for resource-routing url writing…
Browse files Browse the repository at this point in the history
…. Routing extensions aren't working, no idea why.
  • Loading branch information
Michael Bleigh committed Jun 17, 2008
1 parent 20e59df commit 915de7a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 10 deletions.
6 changes: 6 additions & 0 deletions lib/autotest/discover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Need this to get picked up by autotest?
$:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))

Autotest.add_discovery do
"rspec"
end
10 changes: 7 additions & 3 deletions lib/subdomain_fu/routing_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ def self.included(base)

def recognition_conditions_with_subdomain
result = recognition_conditions_without_subdomain
result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true
result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true && conditions[:subdomain] != false
result << "SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == true
result << "!SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == false
result
end
end

module RouteSetExtensions
def self.included(base)
base.alias_method_chain :extract_request_environment, :subdomain
base.alias_method_chain :extract_request_environment, :subdomain
end

def extract_request_environment_with_subdomain(request)
env = extract_request_environment_without_subdomain(request)
env.merge(:host => request.host, :domain => request.domain, :subdomain => SubdomainFu.subdomain_from(request.host))
end
end
end
end

ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
3 changes: 0 additions & 3 deletions rails/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

ActionController::Base.send :include, SubdomainFu::Controller

ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions

RAILS_DEFAULT_LOGGER.info("** SubdomainFu: initialized properly")
5 changes: 5 additions & 0 deletions spec/routing_extension_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require File.dirname(__FILE__) + '/spec_helper'

describe "SubdomainFu Routing" do
it "should not recognize a subdomainless request for a subdomain-required request"
end
7 changes: 7 additions & 0 deletions spec/spec.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--colour
--format
specdoc
--loadby
mtime
--reverse
--backtrace
20 changes: 17 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@

ActionController::Routing::Routes.draw do |map|
map.needs_subdomain '/needs_subdomain', :controller => "fu", :action => "awesome", :conditions => {:subdomain => true}
map.resources :fu_somethings, :conditions => {:subdomain => true}
map.no_subdomain '/no_subdomain', :controller => "fu", :action => "lame", :conditions => {:subdomain => false}
map.needs_awesome '/needs_awesome', :controller => "fu", :action => "lame", :conditions => {:subdomain => 'awesome'}

map.resources :foos, :conditions => {:subdomain => true} do |fu|
fu.resources :bars
end

map.connect '/:controller/:action/:id'
end

class Paramed
def initialize(param)
@param = param
end

def to_param
@param || "param"
end
end

include ActionController::UrlWriter
default_url_options[:host] = "testapp.com"
include ActionController::UrlWriter
26 changes: 25 additions & 1 deletion spec/url_rewriter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe "SubdomainFu URL Writing" do
before do
SubdomainFu.tld_size = 1
default_url_options[:host] = "testapp.com"
end

describe "#url_for" do
Expand Down Expand Up @@ -43,8 +44,31 @@
end
end

describe "Resourced Routes" do
it "should be able to add a subdomain" do
foo_path(:id => "something", :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something"
end

it "should be able to remove a subdomain" do
default_url_options[:host] = "awesome.testapp.com"
foo_path(:id => "something", :subdomain => false).should == "http://testapp.com/foos/something"
end

it "should work when passed in a paramable object" do
foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something"
end

it "should work on nested resource collections" do
foo_bars_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something/bars"
end

it "should work on nested resource members" do
foo_bar_path(Paramed.new("something"),Paramed.new("else"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something/bars/else"
end
end

after do
SubdomainFu.tld_size = 0
default_url_options[:host] = "testapp.com"
default_url_options[:host] = "localhost"
end
end

0 comments on commit 915de7a

Please sign in to comment.