Sha256: 5eb2a0862bb30b83b73336d60f4ec0cc2553f7a5d99992208f76b1c1949f6ba9

Contents?: true

Size: 958 Bytes

Versions: 4

Compression:

Stored size: 958 Bytes

Contents

module EnumGeneratorHelpers
  # Helper methods to figure out the migration number.
  module MigrationNumber

    # Returns the number of the last migration.
    def current_migration_number
      dirname = "#{Rails.root}/db/migrate/[0-9]*_*.rb"
      Dir.glob(dirname).collect do |file|
        File.basename(file).split("_").first.to_i
      end.max.to_i
    end

    # Returns the next upcoming migration number.  Sadly, Rails has no API for
    # this, so we're reduced to copying from ActiveRecord::Generators::Migration
    def next_migration_number
      # Lifted directly from ActiveRecord::Generators::Migration
      # Unfortunately, no API is provided by Rails at this time.
      next_migration_number = current_migration_number + 1
      if ActiveRecord::Base.timestamped_migrations
        [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
      else
        "%.3d" % next_migration_number
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
power_enum-1.3.1 lib/generators/enum/enum_generator_helpers/migration_number.rb
power_enum-1.3.0 lib/generators/enum/enum_generator_helpers/migration_number.rb
power_enum-1.2.0 lib/generators/enum/enum_generator_helpers/migration_number.rb
power_enum-1.1.2 lib/generators/enum/enum_generator_helpers/migration_number.rb