From 5cfb7d4ae41e7ab55ac7d5584cff9773e24b8719 Mon Sep 17 00:00:00 2001 From: Md Mosharaf Hossan Date: Wed, 14 Jun 2023 19:34:33 +0700 Subject: [PATCH 1/2] [#16] Add search query --- app/controllers/search_stats_controller.rb | 8 +++++++- app/views/search_stats/index.html.erb | 9 ++++++++- config/locales/en.yml | 3 +++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/search_stats_controller.rb b/app/controllers/search_stats_controller.rb index f426438..8249186 100644 --- a/app/controllers/search_stats_controller.rb +++ b/app/controllers/search_stats_controller.rb @@ -3,7 +3,13 @@ class SearchStatsController < ApplicationController # GET /search_stats def index - @pagy, @search_stats = pagy(current_user.search_stats) + search_keyword = params[:search_keyword] + + if search_keyword.present? + @pagy, @search_stats = pagy(current_user.search_stats.where(keyword: search_keyword)) + else + @pagy, @search_stats = pagy(current_user.search_stats) + end end def show diff --git a/app/views/search_stats/index.html.erb b/app/views/search_stats/index.html.erb index d7ef5d2..ac805fb 100644 --- a/app/views/search_stats/index.html.erb +++ b/app/views/search_stats/index.html.erb @@ -1,6 +1,13 @@

<%= SearchStat.model_name.human %>

- + <%= form_tag(search_stats_path, method: :get, class: "mt-3 mb-3") do %> +
+ <%= text_field_tag(:search_keyword, params[:search_keyword], class: "form-control rounded me-3", placeholder: t('instructions.search_query')) %> +
+ <%= submit_tag(t('buttons.search'), class: "btn btn-primary") %> +
+
+ <% end %> <% if @search_stats.any? %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 3bf5b15..f53fa90 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -47,3 +47,6 @@ en: buttons: search_stat_details: Show Details raw_response: View Raw Response + search: Search + instructions: + search_query: Enter your keyword From 533a79b462593bb324c1ac3d24dfcbccbf68d62d Mon Sep 17 00:00:00 2001 From: Md Mosharaf Hossan Date: Mon, 19 Jun 2023 18:54:16 +0700 Subject: [PATCH 2/2] [#16] Use custom query class for keyword query --- app/controllers/search_stats_controller.rb | 19 +++++++++------ app/queries/search_stats_query.rb | 28 ++++++++++++++++++++++ app/views/search_stats/index.html.erb | 2 +- 3 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 app/queries/search_stats_query.rb diff --git a/app/controllers/search_stats_controller.rb b/app/controllers/search_stats_controller.rb index 8249186..51ee0ec 100644 --- a/app/controllers/search_stats_controller.rb +++ b/app/controllers/search_stats_controller.rb @@ -3,16 +3,21 @@ class SearchStatsController < ApplicationController # GET /search_stats def index - search_keyword = params[:search_keyword] - - if search_keyword.present? - @pagy, @search_stats = pagy(current_user.search_stats.where(keyword: search_keyword)) - else - @pagy, @search_stats = pagy(current_user.search_stats) - end + search_stats_query.call + @pagy, @search_stats = pagy(search_stats_query.keywords) end def show @search_stat = SearchStat.includes(:result_links).find(params[:id]) end + + private + + def search_stats_query + @search_stats_query ||= SearchStatsQuery.new(current_user.search_stats, permitted_params) + end + + def permitted_params + params.permit(:keyword) + end end diff --git a/app/queries/search_stats_query.rb b/app/queries/search_stats_query.rb new file mode 100644 index 0000000..63d934b --- /dev/null +++ b/app/queries/search_stats_query.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class SearchStatsQuery + attr_reader :keywords + + def initialize(keywords, filters) + @keywords = keywords + @filters = filters + end + + def call + @keywords = filtered_keywords if filter_by_keyword.present? + @keywords = keywords.order('created_at DESC') + end + + private + + attr_reader :filters + + def filter_by_keyword + filters[:keyword] + end + + def filtered_keywords + query = "%#{filter_by_keyword}%" + keywords.where('keyword ILIKE ?', query) + end +end diff --git a/app/views/search_stats/index.html.erb b/app/views/search_stats/index.html.erb index ac805fb..d0c1b2f 100644 --- a/app/views/search_stats/index.html.erb +++ b/app/views/search_stats/index.html.erb @@ -2,7 +2,7 @@

<%= SearchStat.model_name.human %>

<%= form_tag(search_stats_path, method: :get, class: "mt-3 mb-3") do %>
- <%= text_field_tag(:search_keyword, params[:search_keyword], class: "form-control rounded me-3", placeholder: t('instructions.search_query')) %> + <%= text_field_tag(:keyword, params[:keyword], class: "form-control rounded me-3", placeholder: t('instructions.search_query')) %>
<%= submit_tag(t('buttons.search'), class: "btn btn-primary") %>