Skip to content

Commit

Permalink
global search
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Nov 5, 2023
1 parent cd51c21 commit 6c25c09
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 37 deletions.
32 changes: 29 additions & 3 deletions app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
class DashboardController < ApplicationController
SEARCHABLE_MODEL_ATTRIBUTES = {
"Post" => ["title", "description"],
"Tag" => ["name"],
"User" => ["first_name", "last_name", "email"]
# "User" => ["first_name", "last_name"]
}

def index
@posts = Post.all
@tags = Tag.all
@users = User.all
# @posts = Post.all
# @tags = Tag.all
# @users = User.all

# if params[:query].present?
# @tags = Tag.where("name ILIKE ?", "%#{params[:query]}%")
# @posts = Post.where("title ILIKE ? OR description ILIKE ?", "%#{params[:query]}%", "%#{params[:query]}%")
# @users = User.where("first_name ILIKE ? OR last_name ILIKE ? OR email ILIKE ?", "%#{params[:query]}%", "%#{params[:query]}%", "%#{params[:query]}%")
# else
# @posts = []
# @tags = []
# @users = []
# end

@search_results = {}

if params[:query].present?
SEARCHABLE_MODEL_ATTRIBUTES.each do |model_name, searchable_fields|
model_results = model_name.constantize.where(searchable_fields.map { |field| "#{field} ILIKE :query" }.join(" OR "), query: "%#{params[:query]}%")
@search_results[model_name] = model_results
end
end
end
end
52 changes: 24 additions & 28 deletions app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
<div>
<h2>
Posts
<%= @posts.count %>
</h2>
<% @posts.each do |post| %>
<%= render 'posts/post', post: %>
<% end %>
</div>
<%= link_to 'Reset search', dashboard_path if params[:query].present? %>
<%= form_with url: dashboard_path, method: :get do |form| %>
<%= form.search_field :query, value: params[:query], autofocus: true, placeholder: "Search anything" %>
<%= form.submit "Search" %>
<% end %>
<div>
<h2>
Tags
<%= @tags.count %>
</h2>
<% @tags.each do |tag| %>
<%= render 'tags/tag', tag: %>
<% end %>
</div>

<div>
<h2>
Users
<%= @users.count %>
</h2>
<% @users.each do |user| %>
<%= render 'users/user', user: %>
<% end %>
</div>
<%#= @search_results %>
<% DashboardController::SEARCHABLE_MODEL_ATTRIBUTES.each do |model_name, searchable_fields| %>
<%#= model_name %>
<%#= searchable_fields %>
<% results = @search_results[model_name] %>
<div>
<h2>
<%= model_name %>
<%= results.count %>
</h2>
<% results.each do |result| %>
<%# searchable_fields.each do |field| %>
<%#= highlight result[field], params[:query] %>
<%# end %>
<%#= result.id %>
<%= render "#{model_name.downcase.pluralize}/#{model_name.downcase}", model_name.downcase.to_sym => result %>
<% end %>
</div>
<% end %>
4 changes: 2 additions & 2 deletions app/views/posts/_post.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<%= post.title %>
<%= highlight post.title, params.dig(:query) %>
<br>
<%= post.description %>
<%= highlight post.description, params.dig(:query) %>
</div>
2 changes: 1 addition & 1 deletion app/views/tags/_tag.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div>
<%= tag.name %>
<%= highlight tag.name, params.dig(:query) %>
</div>
6 changes: 3 additions & 3 deletions app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<%= user.first_name %>
<%= user.last_name %>
<%= user.email %>
<%= highlight user.first_name, params.dig(:query) %>
<%= highlight user.last_name, params.dig(:query) %>
<%= highlight user.email, params.dig(:query) %>
<%= user.social_security_number %>
</div>

0 comments on commit 6c25c09

Please sign in to comment.