Sha256: 8aa777db12835f4b2f5edc22982c07ee58c16b050cf2b8e00d4e5d26f7f19e7b
Contents?: true
Size: 1.3 KB
Versions: 1
Compression:
Stored size: 1.3 KB
Contents
# frozen_string_literal: true class PostSearch include SearchObject.module(:model, :sorting, :will_paginate, :enum) 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 :term, with: :apply_term option :rating, enum: %i[low high] 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 :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 apply_term(scope, value) scope.where 'title LIKE :term OR body LIKE :term', term: escape_search_term(value) end def apply_rating_with_low(scope) scope.where 'views_count < 100' end def apply_rating_with_high(scope) scope.where 'views_count > 500' end def parse_date(value) Date.parse(value).strftime('%Y-%m-%d') rescue ArgumentError nil end def escape_search_term(term) "%#{term.gsub(/\s+/, '%')}%" end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
search_object-1.2.3 | example/app/models/post_search.rb |