Sha256: cb8be4ba50a74b865e60865e5a4d8cb22d76440d41d73bd9be1d6cbe6bcc855f

Contents?: true

Size: 1.87 KB

Versions: 4

Compression:

Stored size: 1.87 KB

Contents

module Torque
  module PostgreSQL
    module Adapter
      module ColumnMethods

        # Creates a column with an interval type, allowing span of times and
        # dates to be stored without having to store a seconds-based integer
        # or any sort of other approach
        def interval(*args, **options)
          args.each { |name| column(name, :interval, options) }
        end

        # Creates a column with an enum type, needing to specify the subtype,
        # which is basically the name of the type defined prior creating the
        # column
        def enum(*args, **options)
          args.each do |name|
            type = options.fetch(:subtype, name)
            column(name, type, options)
          end
        end

      end

      module TableDefinition
        include ColumnMethods

        attr_reader :inherits

        def initialize(name, *_, **options)
          old_args = []
          old_args << options.delete(:temporary) || false
          old_args << options.delete(:options)
          old_args << options.delete(:as)
          comment = options.delete(:comment)

          super(name, *old_args, comment: comment)

          if options.key?(:inherits)
            @inherits = Array[options.delete(:inherits)].flatten.compact
            @inherited_id = !(options.key?(:primary_key) || options.key?(:id))
          end
        end

        def inherited_id?
          @inherited_id
        end
      end

      ActiveRecord::ConnectionAdapters::PostgreSQL::Table.include ColumnMethods
      ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition.include TableDefinition

      if ActiveRecord::ConnectionAdapters::PostgreSQL.const_defined?('ColumnDefinition')
        module ColumnDefinition
          attr_accessor :subtype
        end

        ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDefinition.include ColumnDefinition
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
torque-postgresql-0.2.16 lib/torque/postgresql/adapter/schema_definitions.rb
torque-postgresql-0.2.15 lib/torque/postgresql/adapter/schema_definitions.rb
torque-postgresql-0.2.14 lib/torque/postgresql/adapter/schema_definitions.rb
torque-postgresql-0.2.13 lib/torque/postgresql/adapter/schema_definitions.rb