Sha256: ed747f63949a91ad2c7d0660c05ed9d1f4be6552a163855f7e7d4701174204b4

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'rails/generators/base'
require 'rails/generators/active_record'

module Reji
  module Generators
    class InstallGenerator < Rails::Generators::Base
      include Rails::Generators::Migration

      source_root(File.expand_path('templates', __dir__))

      def create_reji_initializer
        copy_file('reji.rb', 'config/initializers/reji.rb')
      end

      def create_reji_migration
        copy_migration('add_reji_to_users')
        copy_migration('create_subscriptions')
        copy_migration('create_subscription_items')
      end

      # for generating a timestamp when using `create_migration`
      def self.next_migration_number(dir)
        ActiveRecord::Generators::Base.next_migration_number(dir)
      end

      private def copy_migration(migration_name, config = {})
        return if migration_exists?(migration_name)

        migration_template(
          "db/migrate/#{migration_name}.rb.erb",
          "db/migrate/#{migration_name}.rb",
          config.merge(migration_version: migration_version)
        )
      end

      private def migration_exists?(name)
        existing_migrations.include?(name)
      end

      private def existing_migrations
        @existing_migrations ||= Dir.glob('db/migrate/*.rb').map do |file|
          migration_name_without_timestamp(file)
        end
      end

      private def migration_name_without_timestamp(file)
        file.sub(%r{^.*(db/migrate/)(?:\d+_)?}, '')
      end

      private def migration_version
        "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
      end

      private def migration_primary_key_type_string
        ", id: :#{configured_key_type}" if configured_key_type
      end

      private def configured_key_type
        Rails.configuration.generators.active_record[:primary_key_type]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reji-1.1.1 lib/generators/reji/install/install_generator.rb
reji-1.1.0 lib/generators/reji/install/install_generator.rb