Class: Generators::Avo::ToolGenerator

Inherits:
BaseGenerator
  • Object
show all
Defined in:
lib/generators/avo/tool_generator.rb

Instance Method Summary collapse

Methods inherited from BaseGenerator

#initialize

Constructor Details

This class inherits a constructor from Generators::Avo::BaseGenerator

Instance Method Details

#handleObject



13
14
15
16
17
18
19
20
21
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generators/avo/tool_generator.rb', line 13

def handle
  # Sidebar items
  template "tool/sidebar_item.tt", "app/views/avo/sidebar/items/_#{file_name}.html.erb"

  # Add controller if it doesn't exist
  controller_path = "app/controllers/avo/tools_controller.rb"
  unless File.file?(Rails.root.join(controller_path))
    template "tool/controller.tt", controller_path
  end

  # Add controller method
  inject_into_class controller_path, "Avo::ToolsController" do
    <<-METHOD
  def #{file_name}
    @page_title = "#{human_name}"
    add_breadcrumb title: "#{human_name}"
  end
    METHOD
  end

  # Add view file
  template "tool/view.tt", "app/views/avo/tools/#{file_name}.html.erb"

  # Add the route inside the `mount_avo` block so it gets the `avo.#{file_name}_path` helper.
  # If `mount_avo` is a one-liner, turn it into a block. If it's already a block, inject into it.
  # If there's no `mount_avo` (or the line can't be safely rewritten), fall back to a
  # standalone Avo engine routes block.
  routes_path = "config/routes.rb"
  routes_content = File.read(Rails.root.join(routes_path))
  route_definition = "get \"#{file_name}\", to: \"tools##{file_name}\", as: :#{file_name}"
  mount_avo_line = routes_content.lines.find { |line| line.match?(/^[ \t]*mount_avo\b/) }
  mount_avo = mount_avo_line && analyze_routes_line(mount_avo_line)

  if mount_avo && mount_avo[:block]
    # `mount_avo do` is already a block → inject the route as its first line.
    indent = mount_avo_line[/^[ \t]*/] + "  "
    inject_into_file routes_path, after: /^[ \t]*mount_avo\b.*\n/ do
      "#{indent}#{route_definition}\n"
    end
  elsif mount_avo && mount_avo[:complete]
    # `mount_avo` one-liner → wrap it into a block around the new route.
    indent = mount_avo_line[/^[ \t]*/]
    inner = indent + "  "
    gsub_file routes_path, /^[ \t]*mount_avo\b.*$/ do |line|
      code, comment = analyze_routes_line(line).values_at(:code, :comment)
      trailing = comment.empty? ? "" : " #{comment}"
      "#{code.rstrip} do#{trailing}\n#{inner}#{route_definition}\n#{indent}end"
    end
  else
    # No `mount_avo`, or a line we can't safely rewrite (multi-line call, unterminated
    # string) → fall back to a standalone Avo engine routes block.
    append_to_file routes_path, <<~ROUTE

      if defined? ::Avo
        Avo::Engine.routes.draw do
          #{route_definition}
        end
      end
    ROUTE
  end

  # Restart the server so the new routes go into effect.
  Rails::Command.invoke "restart"
end