Sha256: a3f7db54cde4ded914ef46d44f3fc6fdf60041bd0e441ab27a2b947128c89be6

Contents?: true

Size: 1.47 KB

Versions: 33

Compression:

Stored size: 1.47 KB

Contents

require "rails/generators"
require "fileutils"

module Generators
  module Avo
    class ToolGenerator < ::Rails::Generators::Base
      argument :name, type: :string, required: true

      source_root File.expand_path("templates", __dir__)

      namespace "avo:tool"

      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"

        route <<-ROUTE
scope :#{::Avo.configuration.namespace} do
  get "#{file_name}", to: "avo/tools##{file_name}"
end
        ROUTE
      end

      no_tasks do
        def file_name
          name.to_s.underscore
        end

        def controller_name
          file_name.to_s
        end

        def human_name
          file_name.humanize
        end

        def in_code(text)
          "<code class='p-1 rounded bg-gray-500 text-white text-sm'>#{text}</code>"
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
avo-1.6.0 lib/generators/avo/tool_generator.rb
avo-1.5.5 lib/generators/avo/tool_generator.rb
avo-1.5.4 lib/generators/avo/tool_generator.rb
avo-1.5.3 lib/generators/avo/tool_generator.rb
avo-1.5.2 lib/generators/avo/tool_generator.rb
avo-1.5.1 lib/generators/avo/tool_generator.rb
avo-1.5.0 lib/generators/avo/tool_generator.rb
avo-1.4.5.pre.1 lib/generators/avo/tool_generator.rb
avo-1.4.4.pre.1 lib/generators/avo/tool_generator.rb
avo-1.4.4 lib/generators/avo/tool_generator.rb
avo-1.4.3 lib/generators/avo/tool_generator.rb
avo-1.4.2 lib/generators/avo/tool_generator.rb
avo-1.4.1 lib/generators/avo/tool_generator.rb
avo-1.4.0 lib/generators/avo/tool_generator.rb
avo-1.4.0.pre.1 lib/generators/avo/tool_generator.rb
avo-1.3.5 lib/generators/avo/tool_generator.rb
avo-1.3.5.pre.1 lib/generators/avo/tool_generator.rb
avo-1.3.4 lib/generators/avo/tool_generator.rb
avo-1.3.3 lib/generators/avo/tool_generator.rb
avo-1.3.2 lib/generators/avo/tool_generator.rb