Sha256: 22293d9bcfc4f3ff9acecd4d7024f51ff3149c9cb9d0c5a81c295352780c19bf
Contents?: true
Size: 1.03 KB
Versions: 3
Compression:
Stored size: 1.03 KB
Contents
class PostSearch include SearchObject.module(:model, :sorting, :will_paginate) scope { Post.all } sort_by :id, :created_at, :views_count, :likes_count, :comments_count per_page 15 min_per_page 10 max_per_page 100 option :user_id option :category_name option :title do |scope, value| scope.where 'title LIKE ?', escape_search_term(value) end option :published do |scope, value| scope.where published: true if value.present? end option :term do |scope, value| scope.where 'title LIKE :term OR body LIKE :term', term: escape_search_term(value) end option :created_after do |scope, value| date = parse_date value scope.where('DATE(created_at) >= ?', date) if date.present? end option :created_before do |scope, value| date = parse_date value scope.where('DATE(created_at) <= ?', date) if date.present? end private def parse_date(value) Date.parse(value).strftime('%Y-%m-%d') rescue nil end def escape_search_term(term) "%#{term.gsub(/\s+/, '%')}%" end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
search_object-1.1.3 | example/app/models/post_search.rb |
search_object-1.1.2 | example/app/models/post_search.rb |
search_object-1.1.1 | example/app/models/post_search.rb |