class RecipeSearch::PostsForBranchService

Public Class Methods

new(params) click to toggle source
# File lib/recipe_search.rb, line 4
def initialize(params)
  @search = params[:search]
  @category = params[:category]
  @branch = params[:branch]
end

Public Instance Methods

call() click to toggle source
this is the code for searching with different types using if else conditions

this is service design pattern to put code at one place so that the code is easy to test.

get posts depending on the request
# File lib/recipe_search.rb, line 13
def call
  if @category.blank? && @search.blank?
    posts = Post.by_branch(@branch).all
  elsif @category.blank? && @search.present?
    posts = Post.by_branch(@branch).search(@search)
  elsif @category.present? && @search.blank?
    posts = Post.by_category(@branch, @category)
  elsif @category.present? && @search.present?
    posts = Post.by_category(@branch, @category).search(@search)
  else
  end
end