Class: RailsUpgrade::Upgraders::FakeResourceRoute

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

Direct Known Subclasses

FakeSingletonResourceRoute

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RouteObject

#indent_lines, #opts_to_string

Constructor Details

#initialize(name, options = {}) ⇒ FakeResourceRoute

Returns a new instance of FakeResourceRoute.



222
223
224
225
226
227
# File 'lib/rails-upgrade/upgraders/routes.rb', line 222

def initialize(name, options = {})
  @name = name
  @children = []
  @options = options
  @indent = RouteRedrawer.indent
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



220
221
222
# File 'lib/rails-upgrade/upgraders/routes.rb', line 220

def children
  @children
end

#nameObject

Returns the value of attribute name.



220
221
222
# File 'lib/rails-upgrade/upgraders/routes.rb', line 220

def name
  @name
end

Instance Method Details

#<<(val) ⇒ Object



266
267
268
# File 'lib/rails-upgrade/upgraders/routes.rb', line 266

def <<(val)
  @children << val
end

#custom_methodsObject



242
243
244
245
246
# File 'lib/rails-upgrade/upgraders/routes.rb', line 242

def custom_methods
  collection_code = generate_custom_methods_for(:collection)
  member_code = generate_custom_methods_for(:member)
  [collection_code, member_code]
end

#generate_custom_methods_for(group) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/rails-upgrade/upgraders/routes.rb', line 248

def generate_custom_methods_for(group)
  return "" unless @options[group]
  
  method_code = []
  
  RouteRedrawer.stack << self
  @options[group].each do |k, v|
    method_code << "#{v} :#{k}"
  end
  RouteRedrawer.stack.pop
  
  indent_lines ["#{group} do", method_code, "end"].flatten
end

#lastObject



270
271
272
# File 'lib/rails-upgrade/upgraders/routes.rb', line 270

def last
  @children.last
end

#route_methodObject



262
263
264
# File 'lib/rails-upgrade/upgraders/routes.rb', line 262

def route_method
  "resources"
end

#to_route_codeObject



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rails-upgrade/upgraders/routes.rb', line 229

def to_route_code
  
  if !@children.empty? || @options.has_key?(:collection) || @options.has_key?(:member)
    prefix = ["#{route_method} :#{@name} do"]
    lines = prefix + custom_methods + [@children.map {|r| r.to_route_code}.join("\n"), "end"]
  
    indent_lines(lines)
  else
    base = "#{route_method} :%s"
    indent_lines [base % [@name]]
  end
end