Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/controllers/search_stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions app/queries/search_stats_query.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion app/views/search_stats/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<div class="container mt-4">
<h4><%= SearchStat.model_name.human %></h4>

<%= form_tag(search_stats_path, method: :get, class: "mt-3 mb-3") do %>
<div class="input-group">
<%= text_field_tag(:keyword, params[:keyword], class: "form-control rounded me-3", placeholder: t('instructions.search_query')) %>
<div class="input-group-append">
<%= submit_tag(t('buttons.search'), class: "btn btn-primary") %>
</div>
</div>
<% end %>
<% if @search_stats.any? %>
<div id="search_stats" class="table-responsive">
<table class="table table-bordered table-striped">
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ en:
buttons:
search_stat_details: Show Details
raw_response: View Raw Response
search: Search
instructions:
search_query: Enter your keyword