Class: DatashiftJourney::SetupGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/datashift_journey/setup/setup_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_engine_to_routesObject



23
24
25
26
27
28
29
30
31
# File 'lib/generators/datashift_journey/setup/setup_generator.rb', line 23

def add_engine_to_routes
  return if File.readlines(File.join(destination_root, "config/routes.rb")).grep(/mount DatashiftJourney::Engine =>/).present?

  route("mount DatashiftJourney::Engine => '/journey'")

  puts '*' * 50
  puts "We added the following line to your application's config/routes.rb file:\n"
  puts "  mount DatashiftJourney::Engine => '/journey'"
end

#create_filesObject



19
20
21
# File 'lib/generators/datashift_journey/setup/setup_generator.rb', line 19

def create_files
  template 'initializer.rb', 'config/initializers/datashift_journey.rb'
end

#generate_modelObject

This class automatically runs every method defined in it,



9
10
11
12
13
14
15
16
17
# File 'lib/generators/datashift_journey/setup/setup_generator.rb', line 9

def generate_model
  Rails::Generators.invoke "active_record:model", [name, 'state:string', 'reference:string:index:unique'], migration: options[:migration] unless model_exists? && behavior == :invoke

  plan = File.open(File.join(SetupGenerator.source_root, "model_concern.rb.tt")).read

  inject_into_file model_path, :after => /class.* < ApplicationRecord/ do
    "\n\tinclude DatashiftJourney::ReferenceGenerator.new(prefix: 'C')\n\tvalidates_presence_of :reference\n\tvalidates_uniqueness_of :reference\n\n#{plan}"
  end
end