Class: Rails::Generators::ResourceRouteGenerator

Inherits:
NamedBase
  • Object
show all
Defined in:
lib/rails/generators/rails/resource_route/resource_route_generator.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from NamedBase

#initialize

Methods inherited from Base

base_root, class_option, default_source_root, desc, hide!, hook_for, inherited, namespace, remove_hook_for, source_root

Methods included from Actions

#add_source, #after_bundle, #capify!, #environment, #gem, #gem_group, #generate, #git, #initialize, #initializer, #lib, #rails_command, #rake, #rakefile, #readme, #route, #vendor

Constructor Details

This class inherits a constructor from Rails::Generators::NamedBase

Instance Method Details

#add_resource_routeObject

Properly nests namespaces passed into a generator

$ rails generate resource admin/users/products

should give you

namespace :admin do
  namespace :users do
    resources :products
  end
end


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails/generators/rails/resource_route/resource_route_generator.rb', line 15

def add_resource_route
  return if options[:actions].present?

  # iterates over all namespaces and opens up blocks
  regular_class_path.each_with_index do |namespace, index|
    write("namespace :#{namespace} do", index + 1)
  end

  # inserts the primary resource
  write("resources :#{file_name.pluralize}", route_length + 1)

  # ends blocks
  regular_class_path.each_index do |index|
    write("end", route_length - index)
  end

  # route prepends two spaces onto the front of the string that is passed, this corrects that.
  # Also it adds a \n to the end of each line, as route already adds that
  # we need to correct that too.
  route route_string[2..-2]
end