Sha256: 05c3701b076446c80c2ff058abb578d4bde0b462cc3c9676d73c431cf0dc6758

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require "digest"

module PgSearch
  class Configuration
    class Association
      attr_reader :columns

      def initialize(model, name, column_names)
        @model = model
        @name = name
        @columns = Array(column_names).map do |column_name, weight|
          Column.new(column_name, weight, @model, self)
        end
      end

      def table_name
        @model.reflect_on_association(@name).table_name
      end

      def join(primary_key)
        selects = columns.map do |column|
          case @model.connection.send(:postgresql_version)
          when 0..90000
            "array_to_string(array_agg(#{column.full_name}), ' ') AS #{column.alias}"
          else
            "string_agg(#{column.full_name}::text, ' ') AS #{column.alias}"
          end
        end.join(", ")
        relation = @model.joins(@name).select("#{primary_key} AS id, #{selects}").group(primary_key)
        "LEFT OUTER JOIN (#{relation.to_sql}) #{subselect_alias} ON #{subselect_alias}.id = #{primary_key}"
      end

      def subselect_alias
        Configuration.alias(table_name, @name, "subselect")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pg_search-0.5.1 lib/pg_search/configuration/association.rb
pg_search-0.5 lib/pg_search/configuration/association.rb
pg_search-0.4.2 lib/pg_search/configuration/association.rb