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
# 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 "#{human_name}"
  end
    METHOD
  end

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

  # Add the route in the `routes.rb` file.
  # The route should be defined inside the Avo engine.
  # The new tool has a dedicated path helper.
  # EX:
  #   bin/rails generate avo:tool lolo
  #   will generate the avo.lolo_path helper
  # THe fact that it will always generate the definded? and Avo::Engine.routes.draw wraps is unfortunate. We'd love a PR to fix that.
  route_contents = <<~ROUTE
    
    if defined? ::Avo
      Avo::Engine.routes.draw do
        # This route is not protected, secure it with authentication if needed.
        get "#{file_name}", to: "tools##{file_name}", as: :#{file_name}
      end
    end
  ROUTE
  append_to_file "config/routes.rb", route_contents

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