Skip to content

Commit

Permalink
Fix specs and rails 3 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
gix committed Jan 4, 2011
1 parent 4da3b21 commit addd2c8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 55 deletions.
48 changes: 25 additions & 23 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
begin
require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
#rescue LoadError
# puts "You need to install RSpec in your base app. Add 'rspec-rails' to your Gemfile."
# exit
end
require 'action_controller/railtie'
require 'active_support/core_ext/hash/slice'
require 'rspec'
require 'subdomain-fu'

Rails.env = 'test'

plugin_spec_dir = File.dirname(__FILE__)
ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
module SubdomainFu
class TestApplication < Rails::Application
end
end

Rails3::Application.routes.draw do |map|
map.needs_subdomain '/needs_subdomain', :controller => "fu", :action => "awesome"
map.no_subdomain '/no_subdomain', :controller => "fu", :action => "lame"
map.needs_awesome '/needs_awesome', :controller => "fu", :action => "lame"
SubdomainFu::TestApplication.routes.draw do
match '/needs_subdomain' => "fu#awesome", :as => 'needs_subdomain'
match '/no_subdomain' => "fu#lame", :as => 'no_subdomain'
match '/needs_awesome' => "fu#lame", :as => 'needs_awesome'

map.resources :foos do |fu|
fu.resources :bars
resources :foos do
resources :bars
end

map.connect '/', :controller => "site", :action => "home", :conditions => {:subdomain => false}
map.connect '/', :controller => "app", :action => "home", :conditions => {:subdomain => true}
map.connect '/', :controller => "mobile", :action => "home", :conditions => {:subdomain => "m"}
match '/' => "site#home", :constraints => { :subdomain => '' }
#match '/' => "app#home", :constraints => { :subdomain => true }
match '/' => "mobile#home", :constraints => { :subdomain => "m" }

map.connect '/subdomain_here', :controller => "app", :action => "success", :conditions => {:subdomain => true}
map.connect '/no_subdomain_here', :controller => "site", :action => "success", :conditions => {:subdomain => false}
map.connect '/m_subdomain_here', :controller => "mobile", :action => "success", :conditions => {:subdomain => "m"}
map.connect '/numbers_only_here', :controller => "numbers", :action => "success", :conditions => {:subdomain => /[0-9]+/}
#match '/subdomain_here' => "app#success", :constraints => { :subdomain => true }
match '/no_subdomain_here' => "site#success", :constraints => { :subdomain => '' }
match '/m_subdomain_here' => "mobile#success", :constraints => { :subdomain => "m" }
match '/numbers_only_here' => "numbers#success", :constraints => { :subdomain => /[0-9]+/ }

map.connect '/:controller/:action/:id'
match ':controller(/:action(/:id(.:format)))'
end

class Paramed
Expand All @@ -39,4 +41,4 @@ def to_param
end
end

include ActionController::UrlWriter
include Rails.application.routes.url_helpers
42 changes: 21 additions & 21 deletions spec/subdomain_fu_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

describe "SubdomainFu" do
before do
SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
SubdomainFu.mirrors = SubdomainFu::DEFAULT_MIRRORS.dup
SubdomainFu.preferred_mirror = nil
SubdomainFu.config.tld_sizes = SubdomainFu::Configuration.defaults[:tld_sizes].dup
SubdomainFu.config.mirrors = SubdomainFu::Configuration.defaults[:mirrors].dup
SubdomainFu.config.preferred_mirror = nil
end

describe "TLD Sizes" do
before do
SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
SubdomainFu.config.tld_sizes = SubdomainFu::Configuration.defaults[:tld_sizes].dup
end

it { SubdomainFu.tld_sizes.should be_kind_of(Hash) }
it { SubdomainFu.config.tld_sizes.should be_kind_of(Hash) }

it "should have default values for development, test, and production" do
SubdomainFu.tld_sizes[:development].should == 0
SubdomainFu.tld_sizes[:test].should == 0
SubdomainFu.tld_sizes[:production].should == 1
SubdomainFu.config.tld_sizes[:development].should == 0
SubdomainFu.config.tld_sizes[:test].should == 0
SubdomainFu.config.tld_sizes[:production].should == 1
end

it "#tld_size should be for the current environment" do
SubdomainFu.tld_size.should == SubdomainFu.tld_sizes[RAILS_ENV.to_sym]
SubdomainFu.config.tld_size.should == SubdomainFu.config.tld_sizes[Rails.env.to_sym]
end

it "should be able to be set for the current environment" do
SubdomainFu.tld_size = 5
SubdomainFu.tld_size.should == 5
SubdomainFu.tld_sizes[:test].should == 5
SubdomainFu.config.tld_size = 5
SubdomainFu.config.tld_size.should == 5
SubdomainFu.config.tld_sizes[:test].should == 5
end
end

