Sha256: dfc38be66e680b2af9fa5e194e751614d5307d96dfb2ff0bfdd14feb9035d1bb

Contents?: true

Size: 1.39 KB

Versions: 55

Compression:

Stored size: 1.39 KB

Contents

module Rails
  module Generators
    class ResourceRouteGenerator < NamedBase # :nodoc:
      # 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
      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

      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

Version data entries

55 entries across 55 versions & 4 rubygems

Version Path
railties-5.1.7 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.7.rc1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.6.2 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.0.7.2 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.6.1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.0.7.1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.6 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.0.7 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.5 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.5.rc1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.4 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.0.6 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.4.rc1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.0.6.rc1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.3 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.3.rc3 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.0.5 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.0.5.rc2 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.1.3.rc2 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-5.0.5.rc1 lib/rails/generators/rails/resource_route/resource_route_generator.rb