Module: RspecRailsRouting::Matchers::Helpers

Defined in:
lib/rspec_rails_routing/matchers/helpers.rb

Instance Method Summary collapse

Instance Method Details

#description_for(action, path_template, controller) ⇒ Object



76
77
78
# File 'lib/rspec_rails_routing/matchers/helpers.rb', line 76

def description_for( action, path_template, controller )
  "route the RESTful #{action} path #{http_verb_for( action ).to_s.upcase} #{format_url_or_path( path_template )} to #{controller}##{action}"
end

#extract_info(action, args) ⇒ Object



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
# File 'lib/rspec_rails_routing/matchers/helpers.rb', line 9

def extract_info( action, args )
  options = args.extract_options!
  domain  = args.pop

  url_part = args.map do |p|
    if p.is_a?( Symbol )
      p.to_s
    elsif p.is_a?( String )
      "#{p}/1"
    end
  end.join( '/' )

  url_template_part = args.map do |p|
    if p.is_a?( Symbol )
      p.to_s
    elsif p.is_a?( String )
      "#{p}/:#{p.singularize.foreign_key}"
    end
  end.join( '/' )

  namespaces = args.select { |p| p.is_a?( Symbol ) }
  args.shift( namespaces.size )

  nesting_params = Hash[*args.map { |p| [p.singularize.foreign_key.to_sym, '1'] }.flatten]

  if %w(show edit update destroy).include?( action.to_s )
    nesting_params.merge! :id => '1'
  end

  url_end = case action.to_sym
              when :index, :create
                ''
              when :show, :update, :destroy
                '1'
              when :new
                'new'
              when :edit
                '1/edit'
            end

  url_template_end = case action.to_sym
                       when :index, :create
                         ''
                       when :show, :update, :destroy
                         ':id'
                       when :new
                         'new'
                       when :edit
                         ':id/edit'
                     end

  path          = [url_part, domain, url_end].reject( &:blank? ).join( '/' )
  path_template = [url_template_part, domain, url_template_end].reject( &:blank? ).join( '/' )
  url           = "#{options[:host]}/" + path
  controller    = [namespaces, domain].reject( &:blank? ).join( '/' )

  return url, path, path_template, controller, nesting_params
end

#failure_message_for_should_for(action, url) ⇒ Object



80
81
82
# File 'lib/rspec_rails_routing/matchers/helpers.rb', line 80

def failure_message_for_should_for( action, url )
  "failed to recognize the RESTful #{action} path #{http_verb_for( action ).to_s.upcase} #{url}"
end

#failure_message_for_should_not_for(action, url) ⇒ Object



84
85
86
# File 'lib/rspec_rails_routing/matchers/helpers.rb', line 84

def failure_message_for_should_not_for( action, url )
  "recognized the RESTful #{action} path #{http_verb_for( action ).to_s.upcase} #{url}, but should not"
end

#format_url_or_path(url_or_path) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/rspec_rails_routing/matchers/helpers.rb', line 68

def format_url_or_path( url_or_path )
  is_url = !url_or_path.match( /^http/ ).nil?
  has_slash = !url_or_path.match( /^\\/ ).nil?
  needs_slash = !is_url && !has_slash

  "#{needs_slash ? '/' : ''}#{url_or_path}".gsub( /\/1/, '/:id' )
end

#http_verb_for(action) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rspec_rails_routing/matchers/helpers.rb', line 88

def http_verb_for( action )
  case action.to_sym
    when :index, :show, :new, :edit
      :get
    when :create
      :post
    when :update
      :put
    when :destroy
      :delete
  end
end

#routerObject



5
6
7
# File 'lib/rspec_rails_routing/matchers/helpers.rb', line 5

def router
  Rails.application.routes
end