Class: RailsUpgrade::Upgraders::RouteRedrawer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-upgrade/upgraders/routes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouteRedrawer

Returns a new instance of RouteRedrawer.



30
31
32
33
34
35
36
# File 'lib/rails-upgrade/upgraders/routes.rb', line 30

def initialize
  @routes = []
  @default_route_generated = false
  
  # Setup the stack for parents
  self.class.stack = [@routes]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



114
115
116
117
# File 'lib/rails-upgrade/upgraders/routes.rb', line 114

def method_missing(m, *args)
  debug "named route: #{m}"
  current_parent << FakeRoute.new(args.shift, args.pop, m.to_s)
end

Instance Attribute Details

#routesObject

Returns the value of attribute routes.



27
28
29
# File 'lib/rails-upgrade/upgraders/routes.rb', line 27

def routes
  @routes
end

Class Method Details

.indentObject



119
120
121
# File 'lib/rails-upgrade/upgraders/routes.rb', line 119

def self.indent
  ' ' * ((stack.length) * 2)
end

Instance Method Details

#connect(path, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails-upgrade/upgraders/routes.rb', line 43

def connect(path, options={})
  debug "connecting #{path}"
  
  if (path == ":controller/:action/:id.:format" || path == ":controller/:action/:id")
    if !@default_route_generated
      current_parent << FakeRoute.new("/:controller(/:action(/:id))", {:default_route => true})
    
      @default_route_generated = true
    end
  else
    current_parent << FakeRoute.new(path, options)
  end
end

#namespace(name) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/rails-upgrade/upgraders/routes.rb', line 103

def namespace(name)
  debug "mapping namespace #{name}"
  namespace = FakeNamespace.new(name)
  
  namespace = stack(namespace) do
    yield(self)
  end
  
  current_parent << namespace
end

#resource(*args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rails-upgrade/upgraders/routes.rb', line 80

def resource(*args)
  if block_given?
    parent = FakeSingletonResourceRoute.new(args.shift)
    debug "mapping resource #{parent.name} with block"
    
    parent = stack(parent) do
      yield(self)
    end
    
    current_parent << parent
  else
    if args.last.is_a?(Hash)
      current_parent << FakeSingletonResourceRoute.new(args.shift, args.pop)
      debug "mapping resources #{current_parent.last.name} w/o block with args"
    else
      args.each do |a|
        current_parent << FakeSingletonResourceRoute.new(a)
        debug "mapping resources #{current_parent.last.name}"
      end
    end
  end
end

#resources(*args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rails-upgrade/upgraders/routes.rb', line 57

def resources(*args)
  if block_given?
    parent = FakeResourceRoute.new(args.shift)
    debug "mapping resources #{parent.name} with block"
    
    parent = stack(parent) do
      yield(self)
    end
    
    current_parent << parent
  else
    if args.last.is_a?(Hash)
      current_parent << FakeResourceRoute.new(args.shift, args.pop)
      debug "mapping resources #{current_parent.last.name} w/o block with args"
    else
      args.each do |a|              
        current_parent << FakeResourceRoute.new(a)
        debug "mapping resources #{current_parent.last.name}"
      end
    end
  end
end

#root(options) ⇒ Object



38
39
40
41
# File 'lib/rails-upgrade/upgraders/routes.rb', line 38

def root(options)
  debug "mapping root"
  @routes << FakeRoute.new("/", options)
end