diff --git a/app/controllers/search_stats_controller.rb b/app/controllers/search_stats_controller.rb index f426438..51ee0ec 100644 --- a/app/controllers/search_stats_controller.rb +++ b/app/controllers/search_stats_controller.rb @@ -3,10 +3,21 @@ class SearchStatsController < ApplicationController # GET /search_stats def index - @pagy, @search_stats = pagy(current_user.search_stats) + 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 d7ef5d2..d0c1b2f 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(:keyword, params[: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