Sha256: b73a5a7d49a417737adf02f07022f0d3e505d4f24051d9464ab2938d74a47c9a

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require "rails/generators"

module Gutentag
  module Generators
    class MigrationVersionsGenerator < Rails::Generators::Base
      desc "Update the ActiveRecord version in Gutentag migrations"

      def update_migration_versions
        superclass = "ActiveRecord::Migration[#{rails_version}]"

        if ::ActiveRecord::VERSION::MAJOR < 5
          superclass = "ActiveRecord::Migration"
        end

        migration_files.each do |file|
          gsub_file file,
            /< ActiveRecord::Migration\[4\.2\]$/,
            "< #{superclass}"
        end
      end

      private

      def migration_files
        Dir[Rails.root.join("db/migrate/*.rb")].select do |path|
          known_migration_names.any? do |known|
            File.basename(path)[/\A\d+_#{known}\.gutentag.rb\z/]
          end
        end
      end

      def known_migration_names
        @known_migration_names ||= begin
          Dir[File.join(__dir__, "../../../db/migrate/*.rb")].collect do |path|
            File.basename(path).gsub(/\A\d+_/, "").gsub(/\.rb\z/, "")
          end
        end
      end

      def rails_version
        @rails_version ||= [
          ::ActiveRecord::VERSION::MAJOR,
          ::ActiveRecord::VERSION::MINOR
        ].join(".")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gutentag-2.6.2 lib/gutentag/generators/migration_versions_generator.rb
gutentag-2.6.1 lib/gutentag/generators/migration_versions_generator.rb