Skip to content
This repository
tag: v3.2.9.rc2
Fetching contributors…

Cannot retrieve contributors at this time

file 50 lines (42 sloc) 1.294 kb
1 2 3 4 5 6 7 8 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
module Rails
  module Generators
    class ResourceRouteGenerator < NamedBase

      # Properly nests namespaces passed into a generator
      #
      # $ rails generate resource admin/users/products
      #
      # should give you
      #
      # namespace :admin do
      # namespace :users
      # resources :products
      # end
      # end
      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
        route route_string[2..-1]
      end
      
      private
        def route_string
          @route_string ||= ""
        end

        def write(str, indent)
          route_string << "#{" " * indent}#{str}\n"
        end

        def route_length
          regular_class_path.length
        end
    end
  end
end
Something went wrong with that request. Please try again.