Created
September 22, 2010 05:14
-
-
Save dougo-chris/591189 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# put this file in your spec/support folder | |
module RulesEngine | |
module RouteMatcher | |
class Matcher | |
attr_reader :route_params, :with_params | |
def initialize(params = {}) | |
@route_params = params | |
@with_params = params.map{|key, value| ":#{key} => #{value}"}.join(' ') | |
end | |
end | |
def with_route_params(params, &block) | |
@route_matcher = RulesEngine::RouteMatcher::Matcher.new(params) | |
yield | |
@route_matcher = nil | |
end | |
def match_route html_method, controller, routes | |
matcher = @route_matcher || RulesEngine::RouteMatcher::Matcher.new() | |
routes.each do |method, path| | |
it "routes #{html_method} #{path} to #{controller}##{method} #{matcher.with_params}" do | |
{ html_method => "#{path}" }.should route_to( | |
{:controller => controller.to_s, | |
:action => method.to_s }.merge(matcher.route_params) | |
) | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe "routing to profiles" do | |
extend RulesEngine::RouteMatcher | |
describe "/re_plans" do | |
# index methods | |
match_route(:get, :re_plans, :index => '/re_plans') | |
match_route(:get, :re_plans, :new => '/re_plans/new') | |
match_route(:post, :re_plans, :create => '/re_plans') | |
with_route_params(:id => "101") do | |
match_route(:get, :re_plans, :show => '/re_plans/101', | |
:edit => '/re_plans/101/edit') | |
match_route(:put, :re_plans, :update => '/re_plans/101') | |
... | |
describe "/re_plans/101/workflows" do | |
with_route_params(:re_plan_id => "101") do | |
match_route(:get, :re_plan_workflows, :new => '/re_plans/101/workflows/new') | |
match_route(:post, :re_plan_workflows, :create => '/re_plans/101/workflows') | |
end | |
with_route_params(:re_plan_id => "101", :id => "202") do | |
match_route(:get, :re_plan_workflows, :show => '/re_plans/101/workflows/202', |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment