Sha256: 225d9154884be34f0c86252dd0eedfc7d19e5113f2c3b73dd3c9e58c3fd4c2ee

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 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|
          "string_agg(#{column.full_name}, ' ') AS #{column.alias}"
        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
        subselect_name = ["pg_search", table_name, @name, "subselect"].compact.join('_')
        "pg_search_#{Digest::SHA2.hexdigest(subselect_name)}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pg_search-0.2.2 lib/pg_search/configuration/association.rb
pg_search-0.2.1 lib/pg_search/configuration/association.rb
pg_search-0.2 lib/pg_search/configuration/association.rb