summaryrefslogtreecommitdiff
path: root/app/models/concerns/optionally_search.rb
blob: 4093429e372a4b8e89e1b5c95205dd265607718a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# frozen_string_literal: true

module OptionallySearch
  extend ActiveSupport::Concern

  class_methods do
    def search(*)
      raise(
        NotImplementedError,
        'Your model must implement the "search" class method'
      )
    end

    # Optionally limits a result set to those matching the given search query.
    def optionally_search(query = nil)
      query.present? ? search(query) : all
    end
  end
end