Sha256: 10f0d871a160aaa52e0512fa21ceb22064e8fd026bc4a92ccb1462a0e34f7289

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

module Rambulance
  module Generators
    class InstallGenerator < Rails::Generators::Base
      source_root File.expand_path('../templates', __FILE__)

      class_option :template_engine, type: :string, aliases: '-e', desc: 'Template engine for the views. Available options are "erb" and "haml".'

      def self.banner #:nodoc:
        <<-BANNER.chomp
rails g rambulance:install

    Copies all error partial templates and an initializer to your application.
BANNER
      end

      desc ''
      def copy_templates #:nodoc:
        say "generating templates:"
        filename_pattern = File.join(self.class.source_root, "views", "*.html.#{template_engine}")
        Dir.glob(filename_pattern).map {|f| File.basename f }.each do |f|
          copy_file "views/#{f}", "app/views/errors/#{f}"
        end

        filename_pattern = File.join(self.class.source_root, "views", "*.json.jbuilder")
        Dir.glob(filename_pattern).map {|f| File.basename f }.each do |f|
          copy_file "views/#{f}", "app/views/errors/#{f}"
        end
      end

      def copy_layout #:nodoc:
        say "\ncopying app/views/layouts/application.html.#{template_engine} to app/views/layouts/error.html.#{template_engine}:"
        copy_file Rails.root.join("app/views/layouts/application.html.#{template_engine}"), "app/views/layouts/error.html.#{template_engine}"
      end

      def copy_initializer #:nodoc:
        say "\ngenerating initializer:"
        copy_file "rambulance.rb", "config/initializers/rambulance.rb"
      end

      private

      def template_engine
        options[:template_engine].try(:to_s).try(:downcase) || 'erb'
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rambulance-0.1.2 lib/generators/rambulance/install_generator.rb
rambulance-0.1.1 lib/generators/rambulance/install_generator.rb