Skip to content

Commit

Permalink
Feature search function (#753)
Browse files Browse the repository at this point in the history
* adding search button function in the index page to query serach terms against the Site Name,Fits_id and Subnet CIDR

* updated site controller to return sites in the order of the fits id

---------

Co-authored-by: Jahir <[email protected]>
  • Loading branch information
juddin927 and juddin927 authored Nov 24, 2023
1 parent 45a5b99 commit 0a10c7c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ class SitesController < ApplicationController
def index
@sites = Site.order(:fits_id).all
@navigation_crumbs = [["Home", root_path]]
@sites = if params[:query].present?
Site.where('name LIKE ? OR fits_id LIKE ? OR id IN (
SELECT sn.site_id
FROM subnets s
INNER JOIN shared_networks sn ON s.shared_network_id = sn.id
WHERE s.cidr_block LIKE ?
)', "%#{params[:query]}%", "%#{params[:query]}%", "%#{params[:query]}%")
else
Site.order(:fits_id).all
end
end

def show
Expand Down
8 changes: 7 additions & 1 deletion app/views/sites/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
<%= link_to "Client classes", client_classes_path, class: "govuk-button govuk-button--secondary" %>

<h3 class="govuk-heading-m">Sites</h3>

<!-- Adding the search form -->
<div style="text-align: right;class="govuk-grid-column-one-half search_bar">
<%= form_tag(dhcp_path, method: :get, class: "govuk-form") do %>
<%= text_field_tag :query, params[:query], placeholder: "Search sites by FITS ID or name...", class: 'govuk-input govuk-input--width-10' %>
<%= submit_tag "Search", class: "govuk-button", "data-module" => "govuk-button" %>
<% end %>
</div>
<% if can? :create, Site %>
<%= link_to "Create a new site", new_site_path, class: "govuk-button" %>
<% end %>
Expand Down

0 comments on commit 0a10c7c

Please sign in to comment.