ref: master
plugins/pg_search/lib/pg_search_plugin/search_scope.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
require 'active_support/concern' module PgSearchPlugin::SearchScope extend ActiveSupport::Concern PRIORITIES = {0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'} included do include PgSearch pg_search_scope :pg_search_plugin_search, -> query { { query: query, against: searchable_fields, using: {tsearch: {prefix: true}} } } # Use count scope :pg_search_plugin_attribute_facets, -> scope, attribute { where(id: scope.map(&:id)). group(attribute.to_sym). order('count_id DESC') } end class_methods do def searchable_fields if defined?(self::SEARCHABLE_FIELDS) i = 0 self::SEARCHABLE_FIELDS.sort_by {|key,value| value[:weight]}.map {|item| item[0]}.reverse.inject({}) do |result, key| result[key] = PRIORITIES[i] || 'D' i += 1 result end else [] end end end end |