Sha256: b064f6d5236e983af3467abe8743b60698f91f6c2982ff2007054f9d12587d04

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require 'generators/upgrow'

module Upgrow
  module Generators
    class InstallGenerator < Rails::Generators::Base
      include Helper

      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

      def create_app_files
        directory('app')
      end

      hook_for :test_framework
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
upgrow-0.0.5 lib/generators/upgrow/install/install_generator.rb