Class: Effective::Generators::RouteGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Helpers
Defined in:
lib/generators/effective/route_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_route ⇒ Object



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
51
# File 'lib/generators/effective/route_generator.rb', line 22

def create_route
  blocks = []

  Effective::CodeWriter.new('config/routes.rb') do |w|
    resource.namespaces.each do |namespace|
      index = nil

      w.within(blocks.last) do
        index = w.first { |line, depth| depth == 1 && line == "namespace :#{namespace} do"}
      end

      index ? (blocks << index) : break
    end

    content = resource.namespaces[blocks.length..-1].map { |namespace| "namespace :#{namespace} do"} +
      [resources].flatten + (['end'] * (resource.namespaces.length - blocks.length))

    w.within(blocks.last) do
      if content.length == 1 && w.find { |line, depth| depth == 1 && line == content.first }
        say_status :identical, content.first, :blue
      else
        w.insert_after_last(content, content_depth: blocks.length) { |line, depth| depth == 1 && line.start_with?('resources') } ||
        w.insert_before_last(content, content_depth: blocks.length) { |line, depth| depth == 1 && line.start_with?('root') } ||
        w.insert_before_last(content, content_depth: blocks.length) { |line, depth| line == 'end' }

        say_status :route, content.join("\n\t\t")
      end
    end
  end
end

#invoke_route ⇒ Object



18
19
20
# File 'lib/generators/effective/route_generator.rb', line 18

def invoke_route
  say_status :invoke, :route, :white
end