Sha256: 6dca130004397f739280000abadce9f1cfde6e6abdf0545135ee58f292906383

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 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
          if activerecord52?
            rel = ActiveRecord::Relation.new dec
          else
            rel = ActiveRecord::Relation.new dec, dec.arel_table, dec.predicate_builder, {}
          end
          rel.readonly!
          yield rel
        end
      end

      def activerecord52?
        ActiveRecord.gem_version >= Gem::Version.new("5.2.x")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
activerecord_pg_stuff-0.2.1 lib/activerecord_pg_stuff/relation/temporary_table.rb
envoy-activerecord_pg_stuff-0.3.0 lib/activerecord_pg_stuff/relation/temporary_table.rb