Sha256: 1d292942f852f0b3609e1f2858892e64d9c94ac82d6ee8ce8287c8804209cb3f

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

module PgSearch
  module Multisearch
    class Rebuilder
      REBUILD_SQL_TEMPLATE = <<-SQL
INSERT INTO :documents_table (searchable_type, searchable_id, content, created_at, updated_at)
  SELECT :model_name AS searchable_type,
         :model_table.id AS searchable_id,
         (
           :content_expressions
         ) AS content,
         :current_time AS created_at,
         :current_time AS updated_at
  FROM :model_table
SQL

      def initialize(model)
        unless model.respond_to?(:pg_search_multisearchable_options)
          raise ModelNotMultisearchable.new(model)
        end

        @model = model
      end

      def rebuild
        if @model.respond_to?(:rebuild_pg_search_documents)
          @model.rebuild_pg_search_documents
        else
          @model.connection.execute(rebuild_sql)
        end
      end

      private

      def connection
        @model.connection
      end

      def rebuild_sql
        replacements.inject(REBUILD_SQL_TEMPLATE) do |sql, key|
          sql.gsub ":#{key}", send(key)
        end
      end

      def replacements
        %w[content_expressions model_name model_table documents_table current_time]
      end

      def content_expressions
        columns.map { |column|
          %Q{coalesce(:model_table.#{column}::text, '')}
        }.join(" || ' ' || ")
      end

      def columns
        Array.wrap(@model.pg_search_multisearchable_options[:against])
      end

      def model_name
        connection.quote(@model.name)
      end

      def model_table
        @model.quoted_table_name
      end

      def documents_table
        PgSearch::Document.quoted_table_name
      end

      def current_time
        connection.quote(connection.quoted_date(Time.now))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pg_search-0.5.4 lib/pg_search/multisearch/rebuilder.rb
pg_search-0.5.3 lib/pg_search/multisearch/rebuilder.rb
pg_search-0.5.2 lib/pg_search/multisearch/rebuilder.rb