class RailsUpgrader::CLI

Attributes

domain[R]

Public Class Methods

call() click to toggle source
# File lib/rails_upgrader/cli.rb, line 9
def self.call
  new.upgrade
end
new() click to toggle source
# File lib/rails_upgrader/cli.rb, line 13
def initialize
  preload_environment
  puts "Preloading relationships..."
  @domain = RailsERD::Domain.generate
end

Public Instance Methods

upgrade() click to toggle source
# File lib/rails_upgrader/cli.rb, line 19
def upgrade
  puts "Upgrading Rails..."
  upgrade_strong_params!
  puts "Rails is upgraded!"
end

Private Instance Methods

preload_environment() click to toggle source
# File lib/rails_upgrader/cli.rb, line 58
def preload_environment
  begin
    require "#{Dir.pwd}/config/environment"
  rescue LoadError => e
    puts "Rails application not found! If you're on "\
         "a Rails application, please open a Github issue: "\
         "https://github.com/ombulabs/rails_upgrader/issues"
    abort
  end

  puts "Preloading environment..."
  Rails.application.eager_load!

  if Rails.application.respond_to?(:config) && Rails.application.config
    rails_config = Rails.application.config
    if rails_config.respond_to?(:eager_load_namespaces)
      rails_config.eager_load_namespaces.each(&:eager_load!)
    end
  end
end
upgrade_strong_params() click to toggle source
# File lib/rails_upgrader/cli.rb, line 27
def upgrade_strong_params
  result = domain.entities.map do |entity|
    RailsUpgrader::StrongParams.new(entity).generate_method if entity.model
  end.join

  File.open("all_strong_params.rb", "w") { |f| f.write(result) }
end
upgrade_strong_params!() click to toggle source
# File lib/rails_upgrader/cli.rb, line 35
def upgrade_strong_params!
  domain.entities.each do |entity|
    next unless entity.model
    entity_to_upgrade = RailsUpgrader::StrongParams.new(entity)

    unless File.file?(entity_to_upgrade.controller_path)
      puts "Skipping #{entity.name}"
      next
    end

    next if entity_to_upgrade.already_upgraded?

    begin
      entity_to_upgrade.update_controller_content!
      entity_to_upgrade.update_model_content!
    rescue => e
      puts e.message
      puts e.backtrace
      next
    end
  end
end