Class: OscMacheteRails::ResourceRouteGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_workflow_routeObject

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


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/osc_machete_rails/resource_route/resource_route_generator.rb', line 20

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