class Rails::Generators::ControllerGenerator
:nodoc:
def add_routes
def add_routes unless options[:skip_routes] actions.reverse_each do |action| # route prepends two spaces onto the front of the string that is passed, this corrects that. route generate_routing_code(action)[2..-1] end end end
def create_controller_files
def create_controller_files template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb") end
def generate_routing_code(action)
end
get 'baz/index'
namespace :bar do
namespace :foo do
Will generate -
For eg. rails g controller foo/bar/baz index
This method creates nested route entry for namespaced resources.
def generate_routing_code(action) depth = regular_class_path.length # Create 'namespace' ladder # namespace :foo do # namespace :bar do namespace_ladder = regular_class_path.each_with_index.map do |ns, i| indent(" namespace :#{ns} do\n", i * 2) end.join # Create route # get 'baz/index' route = indent(%{ get '#{file_name}/#{action}'\n}, depth * 2) # Create `end` ladder # end # end end_ladder = (1..depth).reverse_each.map do |i| indent("end\n", i * 2) end.join # Combine the 3 parts to generate complete route entry namespace_ladder + route + end_ladder end