Sha256: 9153d143442d69488402d6011458dd96c2c9e0aa71ab5880d78b9acdd7fc4af5
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
module PgSearch module Multisearch 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 class << self def rebuild(model) model.transaction do PgSearch::Document.where(:searchable_type => model.name).delete_all model.connection.execute(rebuild_sql(model)) end end def rebuild_sql(model) connection = model.connection unless model.respond_to?(:pg_search_multisearchable_options) raise ModelNotMultisearchable.new(model) end columns = Array.wrap( model.pg_search_multisearchable_options[:against] ) content_expressions = columns.map { |column| %Q{coalesce(:model_table.#{column}::text, '')} }.join(" || ' ' || ") REBUILD_SQL_TEMPLATE.gsub( ":content_expressions", content_expressions ).gsub( ":model_name", connection.quote(model.name) ).gsub( ":model_table", model.quoted_table_name ).gsub( ":documents_table", PgSearch::Document.quoted_table_name ).gsub( ":current_time", connection.quote(connection.quoted_date(Time.now)) ) end end class ModelNotMultisearchable < StandardError def initialize(model_class) @model_class = model_class end def message "#{@model_class.name} is not multisearchable. See PgSearch::ClassMethods#multisearchable" end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pg_search-0.5 | lib/pg_search/multisearch.rb |