Class: Refinery::Generators::EngineInstaller

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/refinery/generators/engine_installer.rb

Overview

The core engine installer streamlines the installation of custom generated engines. It takes the migrations and seeds in your engine and moves them into the rails app db directory, ready to migrate.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#silence_putsObject

Returns the value of attribute silence_puts.



15
16
17
# File 'lib/refinery/generators/engine_installer.rb', line 15

def silence_puts
  @silence_puts
end

Class Method Details

.engine_name(name = nil) ⇒ Object



22
23
24
25
# File 'lib/refinery/generators/engine_installer.rb', line 22

def engine_name(name = nil)
  @engine_name = name.to_s unless name.nil?
  @engine_name
end

.next_migration_number(dirname) ⇒ Object



35
36
37
# File 'lib/refinery/generators/engine_installer.rb', line 35

def next_migration_number(dirname)
  ::ActiveRecord::Generators::Base.next_migration_number(dirname)
end

.source_root(root = nil) ⇒ Object



27
28
29
# File 'lib/refinery/generators/engine_installer.rb', line 27

def source_root(root = nil)
  Pathname.new(super.to_s)
end

Instance Method Details

#generateObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/refinery/generators/engine_installer.rb', line 40

def generate
  Pathname.glob(self.class.source_root.join('db', '**', '*.rb')).sort.each do |path|
    case path.to_s
    when %r{.*/migrate/.*}
      # unless the migration has already been generated.
      migration_name = "#{path.split.last.to_s.split(/^\d+_/).last}"
      unless Dir[Rails.root.join('db', 'migrate', "*#{migration_name}")].any?
        migration_template path, Rails.root.join('db', 'migrate', migration_name)
      else
        puts "You already have a migration called #{migration_name.split('.rb').first}" unless self.silence_puts || self.behavior == :revoke
      end
    when %r{.*/seeds/.*}
      template path, Rails.root.join('db', 'seeds', path.to_s.split('/seeds/').last)
    end
  end

  if !self.silence_puts && self.behavior != :revoke
    puts "------------------------"
    puts "Now run:"
    puts "rake db:migrate"
    puts "------------------------"
  elsif self.behavior == :revoke
    ::Refinery::Generators::Migrations.revoke({
      :pattern => self.class.source_root.join('db', 'migrate', '*.rb')
    })
  end
end