Sha256: f71ae37d4e0790a2d515e68969494b7f882c17092eaaff578d099ca3cc8b8df0

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

module StaticRecord
  # Provides helper methods that can be used in migrations
  module Schema
    COLUMNS = {
      static_record_type: :string
    }.freeze

    def self.included(_base)
      ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, Statements
      ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder
    end

    module Statements # :nodoc:
      def bind_static_record(table_name, *record_tables)
        raise ArgumentError, 'Please specify StaticRecord table in your bind_static_record call.' if record_tables.empty?

        options = record_tables.extract_options!
        record_tables.each do |record_table|
          COLUMNS.each_pair do |column_name, column_type|
            column_options = options.merge(options[column_name.to_sym] || {})
            add_column(table_name, "#{record_table}_#{column_name}", column_type, column_options)
          end
        end
      end

      def remove_static_record(table_name, *record_tables)
        raise ArgumentError, 'Please specify StaticRecord table in your remove_static_record call.' if record_tables.empty?

        record_tables.each do |record_table|
          COLUMNS.keys.each do |column_name|
            remove_column(table_name, "#{record_table}_#{column_name}")
          end
        end
      end
    end

    module CommandRecorder # :nodoc:
      def bind_static_record(*args)
        record(:bind_static_record, args)
      end

      private

      def invert_bind_static_record(args)
        [:remove_static_record, args]
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
static-record-1.2.1 lib/static_record/migrations/schema.rb
static-record-1.2.0 lib/static_record/migrations/schema.rb
static-record-1.1.0 lib/static_record/migrations/schema.rb
static-record-1.0.1 lib/static_record/migrations/schema.rb
static-record-1.0.0 lib/static_record/migrations/schema.rb
static-record-1.0.0.pre.4 lib/static_record/migrations/schema.rb