Sha256: 929585c8c51c4f709b3d736089d02cd0ee171bdf8f386214e70c85c0379a524b

Contents?: true

Size: 1011 Bytes

Versions: 2

Compression:

Stored size: 1011 Bytes

Contents

# frozen_string_literal: true

module PGTrunk
  # @private
  # The module adds custom type casting
  module CustomTypes
    # All custom types are typecasted to strings in Rails
    TYPE = ActiveRecord::ConnectionAdapters::PostgreSQL::OID::SpecializedString

    def self.known
      @known ||= Set.new([])
    end

    def enable_pg_trunk_types
      execute(<<~SQL).each { |item| enable_pg_trunk_type item["name"] }
        SELECT (
          CASE
          WHEN t.typnamespace = 'public'::regnamespace THEN t.typname
          ELSE t.typnamespace::regnamespace || '.' || t.typname
          END
        ) AS name
        FROM pg_trunk e JOIN pg_type t ON t.oid = e.oid
        WHERE e.classid = 'pg_type'::regclass
      SQL
    end

    def enable_pg_trunk_type(type)
      type = type.to_s
      CustomTypes.known << type
      type_map.register_type(type, TYPE.new(type)) unless type_map.key?(type)
    end

    def valid_type?(type)
      CustomTypes.known.include?(type.to_s) || super
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pg_trunk-0.1.1 lib/pg_trunk/core/railtie/custom_types.rb
pg_trunk-0.1.0 lib/pg_trunk/core/railtie/custom_types.rb