Sha256: 135bd350a88026beb88625fbceeebbf2805ad27bf4f6c7ed8a0bb49ec87a65e9

Contents?: true

Size: 1.53 KB

Versions: 26

Compression:

Stored size: 1.53 KB

Contents

require "rails/generators"
require "fileutils"

module Generators
  module Avo
    class EjectGenerator < ::Rails::Generators::Base
      argument :filename, type: :string, required: true

      source_root ::Avo::Engine.root

      namespace "avo:eject"

      TEMPLATES = {
        logo: "app/views/avo/partials/_logo.html.erb",
        head: "app/views/avo/partials/_head.html.erb",
        header: "app/views/avo/partials/_header.html.erb",
        footer: "app/views/avo/partials/_footer.html.erb",
        scripts: "app/views/avo/partials/_scripts.html.erb",
        sidebar_extra: "app/views/avo/partials/_sidebar_extra.html.erb",
      }

      def handle
        if @filename.starts_with?(":")
          template_id = path_to_sym @filename
          template_path = TEMPLATES[template_id]

          if path_exists? template_path
            eject template_path
          else
            say("Failed to find the `#{template_id.to_sym}` template.", :yellow)
          end
        elsif path_exists? @filename
          eject @filename
        else
          say("Failed to find the `#{@filename}` template.", :yellow)
        end
      end

      no_tasks do
        def path_to_sym(filename)
          template_id = filename.dup
          template_id[0] = ""
          template_id.to_sym
        end

        def path_exists?(path)
          path.present? && File.file?(::Avo::Engine.root.join(path))
        end

        def eject(path)
          copy_file ::Avo::Engine.root.join(path), ::Rails.root.join(path)
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
avo-2.10.2 lib/generators/avo/eject_generator.rb
avo-2.9.2.pre1 lib/generators/avo/eject_generator.rb
avo-2.10.0 lib/generators/avo/eject_generator.rb
avo-2.9.1.pre7 lib/generators/avo/eject_generator.rb
avo-2.9.1.pre6 lib/generators/avo/eject_generator.rb
avo-2.9.1.pre5 lib/generators/avo/eject_generator.rb
avo-2.9.1.pre4 lib/generators/avo/eject_generator.rb
avo-2.9.1.pre3 lib/generators/avo/eject_generator.rb
avo-2.9.1.pre2 lib/generators/avo/eject_generator.rb
avo-2.9.1.pre1 lib/generators/avo/eject_generator.rb
avo-2.9.0 lib/generators/avo/eject_generator.rb
avo-2.8.0 lib/generators/avo/eject_generator.rb
avo-2.7.1.pre.1 lib/generators/avo/eject_generator.rb
avo-2.7.0 lib/generators/avo/eject_generator.rb
avo-2.6.1.pre.2 lib/generators/avo/eject_generator.rb
avo-2.6.1.pre.1 lib/generators/avo/eject_generator.rb
avo-2.6.0 lib/generators/avo/eject_generator.rb
avo-2.5.2.pre.7 lib/generators/avo/eject_generator.rb
avo-2.5.2.pre.6 lib/generators/avo/eject_generator.rb
avo-2.5.2.pre.5 lib/generators/avo/eject_generator.rb