Sha256: 9d48fecd9cf5c36ac25906089f3090ca7de926a1a311dbc839191f1f283c262e

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

class OscMacheteRails::ResourceRouteGenerator < Rails::Generators::NamedBase
  source_root File.expand_path('../templates', __FILE__)

  # Properly nests namespaces passed into a generator
  #
  #   $ rails generate osc_machete_rails:workflow_route admin/users/products
  #
  # should give you
  #
  #   namespace :admin do
  #     namespace :users
  #       resources :products do
  #         member do
  #           put 'submit'
  #           put 'copy'
  #         end
  #       end
  #     end
  #   end
  def add_workflow_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} do", route_length + 1)
    write("member do", route_length + 2)
    write("put 'submit'", route_length + 3)
    write("put 'copy'", route_length + 3)
    write("end", route_length + 2)
    write("end", 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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
osc_machete_rails-2.1.2 lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb
osc_machete_rails-2.1.0 lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb
osc_machete_rails-2.0.0 lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb
osc_machete_rails-1.3.1 lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb
osc_machete_rails-1.3.0 lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb
osc_machete_rails-1.2.2 lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb
osc_machete_rails-2.0.0.pre1 lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb
osc_machete_rails-1.2.1 lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb
osc_machete_rails-1.2.0 lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb