Last active
December 24, 2015 20:38
-
-
Save nglx/6858852 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
include User::SearchScope | |
def index | |
search_params = params[:user_search] || {} | |
@users = apply_scopes(User, search_params) | |
end | |
end | |
class User < ActiveRecord::Base | |
# placed here only for convenience | |
module SearchScope | |
def self.included(base) | |
base.has_scope :name | |
base.has_scope :rating, default: 4 # setting default value | |
base.has_scope :skills | |
end | |
end | |
scope :name, ->(name) { where('name like ?', "%#{name}%") } | |
scope :rating, ->(rating) { where('rating > ?', rating) } | |
# parsing values in the scope | |
scope :skills, ->(skills) { tagged_with(skills.split(','), any: true) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment