Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
794
On Slideshare
794
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
3
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide




















Transcript

  • 1. RSpec2 Route Matcher Chris Douglas mail dougo.chris@gmail.com twitter dougo_chris github dougochris blog www.r3ef.com /Articles/2010/10/12/rspec2-route-matcher.html
  • 2. http://github.com/rspec/rspec-rails describe "routing to profiles" do it "routes /profile/:username to profile#show for username" do { :get => "/profiles/jsmith" }.should route_to( :controller => "profiles", :action => "show", :username => "jsmith" ) end it "does not expose a list of profiles" do { :get => "/profiles" }.should_not be_routable end end
  • 3. My Routes to Test resources :re_plans do resources :workflows, :controller => :re_plan_workflows, :except => [:index] do resources :rules, :controller => :re_plan_workflow_rules, :except => [:index, :show] do collection do get 'help' get 'error' end member do put 'move_up' put 'move_down' end end end end
  • 4. Before Route Matcher Pg.1 describe "routes" do it "routes get /re_plans/101/workflows/202/rules/new to re_plan_workflow_rules#new" do { get => "/re_plans/101/workflows/202/rules/new" }.should route_to( :controller => "re_plan_workflow_rules", :action => "new", :re_plan_id => "101", :workflow_id => "202", ) end it "routes post /re_plans/101/workflows/202/rules to re_plan_workflow_rules#create" do { post => "/re_plans/101/workflows/202/rules" }.should route_to( :controller => "re_plan_workflow_rules", :action => "create", :re_plan_id => "101", :workflow_id => "202", ) end it "routes get /re_plans/101/workflows/202/rules/help to re_plan_workflow_rules#help" do { get => "/re_plans/101/workflows/202/rules/help" }.should route_to( :controller => "re_plan_workflow_rules", :action => "help", :re_plan_id => "101", :workflow_id => "202", ) end
  • 5. Before Route Matcher Pg.2 it "routes get /re_plans/101/workflows/202/rules/error to re_plan_workflow_rules#error" do { get => "/re_plans/101/workflows/202/rules/error" }.should route_to( :controller => "re_plan_workflow_rules", :action => "error", :re_plan_id => "101", :workflow_id => "202", ) end it "routes get /re_plans/101/workflows/202/rules/303/edit to re_plan_workflow_rules#edit" do { get => "/re_plans/101/workflows/202/rules/303/edit" }.should route_to( :controller => "re_plan_workflow_rules", :action => "edit", :re_plan_id => "101", :workflow_id => "202", :id => "303", ) end it "routes put /re_plans/101/workflows/202/rules/303 to re_plan_workflow_rules#update" do { put => "/re_plans/101/workflows/202/rules/303" }.should route_to( :controller => "re_plan_workflow_rules", :action => "update", :re_plan_id => "101", :workflow_id => "202", :id => "303", ) end
  • 6. Before Route Matcher Pg.3 it "routes delete /re_plans/101/workflows/202/rules/303 to re_plan_workflow_rules#destroy" do { delete => "/re_plans/101/workflows/202/rules/303" }.should route_to( :controller => "re_plan_workflow_rules", :action => "destroy", :re_plan_id => "101", :workflow_id => "202", :id => "303", ) end it "routes put /re_plans/101/workflows/202/rules/303/move_up to re_plan_workflow_rules#move_up" { put => "/re_plans/101/workflows/202/rules/303/move_up" }.should route_to( :controller => "re_plan_workflow_rules", :action => "move_up", :re_plan_id => "101", :workflow_id => "202", :id => "303", ) end it "routes put /re_plans/101/workflows/202/rules/303/move_down to re_plan_workflow_rules#move_d { put => "/re_plans/101/workflows/202/rules/303/move_down" }.should route_to( :controller => "re_plan_workflow_rules", :action => "move_down", :re_plan_id => "101", :workflow_id => "202", :id => "303", ) end
  • 7. Ouch! - Too much repetition :controller => "re_plan_workflow_rules", :re_plan_id => "101", :workflow_id => "202", :id => "303",
  • 8. After Route Matcher describe "routes" do extend RulesEngine::RouteMatcher describe "/re_plans/101/workflows/202/rules" do match_controller(:re_plan_workflow_rules, :re_plan_id => "101", :workflow_id => "202") do with_route(:get, '/re_plans/101/workflows/202/rules/new' => :new) with_route(:post, '/re_plans/101/workflows/202/rules' => :create) with_route(:get, '/re_plans/101/workflows/202/rules/help' => :help) with_route(:get, '/re_plans/101/workflows/202/rules/error' => :error) end match_controller(:re_plan_workflow_rules, :re_plan_id => "101", :workflow_id => "202", :id => "303") do with_route(:get, '/re_plans/101/workflows/202/rules/303/edit' => :edit) with_route(:put, '/re_plans/101/workflows/202/rules/303' => :update) with_route(:delete, '/re_plans/101/workflows/202/rules/303' => :destroy) with_route(:put, '/re_plans/101/workflows/202/rules/303/move_up' => :move_up) with_route(:put, '/re_plans/101/workflows/202/rules/303/move_down' => :move_down) end end
  • 9. How it Works module RulesEngine module RouteMatcher class Matcher attr_reader :route_controller, :route_params, :with_params def initialize(route_controller, params = {}) @route_controller = route_controller.to_s @route_params = params @with_params = params.map{|key, value| ":#{key} => #{value}"}.join(' ') end end def match_controller(route_controller, params = {}, &block) @route_matcher = RulesEngine::RouteMatcher::Matcher.new(route_controller, params) yield @route_matcher = nil end def with_route(html_method, routes) matcher = @route_matcher routes.each do |path, method| it "routes #{html_method} #{path} to #{matcher.route_controller}##{method} #{matcher.wi { html_method => "#{path}" }.should route_to( {:controller => matcher.route_controller, :action => method.to_s }.merge(matcher.route_params) ) end end end end
  • 10. Questions ? Chris Douglas mail dougo.chris@gmail.com twitter dougo_chris github dougochris blog www.r3ef.com /Articles/2010/10/12/rspec2-route-matcher.html