Sha256: 40fb9d39aed1e4fe5e053c52bb578c4c2362db49bb794de28cc3e3667df93ea3

Contents?: true

Size: 731 Bytes

Versions: 9

Compression:

Stored size: 731 Bytes

Contents

module Arel
  module Transformer
    class ReplaceTableWithSubquery
      attr_reader :subquery_for_table

      def initialize(subquery_for_table)
        @subquery_for_table = subquery_for_table
      end

      def call(arel, next_middleware)
        tree = Arel.enhance(arel)
        update_arel_tables(tree)
        next_middleware.call tree
      end

      private

      def update_arel_tables(tree)
        tree.query(
          class: Arel::Table,
          context: { range_variable: true },
          schema_name: nil,
        ).each do |node|
          if (subquery = subquery_for_table.call(node.name.value))
            node.replace subquery.as(node.name.value)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
arel_toolkit-0.4.9 lib/arel/transformer/replace_table_with_subquery.rb
arel_toolkit-0.4.8 lib/arel/transformer/replace_table_with_subquery.rb
arel_toolkit-0.4.7 lib/arel/transformer/replace_table_with_subquery.rb
arel_toolkit-0.4.6 lib/arel/transformer/replace_table_with_subquery.rb
arel_toolkit-0.4.5 lib/arel/transformer/replace_table_with_subquery.rb
arel_toolkit-0.4.4 lib/arel/transformer/replace_table_with_subquery.rb
arel_toolkit-0.4.3 lib/arel/transformer/replace_table_with_subquery.rb
arel_toolkit-0.4.2 lib/arel/transformer/replace_table_with_subquery.rb
arel_toolkit-0.4.1 lib/arel/transformer/replace_table_with_subquery.rb