Sha256: fa1a3a02a532167dbadcad917061a62c4613120efc9fd1a9df54564c515dadb9

Contents?: true

Size: 1.27 KB

Versions: 38

Compression:

Stored size: 1.27 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
        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

Version data entries

38 entries across 38 versions & 3 rubygems

Version Path
activejob-lock-0.0.2 rails/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.1.10.rc4 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.1.rc4 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.1.10.rc3 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.1.rc3 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.1.10.rc2 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.1.rc2 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.1.10.rc1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.1.rc1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
activejob-lock-0.0.1 rails/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.1.9 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.1.9.rc1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.0 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.0.rc3 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.0.rc2 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.0.rc1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.1.7.1 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.1.8 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.0.beta4 lib/rails/generators/rails/resource_route/resource_route_generator.rb
railties-4.2.0.beta3 lib/rails/generators/rails/resource_route/resource_route_generator.rb