Skip to content

Instantly share code, notes, and snippets.

@kopylovvlad
Last active June 12, 2018 17:27
Show Gist options
  • Save kopylovvlad/446a47e3c8833b9596e7b1c4b72e0681 to your computer and use it in GitHub Desktop.
Save kopylovvlad/446a47e3c8833b9596e7b1c4b72e0681 to your computer and use it in GitHub Desktop.
# model
class Post
include Neo4j::ActiveNode
include Neo4j::Timestamps
scope :published, -> { where(published: true) }
def self.searching(search_params)
result = Post.published
# relations
if search_params[:post_type].present?
result = result.branch { post_type.where(id: search_params[:post_type]) }
end
if search_params[:post_class].present?
result = result.branch { post_class.where(id: search_params[:post_class]) }
end
if search_params[:post_subject].present?
result = result.branch { post_subject.where(id: search_params[:post_subject]) }
end
# bollean fields
if search_params[:answered].present?
result = result.where(answered: search_params[:answered].to_bool)
end
if search_params[:answerable].present?
result = result.branch{ post_type.where(answerable: search_params[:answerable].to_bool) }
end
# text fields
if search_params[:query].present?
result = result.where("LOWER(n.title) =~ ? OR LOWER(n.body) =~ ?", ".*#{search_params[:query]}.*".downcase)
end
result
end
end
# controller
module Api
class PostsController < ApplicationController
def index
@posts = Post.searching(search_params).page(@page).per(@per)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment