Skip to content

Instantly share code, notes, and snippets.

@domagude
Created November 5, 2017 14:27
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 domagude/4cc3223781fcf5ee0a2d40092a01b245 to your computer and use it in GitHub Desktop.
Save domagude/4cc3223781fcf5ee0a2d40092a01b245 to your computer and use it in GitHub Desktop.
class PostsForBranchService
def initialize(params)
@search = params[:search]
@category = params[:category]
@branch = params[:branch]
end
# get posts depending on the request
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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment