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

    def install
      add_route
      add_secrets
      add_initializer
      add_javascripts
      add_stylesheets
      rake 'calagator:install:migrations'
      rake 'db:migrate'
      rake 'db:test:prepare'
      run 'rm -f public/index.html'
    end

    private

    def add_route
      inject_into_file 'config/routes.rb', "\s\smount Calagator::Engine => '/'\n", after: "Application.routes.draw do\n"
    end

    def add_secrets
      copy_file File.expand_path(File.join(__FILE__, '../templates/config/secrets.yml.sample')), 'config/secrets.yml'
    end

    def add_initializer
      copy_file File.expand_path(File.join(__FILE__, '../templates/config/calagator.rb')), 'config/initializers/calagator.rb'
    end

    def add_javascripts
      append_file 'app/assets/javascripts/application.js', '//= require calagator'
    end

    def add_stylesheets
      append_file 'app/assets/stylesheets/application.css', '//= require calagator'
    end
  end
end

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

    class_option :dummy, type: :boolean, default: false

    def install
      add_route
      add_secrets
      add_initializer
      add_javascripts
      add_stylesheets
      run 'rm -f public/index.html'
      unless options[:dummy]
        rake 'calagator:install:migrations'
        rake 'db:migrate'
        rake 'db:test:prepare'
      end
    end

    private

    def add_route
      inject_into_file 'config/routes.rb', "\s\smount Calagator::Engine => '/'\n", after: "Application.routes.draw do\n"
    end

    def add_secrets
      copy_file File.expand_path(File.join(__FILE__, '../templates/config/secrets.yml.sample')), 'config/secrets.yml'
    end

    def add_initializer
      initializer 'calagator.rb', File.read(File.expand_path('../templates/config/calagator.rb', __FILE__))
    end

    def add_javascripts
      append_file 'app/assets/javascripts/application.js', '//= require calagator'
    end

    def add_stylesheets
      append_file 'app/assets/stylesheets/application.css', '//= require calagator'
    end
  end
end

Add a --dummy option to calagator:install generator
Add a --dummy option to calagator:install generator

The dummy app in spec/dummy shouldn't copy migrations from db/migrate,
since this will cause migration name exceptions when using the rake
tasks provided by the engine-level Rakefile.
