-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
59 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<div> | ||
<%= tag.name %> | ||
<%= highlight tag.name, params.dig(:query) %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |