Skip to content

Instantly share code, notes, and snippets.

@nglx
Last active December 24, 2015 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nglx/6858852 to your computer and use it in GitHub Desktop.
Save nglx/6858852 to your computer and use it in GitHub Desktop.
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