Sha256: 6e1c148ec406251f101e1dfbb7d9857d7c99fbe7a5ef456ad9cd6b23ccc69151

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

require "active_record"
require "active_support/lazy_load_hooks"

module ActiveRecord
  module PGCollation
    KNOWN_VERSIONS = %w[6.1].map { |v| Gem::Version.new(v) }

    class << self
      attr_reader :enabled_version

      def install(version)
        @enabled_version = approximate_version(version)

        # Don't immediately fail if we don't yet support the current version.
        # There's at least a chance it could work.
        if !KNOWN_VERSIONS.include?(enabled_version) && enabled_version > KNOWN_VERSIONS.last
          @enabled_version = KNOWN_VERSIONS.last
          warn("[PGCollation] Current ActiveRecord version unsupported! Falling back to: #{enabled_version}")
        end

        initialize!
      end

      def register(patch, &block)
        monkeypatches[patch] = block
      end

      def detected_version
        approximate_version(Gem.loaded_specs["activerecord"].version)
      end

      private

      def approximate_version(version)
        segments = version.respond_to?(:canonical_segments) ? version.canonical_segments.dup : version.segments

        segments.pop while segments.any? {|s| String === s}
        segments.pop while segments.size > 2
        segments.push(0) while segments.size < 2

        Gem::Version.new(segments.join("."))
      end

      def monkeypatches
        @patches ||= {}
      end

      def initialize!
        require "active_record/pg_collation/command_recorder"
        require "active_record/pg_collation/postgresql_adapter"
        require "active_record/pg_collation/schema_statements"

        Dir[File.join(__dir__, "pg_collation", enabled_version.to_s, "*.rb")].each {|file| require file}
        monkeypatches.keys.each {|patch| monkeypatches.delete(patch).call}
      end
    end
  end
end

ActiveSupport.on_load(:active_record) do
  ActiveRecord::PGCollation.install(Gem.loaded_specs["activerecord"].version)
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-pg_collation-0.1.2 lib/active_record/pg_collation.rb
activerecord-pg_collation-0.1.1 lib/active_record/pg_collation.rb
activerecord-pg_collation-0.1.0 lib/active_record/pg_collation.rb