Changeset 1104

Show
Ignore:
Timestamp:
12/14/07 00:20:25 (1 year ago)
Author:
e.@brainspl.at
Message:

add :namespace option to routes. closes #341 [mail@paulbarry.com]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/CHANGELOG

    r1090 r1104  
    66* Fixed merb.show_routes within merb -i 
    77* Fixed image_tag, css_include_tag, and js_include_tag to work with path_prefix 
     8* Adds spec helper methods with_route and dispatch_to 
     9* fix rakefile cfor cygwin 
     10* add count with collection to partial() 
     11* Form control mixin is deprecated Use merb_helpers plugin. 
     12* add redirect matcher to rspec test helpers 
     13* allow r.resource(:foo, :myparam => 42) resource routes pass on params to underlying match() call 
     14* spit out error and help message if you call merb with no args 
     15* get rid of dependency on mongrel for escape and unescape 
     16* make sure not to use write_nonblock when logging to STDOUT 
     17* Fixed image_tag, css_include_tag, and js_include_tag to work with path_prefix 
     18* fix set_status to actually work, add docs, 
     19* config/merb.yml is now correctly loaded from Rake and test environment - using Merb::Server.load_config 
     20* added config option to disable loading of the JSON gem - still enabled by default  
     21* don't raise if names local on a partial is nil 
     22* Use svn export instead of checkout upon merb:freeze_from_svn 
     23* Extracted url and other general methods out of ControllerMixin into GeneralControllerMixin 
     24* fix caching of @_buffer in render, form_for 
     25* Seperates spec helpers into the Merb::Test namespace to prevent spec methods leaking into specs 
     26* Changes the spec url helper method to the same used in the controller 
     27* Made Request#parse_multipart return an empty hash instead of nil if the request is not multipart 
     28* Changes throw_content so that it can be called without a block 
     29*  
     30 
    831 
    932== 0.4.1 "Faster Partials or Partially Faster?" 2007-11-12 
  • trunk/lib/merb/request.rb

    r1066 r1104  
    143143     
    144144    def controller_name 
    145       route_params[:controller] 
     145      if route_params[:namespace] 
     146        route_params[:namespace] + '/' + route_params[:controller] 
     147      else 
     148        route_params[:controller] 
     149      end 
    146150    end 
    147151     
  • trunk/lib/merb/router.rb

    r1097 r1104  
    4343      def generate(name, params = {}, fallback = {}) 
    4444        raise "Named route not found: #{name}" unless @@named_routes.has_key? name 
    45         @@named_routes[name].generate(params, fallback) 
     45        @@named_routes[name].generate(params, fallback).sub(/\/$/, '').squeeze("/") 
    4646      end 
    4747    end # self 
     
    305305 
    306306        singular = name.to_s.singularize 
     307         
     308        namespace = options[:namespace] || merged_params[:namespace] 
     309         
     310        if options[:name_prefix].nil? && namespace != nil 
     311          options[:name_prefix] = "#{namespace}_" 
     312        end 
     313         
    307314        name_prefix = options.delete(:name_prefix) 
    308315 
     
    355362        routes = next_level.to_resource(options) 
    356363 
     364        namespace = options[:namespace] || merged_params[:namespace]  
     365         
     366        if options[:name_prefix].nil? && namespace != nil 
     367          options[:name_prefix] = "#{namespace}_" 
     368        end 
     369         
    357370        route_name = "#{name_prefix}#{name}" 
    358371 
  • trunk/spec/merb/request_spec.rb

    r1066 r1104  
    278278    request.send(:multipart_params).should == {} 
    279279  end 
     280   
     281  it "should add namespace to controller name" do 
     282    request = Merb::Request.new(@in) 
     283    request.stubs(:route_params).returns({:controller => 'bar', :namespace => 'foo'}) 
     284    request.controller_name.should == "foo/bar" 
     285  end 
    280286end 
  • trunk/spec/merb/router_spec.rb

    r1102 r1104  
    187187        to(:controller => "users", :action => "[2]", :id => "[1]") 
    188188       
     189      # Namespace can be used to specify the module 
     190      r.match('/bar').to(:controller => 'bar', :namespace => 'foo') 
     191       
     192      # Namespace can be used to provide path prefix 
     193      r.match('/admin').to(:namespace => 'admin') do |foo| 
     194        foo.match('/foo').to(:controller => 'foo') 
     195      end 
     196      r.match('/foo').to(:controller => 'foo') 
     197         
    189198      # Putting it all together, and adding the requirement that we use an "admin" prefix on the 
    190       # domain (e.g. admin.mysite.com), do some interesting stuff: 
    191       r.match(:domain => /^admin\b/) do |admin| 
     199      # host (e.g. admin.mysite.com), do some interesting stuff: 
     200      r.match(:host => /^admin\b/).to(:namespace => 'admin') do |admin| 
    192201        admin.match(%r[/([A-Z]\w+)\+([A-Z]\w+)/::]). 
    193           to(:controller => "admin/users", :action => ":path[3]", 
     202          to(:controller => "users", :action => ":path[3]", 
    194203            :first_name => ":path[1]", :last_name => ":path[2]") 
    195       end.to(:controller => "admin/users", :action => "default") 
     204      end.to(:controller => "users", :action => "default") 
    196205      # Note that the last line above sends all traffic in the "admin" subdomain to the 
    197206      # Admin::Users#default action if no other route is matched. 
     
    323332  end 
    324333   
     334  it "should use namespace" do 
     335    index, route = Merb::Router.match(SimpleRequest.new(:path => '/bar', :method => :get), {}) 
     336    route[:namespace].should == 'foo' 
     337    route[:controller].should == 'bar' 
     338    route[:action].should == 'index'  
     339    should_only_have_keys(route, :namespace, :controller, :action)    
     340  end 
     341   
     342  it "should have namespace 'admin' if path is '/admin/foo'" do 
     343    index, route = Merb::Router.match(SimpleRequest.new(:path => '/admin/foo', :method => :get), {}) 
     344    route[:namespace].should == 'admin' 
     345    route[:controller].should == 'foo' 
     346    route[:action].should == 'index'  
     347    should_only_have_keys(route, :namespace, :controller, :action)    
     348  end 
     349   
     350  it "should not have namespace if path is just '/foo'" do 
     351    index, route = Merb::Router.match(SimpleRequest.new(:path => '/foo', :method => :get), {}) 
     352    route[:controller].should == 'foo' 
     353    route[:action].should == 'index'  
     354    should_only_have_keys(route, :controller, :action)    
     355  end 
     356   
    325357  it "should send all admin.* domains to the 'admin/users' controller, and 'default' action" do 
    326     index, route = Merb::Router.match(SimpleRequest.new(:domain => "admin.mysite.com", :path => '/welcome', :protocol => "https://"), {}) 
    327     route[:controller].should == "admin/users" 
     358    index, route = Merb::Router.match(SimpleRequest.new(:host => "admin.mysite.com", :path => '/welcome', :protocol => "https://"), {}) 
     359    route[:namespace].should == "admin" 
     360    route[:controller].should == "users" 
    328361    route[:action].should == "default" 
    329     should_only_have_keys(route, :controller, :action) 
     362    should_only_have_keys(route, :namespace, :controller, :action) 
    330363     
    331     index, route = Merb::Router.match(SimpleRequest.new(:domain => "admin.another-site.com", :path => '/go/somewhere/else', :protocol => "https://"), {}) 
    332     route[:controller].should == "admin/users" 
     364    index, route = Merb::Router.match(SimpleRequest.new(:host => "admin.another-site.com", :path => '/go/somewhere/else', :protocol => "https://"), {}) 
     365    route[:namespace].should == "admin" 
     366    route[:controller].should == "users" 
    333367    route[:action].should == "default" 
    334     should_only_have_keys(route, :controller, :action) 
     368    should_only_have_keys(route, :namespace, :controller, :action) 
    335369  end 
    336370   
    337371  it "should decipher the first-name / last-name pairs on an admin.* domain" do 
    338     index, route = Merb::Router.match(SimpleRequest.new(:domain => "admin.mysite.com", :path => '/Duane+Johnson/edit', :protocol => "https://"), {}) 
    339     route[:controller].should == "admin/users" 
     372    index, route = Merb::Router.match(SimpleRequest.new(:host => "admin.mysite.com", :path => '/Duane+Johnson/edit', :protocol => "https://"), {}) 
     373    route[:namespace].should == "admin" 
     374    route[:controller].should == "users"     
    340375    route[:action].should == "edit" 
    341376    route[:first_name].should == "Duane" 
    342377    route[:last_name].should == "Johnson" 
    343     should_only_have_keys(route, :controller, :action, :first_name, :last_name) 
     378    should_only_have_keys(route, :namespace, :controller, :action, :first_name, :last_name) 
    344379  end 
    345380   
     
    747782  end 
    748783end 
     784 
     785describe Merb::Router, "with resources using namespace 'admin'" do 
     786  before(:each) do 
     787    Merb::Router.prepare do |r| 
     788      #Declare one in the nested style 
     789      r.match(:host => /^.*$/).to(:namespace => 'admin') do |admin| 
     790        admin.resources :oranges 
     791      end 
     792      #Declare one in the non-nested style 
     793      r.resources :ape, :namespace => 'admin' 
     794      #Declare resources without a namespace to make sure it's not overridden 
     795      r.resources :oranges 
     796      r.resource :ape 
     797    end 
     798  end 
     799   
     800  it "should generate admin_oranges path" do 
     801    Merb::Router.generate(:admin_oranges).should == '/oranges' 
     802  end 
     803 
     804  it "should generate admin_orange path" do 
     805    Merb::Router.generate(:admin_orange, {:id => 1}).should == '/oranges/1' 
     806    b = Blogposts.new 
     807    Merb::Router.generate(:admin_orange, b).should == '/oranges/42' 
     808    Merb::Router.generate(:admin_orange, :id => b).should == '/oranges/42' 
     809  end 
     810   
     811  it "should generate new_admin_orange path" do 
     812    Merb::Router.generate(:new_admin_orange).should == '/oranges/new' 
     813  end 
     814   
     815  it "should generate edit_admin_orange path" do 
     816    Merb::Router.generate(:edit_admin_orange, {:id => 1}).should == '/oranges/1/edit' 
     817  end   
     818   
     819  it "should generate admin_ape path" do 
     820    Merb::Router.generate(:admin_ape).should == '/ape' 
     821  end 
     822 
     823  it "should generate new_admin_ape path" do 
     824    Merb::Router.generate(:new_admin_ape).should == '/ape/new' 
     825  end 
     826   
     827  it "should generate edit_admin_ape path" do 
     828    Merb::Router.generate(:edit_admin_ape).should == '/ape/edit' 
     829  end   
     830 
     831end