Module: Warped::Controllers::Searchable

Extended by:
ActiveSupport::Concern
Included in:
Ui, Tabulatable
Defined in:
lib/warped/controllers/searchable.rb,
lib/warped/controllers/searchable/ui.rb

Overview

Provides functionality for searching records from an ActiveRecord::Relation in a controller.

Example usage:

class User < ApplicationRecord
  scope :search, ->(term) { where('name ILIKE ?', "%#{term}%") }
end

class UsersController < ApplicationController
  include Searchable

  def index
    scope = search(User.all)
    render json: scope
  end
end

Example requests:

GET /users?q=John
GET /users?q=John%20Doe

There are cases where the search scope is not called search. In such cases, you can use the searchable_by method to override the search scope.

Example:

class User < ApplicationRecord
  scope :search_case_sensitive, ->(term) { where('name LIKE ?', "%#{term}%") }
end

class UsersController < ApplicationController
  include Searchable

  searchable_by :search_case_sensitive

  def index
    scope = search(User.all)
    render json: scope
  end
end

When only overriding the search scope for a single action, you can pass the scope name as an argument to the search method. Example:

class UsersController < ApplicationController
  include Searchable

  def index
    # The default search scope name is overridden.
    # runs User.all.search_case_sensitive(search_term)
    scope = search(User.all, model_search_scope: :search_case_sensitive)
    render json: scope
  end

  def other_index
    # The default search scope name is used.
    # runs User.all.search(search_term)
    scope = search(User.all)
    render json: scope
  end
end

In addition, you can override the search term parameter name for the entire controller by implementing the search_param method.

Example:

class UsersController < ApplicationController
  include Searchable

  def index
    scope = search(User.all)
    render json: scope
  end

  private

  def search_param
    :term
  end
end

Or you can override the search term parameter name for a single action by passing the parameter name as an argument to the search_term method.

Example:

class UsersController < ApplicationController
  include Searchable

  def index
    # The default search term parameter name (+q+) is overridden.
    # GET /users?term=John
    scope = search(User.all, search_term: params[:term])
    render json: scope
  end

  def other_index
    # The default search term parameter name (+q+) is used.
    # GET /other_users?q=John
    scope = search(User.all)
    render json: scope
  end
end

Example requests:

GET /users?term=John
GET /other_users?q=John%20Doe

Defined Under Namespace

Modules: Ui

Instance Method Summary collapse

Instance Method Details

#model_search_scopeSymbol

Returns the name of the model’s search scope.

Returns:

  • (Symbol)

    The name of the model’s search scope.



159
160
161
# File 'lib/warped/controllers/searchable.rb', line 159

def model_search_scope
  self.class.model_search_scope
end

#search(scope, search_term: self.search_term, model_search_scope: self.model_search_scope) ⇒ ActiveRecord::Relation

Searches records based on the provided scope and search term.

Parameters:

  • scope (ActiveRecord::Relation)

    The scope to search within.

  • search_term (String) (defaults to: self.search_term)

    The search term.

  • model_search_scope (Symbol) (defaults to: self.model_search_scope)

    The name of the search scope.

Returns:

  • (ActiveRecord::Relation)

    The result of the search.



138
139
140
# File 'lib/warped/controllers/searchable.rb', line 138

def search(scope, search_term: self.search_term, model_search_scope: self.model_search_scope)
  Queries::Search.call(scope, search_term:, model_search_scope:)
end

#search_paramSymbol

Returns the name of the parameter used for searching.

Returns:

  • (Symbol)

    The search parameter name.



152
153
154
# File 'lib/warped/controllers/searchable.rb', line 152

def search_param
  self.class.search_param
end

#search_termString?

Retrieves the search term from the request parameters.

Returns:

  • (String, nil)

    The search term if present, otherwise nil.



145
146
147
# File 'lib/warped/controllers/searchable.rb', line 145

def search_term
  params[search_param]&.strip
end