Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created September 25, 2019 08:35
Show Gist options
  • Select an option

  • Save krisleech/b8dd12fd72ffbea9405ae84575f18a68 to your computer and use it in GitHub Desktop.

Select an option

Save krisleech/b8dd12fd72ffbea9405ae84575f18a68 to your computer and use it in GitHub Desktop.
Mimicking ActiveRecord::Migration[5.2] class versions

In Rails migrations now need a version number:

class ActiveRecord::Migration[5.2]
  # ...
end

I haven't looked at the code in Rails for this, but one way it could be done would be as such:

module ActiveRecord
  class Migration
    def self.[](version)
      const_get(version.to_s.split('.').map { |num| 'V' + num }.join('::'))
    end
    
    module V5
      class V2
      end
    end
    
    module V4
      class V0
      end
    end
  end
end

ActiveRecord::Migration[5.2] # => ActiveRecord::Migration::V5::V2

class MyMigration < ActiveRecord::Migration[5.2]
  # ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment