svenfuchs / routing-filter

routing-filter wraps around the complex beast that the Rails routing system is, allowing for unseen flexibility and power in Rails URL recognition and generation.

This URL has Read+Write access

routing-filter / spec / spec_helper.rb
100644 95 lines (79 sloc) 2.661 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
$: << File.dirname(__FILE__)
$: << File.dirname(__FILE__) + '/../lib/'
 
require 'rubygems'
require 'actionpack'
require 'activesupport'
require 'action_controller'
require 'action_controller/test_process'
require 'active_support/vendor'
require 'spec'
 
require 'routing_filter'
require 'routing_filter/locale'
require 'routing_filter/pagination'
 
class Site
end
 
class Section
  def id; 1 end
  alias :to_param :id
  def type; 'Section' end
  def path; 'section' end
end
 
class Article
  def to_param; 1 end
end
 
module RoutingFilterHelpers
  def draw_routes(&block)
    set = returning ActionController::Routing::RouteSet.new do |set|
      class << set; def clear!; end; end
      set.draw &block
      silence_warnings{ ActionController::Routing.const_set 'Routes', set }
    end
    set
  end
 
  def instantiate_controller(params)
    returning ActionController::Base.new do |controller|
      request = ActionController::TestRequest.new
      url = ActionController::UrlRewriter.new(request, params)
      controller.stub!(:request).and_return request
      controller.instance_variable_set :@url, url
      controller
    end
  end
 
  def should_recognize_path(path, params)
    @set.recognize_path(path, {}).should == params
  end
 
  def home_path(*args)
    @controller.send :home_path, *args
  end
 
  def section_path(*args)
    @controller.send :section_path, *args
  end
 
  def section_article_path(*args)
    @controller.send :section_article_path, *args
  end
 
  def admin_articles_path(*args)
    @controller.send :admin_articles_path, *args
  end
 
  def url_for(*args)
    @controller.send :url_for, *args
  end
 
  def setup_environment(*filters)
    RoutingFilter::Locale.locales = [:en, 'en-US', :de, :fi, 'en-UK']
    RoutingFilter::Locale.include_default_locale = true
    I18n.default_locale = :en
    I18n.locale = :en
 
    @controller = instantiate_controller :locale => 'de', :id => 1
    @set = draw_routes do |map|
      yield map if block_given?
      filters.each { |filter| map.filter filter }
      map.section 'sections/:id.:format', :controller => 'sections', :action => "show"
      map.section_article 'sections/:section_id/articles/:id', :controller => 'articles', :action => "show"
      map.admin_articles 'admin/articles/:id', :controller => 'admin/articles', :action => "index"
      map.home '/', :controller => 'home', :action => 'index'
    end
 
    @section_params = {:controller => 'sections', :action => "show", :id => "1"}
    @article_params = {:controller => 'articles', :action => "show", :section_id => "1", :id => "1"}
    @locale_filter = @set.filters.first
    @pagination_filter = @set.filters.last
  end
end