Sha256: ccf0eb34bf2586ba099f8ace636b4b651796ad4345ca95e70955f3b9019d082b

Contents?: true

Size: 1.86 KB

Versions: 41

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require "rails/generators/base"

module Rails
  module Generators
    module Db
      module System
        class ChangeGenerator < Base # :nodoc:
          include Database
          include AppName

          class_option :to, required: true,
            desc: "The database system to switch to."

          def self.default_generator_root
            path = File.expand_path(File.join(base_name, "app"), base_root)
            path if File.exist?(path)
          end

          def initialize(*)
            super

            unless DATABASES.include?(options[:to])
              raise Error, "Invalid value for --to option. Supported preconfigurations are: #{DATABASES.join(", ")}."
            end

            opt = options.dup
            opt[:database] ||= opt[:to]
            self.options = opt.freeze
          end

          def edit_database_config
            template("config/databases/#{options[:database]}.yml", "config/database.yml")
          end

          def edit_gemfile
            name, version = gem_for_database
            gsub_file("Gemfile", all_database_gems_regex, name)
            gsub_file("Gemfile", gem_entry_regex_for(name), gem_entry_for(name, *version))
          end

          private
            def all_database_gems
              DATABASES.map { |database| gem_for_database(database) }
            end

            def all_database_gems_regex
              all_database_gem_names = all_database_gems.map(&:first)
              /(\b#{all_database_gem_names.join('\b|\b')}\b)/
            end

            def gem_entry_regex_for(gem_name)
              /^gem.*\b#{gem_name}\b.*/
            end

            def gem_entry_for(*gem_name_and_version)
              gem_name_and_version.map! { |segment| "\"#{segment}\"" }
              "gem #{gem_name_and_version.join(", ")}"
            end
        end
      end
    end
  end
end

Version data entries

41 entries across 38 versions & 5 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/railties-7.0.8.7/lib/rails/generators/rails/db/system/change/change_generator.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/railties-7.0.8.6/lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.8.7 lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.8.6 lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.8.5 lib/rails/generators/rails/db/system/change/change_generator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/railties-7.0.8.4/lib/rails/generators/rails/db/system/change/change_generator.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/rails/db/system/change/change_generator.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/rails/db/system/change/change_generator.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.8.4 lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.8.1 lib/rails/generators/rails/db/system/change/change_generator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-7.0.2.3/lib/rails/generators/rails/db/system/change/change_generator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-7.0.3.1/lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.1.0.beta1 lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.8 lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.7.2 lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.7.1 lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.7 lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.6 lib/rails/generators/rails/db/system/change/change_generator.rb
railties-7.0.5.1 lib/rails/generators/rails/db/system/change/change_generator.rb