Expand All @@ -37,7 +37,7 @@
end

it "should be false for mirrored subdomains" do
SubdomainFu.has_subdomain?(SubdomainFu.mirrors.first).should be_false
SubdomainFu.has_subdomain?(SubdomainFu.config.mirrors.first).should be_false
end

it "shoud be false for a nil or blank subdomain" do
Expand All @@ -50,11 +50,11 @@
describe "#subdomain_from" do
it "should return the subdomain based on the TLD of the current environment" do
SubdomainFu.subdomain_from("awesome.localhost").should == "awesome"
SubdomainFu.tld_size = 2
SubdomainFu.config.tld_size = 2
SubdomainFu.subdomain_from("awesome.localhost.co.uk").should == "awesome"
SubdomainFu.tld_size = 1
SubdomainFu.config.tld_size = 1
SubdomainFu.subdomain_from("awesome.localhost.com").should == "awesome"
SubdomainFu.tld_size = 0
SubdomainFu.config.tld_size = 0
end

it "should join deep subdomains with a period" do
Expand All @@ -75,7 +75,7 @@
describe "#preferred_mirror?" do
describe "when preferred_mirror is false" do
before do
SubdomainFu.preferred_mirror = false
SubdomainFu.config.preferred_mirror = false
end

it "should return true for false" do
Expand All @@ -98,7 +98,7 @@
end

it "should not change the subdomain for a host the same or smaller than the tld size" do
SubdomainFu.tld_size = 1
SubdomainFu.config.tld_size = 1
SubdomainFu.rewrite_host_for_subdomains("cool","localhost").should == "localhost"
end

Expand All @@ -116,7 +116,7 @@

describe "when preferred_mirror is false" do
before do
SubdomainFu.preferred_mirror = false
SubdomainFu.config.preferred_mirror = false
end

it "should remove the subdomain if passed false when it is a mirror" do
Expand Down Expand Up @@ -219,7 +219,7 @@

describe "when preferred_mirror is false" do
before do
SubdomainFu.preferred_mirror = false
SubdomainFu.config.preferred_mirror = false
end

it { SubdomainFu.needs_rewrite?("www","www.localhost").should be_false }
Expand All @@ -236,7 +236,7 @@

describe "when preferred_mirror is string" do
before do
SubdomainFu.preferred_mirror = "www"
SubdomainFu.config.preferred_mirror = "www"
end

it { SubdomainFu.needs_rewrite?("www","www.localhost").should be_false }
Expand Down
22 changes: 11 additions & 11 deletions spec/url_rewriter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

describe "SubdomainFu URL Writing" do
before do
SubdomainFu.tld_size = 1
SubdomainFu.mirrors = SubdomainFu::DEFAULT_MIRRORS.dup
SubdomainFu.override_only_path = true
SubdomainFu.preferred_mirror = nil
SubdomainFu.config.tld_size = 1
SubdomainFu.config.mirrors = SubdomainFu::Configuration.defaults[:mirrors].dup
SubdomainFu.config.override_only_path = true
SubdomainFu.config.preferred_mirror = nil
default_url_options[:host] = "example.com"
end

Expand All @@ -23,12 +23,12 @@
end

it "should should not force the full url with :only_path if override_only_path is false (default)" do
SubdomainFu.override_only_path = false
SubdomainFu.config.override_only_path = false
url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "/something/other"
end

it "should should force the full url, even with :only_path if override_only_path is true" do
SubdomainFu.override_only_path = true
SubdomainFu.config.override_only_path = true
url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "http://awesome.example.com/something/other"
end
end
Expand Down Expand Up @@ -112,8 +112,8 @@

describe "Preferred Mirror" do
before do
SubdomainFu.preferred_mirror = "www"
SubdomainFu.override_only_path = true
SubdomainFu.config.preferred_mirror = "www"
SubdomainFu.config.override_only_path = true
end

it "should switch to the preferred mirror instead of no subdomain" do
Expand All @@ -132,18 +132,18 @@
end

it "should force a switch to no subdomain on a mirror if preferred_mirror is false" do
SubdomainFu.preferred_mirror = false
SubdomainFu.config.preferred_mirror = false
default_url_options[:host] = "www.example.com"
needs_subdomain_url(:subdomain => false).should == "http://example.com/needs_subdomain"
end

after do
SubdomainFu.preferred_mirror = nil
SubdomainFu.config.preferred_mirror = nil
end
end

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

0 comments on commit addd2c8

Please sign in to comment.