Sha256: 192df2423911153b4f66f51d20d9dac5138c981efd36262205d79c9788110d05

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

module ActiveRecordPgStuff
  module Relation

    module TemporaryTable

      class Decorator
        attr_reader :table_name, :arel_table, :quoted_table_name, :table_metadata, :predicate_builder

        def initialize(object, table_name)
          @table_name        = table_name
          @object            = object
          @arel_table        = Arel::Table.new(table_name)
          @quoted_table_name = @object.connection.quote_table_name(table_name)
          @table_metadata    = ActiveRecord::TableMetadata.new(self, @arel_table)
          @predicate_builder = ActiveRecord::PredicateBuilder.new(@table_metadata)
        end

        def method_missing(name, *args, &block)
          @object.send(name, *args, &block)
        end

        def respond_to?(name, *args)
          @object.respond_to?(name, *args)
        end
      end

      def temporary_table
        tname = "temporary_#{self.table.name}_#{self.object_id}"
        self.klass.connection.with_temporary_table tname, self.to_sql do |name|
          dec     = Decorator.new self.klass, name
          rel     = ActiveRecord::Relation.new dec, table: dec.arel_table
          rel.readonly!
          yield rel
        end
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
activerecord_pg_stuff-0.2.0 lib/activerecord_pg_stuff/relation/temporary_table.rb
envoy-activerecord_pg_stuff-0.0.3 lib/activerecord_pg_stuff/relation/temporary_table.rb
envoy_activerecord_pg_stuff-0.0.2 lib/activerecord_pg_stuff/relation/temporary_table.rb