Sha256: 8abcdc765150b07b2e4a58193f53a790eb0cb35c89fbb5b546aca5aa5efbb8ff

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

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

module Generalis
  module Generators
    class MigrationsGenerator < Rails::Generators::Base
      include Rails::Generators::Migration

      source_root File.expand_path('templates', __dir__)

      def self.next_migration_number(dir)
        ::ActiveRecord::Generators::Base.next_migration_number(dir)
      end

      def create_migration_files
        migration_template 'create_ledger_accounts.rb.erb', 'db/migrate/create_ledger_accounts.rb'
        migration_template 'create_ledger_transactions.rb.erb', 'db/migrate/create_ledger_transactions.rb'
        migration_template 'create_ledger_entries.rb.erb', 'db/migrate/create_ledger_entries.rb'
        migration_template 'create_ledger_links.rb.erb', 'db/migrate/create_ledger_links.rb'
      end

      def json_column_type
        case ActiveRecord::Base.connection.adapter_name
        when 'SQLite'     then ':string'
        when 'MySQL'      then ':json'
        when 'PostgreSQL' then ':jsonb'
        else
          Rails.logger.warn('Unsupported database adapter; using String for JSON data types')

          ':string'
        end
      end

    private

      # @param name [Symbol, String]
      # @return [String]
      def prefixed_table_name(name)
        ":#{Generalis.table_name_prefix}#{name}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
generalis-0.1.0 lib/generators/generalis/migrations_generator.rb