Skip to content

Instantly share code, notes, and snippets.

@ScorpiusZ
Created January 22, 2016 04:44
Show Gist options
  • Save ScorpiusZ/f4cac4ebfdbaea4540b6 to your computer and use it in GitHub Desktop.
Save ScorpiusZ/f4cac4ebfdbaea4540b6 to your computer and use it in GitHub Desktop.

Revisions

  1. ScorpiusZ renamed this gist Jan 22, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. ScorpiusZ created this gist Jan 22, 2016.
    25 changes: 25 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    module Searchable
    extend ActiveSupport::Concern

    included do
    scope :pg_search, ->(keyword){ where(search_query keyword) }
    include ClassMethods
    end

    module ClassMethods
    def search_against params
    @search_against ||= params if params.is_a? Array
    end

    def search_query keyword
    @search_against.map{|x| x.to_s + "~* '#{keyword}'"}.join(' or ')
    end
    end
    end


    class Topic < ActiveRecord::Base
    include Searchable
    search_against [:title,:description,:body]
    end