Sha256: d22f42cfabc53a8ab818649276647b6a1f46b33b8483a71b1fe7b4206b907bef

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'active_support/core_ext/module/aliasing'
require 'active_record/connection_adapters/postgresql_adapter'

# Patch the PostgreSQL adapter to recognize defaults on ENUM columns.

class ActiveRecord::ConnectionAdapters::PostgreSQLColumn
  def initialize(name, default, oid_type, sql_type = nil, null = true)
    @oid_type = oid_type
    if sql_type =~ /\[\]$/
      @array = true
      super(name, self.class.extract_value_from_default(default, sql_type), sql_type[0..sql_type.length - 3], null)
    else
      @array = false
      super(name, self.class.extract_value_from_default(default, sql_type), sql_type, null)
    end
  end

  def self.extract_value_from_default_with_enum(default, type=nil)
    return extract_value_from_default_without_enum(default) unless type
    case default
      when /\A'(.*)'::(?:#{Regexp.escape type})\z/
        $1
      else
        extract_value_from_default_without_enum default
    end
  end

  class << self
    alias_method_chain :extract_value_from_default, :enum
  end
end if ActiveRecord::ConnectionAdapters::PostgreSQLColumn.methods.include?(:extract_value_from_default)

class ActiveRecord::ConnectionAdapters::JdbcColumn
  def initialize(config, name, default, *args)
    call_discovered_column_callbacks(config)
    super(name,enum_default_value(default, args.first),*args)
    init_column(name, default, *args)
  end

  def enum_default_value(default, type)
    case default
      when /\A'(.*)'::(?:#{Regexp.escape type})\z/
        $1
      else
        default_value default
    end
  end
end if defined?(ActiveRecord::ConnectionAdapters::JdbcColumn)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enum_type-2.1.1 lib/enum_type/extensions.rb