spec/routes_spec.rb in mholling-subdomain_routes-0.1.0 vs spec/routes_spec.rb in mholling-subdomain_routes-0.2.0

- old
+ new

@@ -43,13 +43,13 @@ end it "should downcase the subdomains" do map_subdomain(:Admin, "SUPPORT") { |map| map.options[:subdomains].should == [ "admin", "support" ] } end - - it "should accept a :proc option as the subdomain" do - map_subdomain(:proc => :name) { |name| name.options[:subdomains].should == { :proc => :name } } + + it "should raise ArgumentError if a nil :model option is specified as the subdomain" do + lambda { map_subdomain(:model => "") { } }.should raise_error(ArgumentError) end it "should raise ArgumentError if no subdomain is specified" do lambda { map_subdomain }.should raise_error(ArgumentError) end @@ -130,119 +130,42 @@ [ nil, "" ].each do |none| map_subdomain(none, :www) { |map| map.options[:name_prefix].should == "www_" } end end end - - context "for a single specified subdomain" do - before(:each) do - map_subdomain(:admin) do |map| - map.resources :articles, :has_many => :comments - map.foobar "foobar", :controller => "foo", :action => "bar" - map.named_route "foobaz", "foobaz", :controller => "foo", :action => "baz" - map.connect "/:controller/:action/:id" - end + + context "for a :model subdomain" do + it "should accept a :model option as the subdomain and turn it into a foreign key symbol" do + map_subdomain(:model => :city) { |city| city.options[:subdomains].should == :city_id } end - it "should add the specified subdomain to the route recognition conditions" do - ActionController::Routing::Routes.routes.each do |route| - route.conditions[:subdomains].should == [ "admin" ] - end + it "should singularize a plural model name" do + map_subdomain(:model => :cities) { |city| city.options[:subdomains].should == :city_id } end - - it "should add the subdomain to the route generation requirements" do - ActionController::Routing::Routes.routes.each do |route| - route.requirements[:subdomains].should == [ "admin" ] - end + + it "should accept a string model name" do + map_subdomain(:model => "city") { |city| city.options[:subdomains].should == :city_id } end - end - - context "for multiple specified subdomains" do - before(:each) do - map_subdomain(:support, :admin) do |map| - map.resources :articles, :has_many => :comments - map.foobar "foobar", :controller => "foo", :action => "bar" - map.named_route "foobaz", "foobaz", :controller => "foo", :action => "baz" - map.connect "/:controller/:action/:id" - end + + it "should set the model name as a namespace" do + map_subdomain(:model => :city) { |city| city.options[:namespace].should == "city/" } end - - it "should add the specified subdomain to the route recognition conditions" do - ActionController::Routing::Routes.routes.each do |route| - route.conditions[:subdomains].should == [ "support", "admin" ] - end - end - - it "should not add a subdomain to the route generation requirements" do - ActionController::Routing::Routes.routes.each do |route| - route.requirements[:subdomains].should == [ "support", "admin" ] - end - end - end - - context "for a :proc subdomain" do - it "should set the value a namespace" do - map_subdomain(:proc => :city) { |city| city.options[:namespace].should == "city/" } - end - - it "should prefix the value to named routes" do - map_subdomain(:proc => :city) { |city| city.options[:name_prefix].should == "city_" } - end - it "should set a namespace to the name if specified" do - map_subdomain(:proc => :city, :name => :something) { |city| city.options[:namespace].should == "something/" } + it "should prefix the model name to named routes" do + map_subdomain(:model => :city) { |city| city.options[:name_prefix].should == "city_" } end - - it "should prefix the name to named routes if specified" do - map_subdomain(:proc => :city, :name => :something) { |city| city.options[:name_prefix].should == "something_" } - end - - it "should add the specified proc to the route recognition conditions" do - map_subdomain(:proc => :city) { |city| city.resources :events } - ActionController::Routing::Routes.routes.each do |route| - route.conditions[:subdomains].should == { :proc => :city } - end - end - - it "should add the specified proc to the route generation requirements" do - map_subdomain(:proc => :city) { |city| city.resources :events } - ActionController::Routing::Routes.routes.each do |route| - route.requirements[:subdomains].should == { :proc => :city } - end - end end end describe "ActionController::Routing::Routes" do before(:each) do + SubdomainRoutes::Config.stub!(:domain_length).and_return(2) ActionController::Routing::Routes.clear! + map_subdomain(:www, nil) { |www| www.root :controller => "homes", :action => "show" } end - - it "should allow a subdomain recognition method to be added" do - ActionController::Routing::Routes.subdomain_procs.recognizes?(:city).should be_false - ActionController::Routing::Routes.recognize_subdomain(:city) { |city| city == "perth" } - ActionController::Routing::Routes.subdomain_procs.recognizes?(:city).should be_true - end - - it "should allow subdomain recognition methods to be cleared" do - ActionController::Routing::Routes.recognize_subdomain(:city) { |city| city == "perth" } - ActionController::Routing::Routes.clear! - ActionController::Routing::Routes.subdomain_procs.recognizes?(:city).should be_false - end - it "should flush the cache when called" do - ActionController::Routing::Routes.subdomain_procs.should_receive(:flush!) - begin - ActionController::Routing::Routes.call(ActionController::TestRequest.new.env) - rescue ActionController::RoutingError # no routes defined so the call will raise a routing error - end - end - - it "should not flush the cache when called if SubdomainRoutes::Config.manual_flush is set" do - SubdomainRoutes::Config.stub!(:manual_flush).and_return(true) - ActionController::Routing::Routes.subdomain_procs.should_not_receive(:flush!) - begin - ActionController::Routing::Routes.call(ActionController::TestRequest.new.env) - rescue ActionController::RoutingError # no routes defined so the call will raise a routing error - end + it "should know a list of its reserved subdomains" do + ActionController::Routing::Routes.reserved_subdomains.should == [ "www", "" ] + ActionController::Routing::Routes.clear! + ActionController::Routing::Routes.reserved_subdomains.should be_empty end end