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 @@