Class: Upgrow::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Helper
Defined in:
lib/generators/upgrow/install/install_generator.rb

Instance Method Summary collapse

Methods included from Helper

#class_name, included, #module_namespacing, #move_file, #namespace_content

Instance Method Details

#create_app_filesObject



49
50
51
# File 'lib/generators/upgrow/install/install_generator.rb', line 49

def create_app_files
  directory('app')
end

#move_existing_modelsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/upgrow/install/install_generator.rb', line 10

def move_existing_models
  pattern = File.expand_path('app/models/**/*.rb', destination_root)
  files = Dir.glob(pattern)

  return if files.empty?

  unless yes?('Would you like to convert all models into Records?', :blue)
    say(
      'Please convert your existing models into Records manually. You '\
      'may move them to the app/records folder, add the Record suffix '\
      'to their class names, and include Upgrow::Record.',
      :yellow
    )
    return
  end

  files.each do |original|
    target = relative_to_original_destination_root(original)
      .sub(%r{\Aapp/models/}, 'app/records/')
      .sub(/^(.+(?<!_record))\.rb\z/, '\1_record.rb')

    move_file(original, target)

    inject_into_file(
      target, 'Record',
      after: /class (?!\w+Record\s)\w+/,
      force: true,
      verbose: false
    )

    inject_into_class(
      target,
      'ApplicationRecord',
      "  include Upgrow::Record\n",
      verbose: false
    )
  end
end