From a3f2780ab0176db8b7a74c3fafd85ff446a16f57 Mon Sep 17 00:00:00 2001 From: Caleb Denio Date: Fri, 20 Oct 2023 13:45:38 -0400 Subject: [PATCH 1/4] Add filters to admin hackathons page --- app/controllers/admin/hackathons_controller.rb | 10 +++++++++- app/views/admin/hackathons/_filters.html.erb | 15 +++++++++++++++ app/views/admin/hackathons/index.html.erb | 7 ++++++- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 app/views/admin/hackathons/_filters.html.erb diff --git a/app/controllers/admin/hackathons_controller.rb b/app/controllers/admin/hackathons_controller.rb index a7c7e7dd..d198f05f 100644 --- a/app/controllers/admin/hackathons_controller.rb +++ b/app/controllers/admin/hackathons_controller.rb @@ -2,7 +2,15 @@ class Admin::HackathonsController < Admin::BaseController before_action :set_hackathon, except: :index def index - @pagy, @hackathons = pagy(Hackathon.all.order(created_at: :desc)) + @hackathons = Hackathon.all.order(created_at: :desc) + + @search = params[:q] + @status = params[:status] + + @hackathons = @hackathons.where("name ILIKE ?", "%#{Hackathon.sanitize_sql_like(@search)}%") if @search.present? + @hackathons = @hackathons.where(status: @status) if @status.present? + + @pagy, @hackathons = pagy(@hackathons) end def show diff --git a/app/views/admin/hackathons/_filters.html.erb b/app/views/admin/hackathons/_filters.html.erb new file mode 100644 index 00000000..125be81d --- /dev/null +++ b/app/views/admin/hackathons/_filters.html.erb @@ -0,0 +1,15 @@ +
+ <%= form_with method: :get, class: "simple_form mb3" do |form| %> + <%= form.hidden_field :status, value: @status if @status.present? %> + +
+ <%= form.text_field :q, placeholder: "Search", value: @search, style: "flex: 1", class: "mr2" %> + +
+ <% end %> + + <%= link_to "All", params.permit(:q).merge(status: nil), class: "mr2", style: ("color: var(--muted); text-decoration: none" unless @status.nil?) %> + <% Hackathon.statuses.keys.each do |status| %> + <%= link_to status.humanize, params.permit(:q).merge(status:), class: "mr2", style: ("color: var(--muted); text-decoration: none" unless @status == status) %> + <% end %> +
diff --git a/app/views/admin/hackathons/index.html.erb b/app/views/admin/hackathons/index.html.erb index f4995f79..befcfad9 100644 --- a/app/views/admin/hackathons/index.html.erb +++ b/app/views/admin/hackathons/index.html.erb @@ -2,6 +2,11 @@ <% @nav_active_item = admin_hackathons_path %>

Hackathons

-<%= render partial: "snippet", collection: @hackathons, as: :hackathon %> +<%= render "filters" %> +<% if @hackathons.size > 0 %> + <%= render partial: "snippet", collection: @hackathons, as: :hackathon %> +<% else %> +

No hackathons to show.

+<% end %> <%== pagy_nav(@pagy) %> From eee2ee05713febe60d703c8003bef7e84b18355f Mon Sep 17 00:00:00 2001 From: Caleb Denio Date: Mon, 23 Oct 2023 17:29:39 -0400 Subject: [PATCH 2/4] Update app/views/admin/hackathons/index.html.erb Co-authored-by: Matt Almeida --- app/views/admin/hackathons/index.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/admin/hackathons/index.html.erb b/app/views/admin/hackathons/index.html.erb index befcfad9..1dfc3bdf 100644 --- a/app/views/admin/hackathons/index.html.erb +++ b/app/views/admin/hackathons/index.html.erb @@ -3,7 +3,7 @@

Hackathons

<%= render "filters" %> -<% if @hackathons.size > 0 %> +<% if @hackathons.any? %> <%= render partial: "snippet", collection: @hackathons, as: :hackathon %> <% else %>

No hackathons to show.

From 7382db82ca41fe830fedfaf493858c75f150a853 Mon Sep 17 00:00:00 2001 From: Caleb Denio Date: Mon, 23 Oct 2023 17:29:46 -0400 Subject: [PATCH 3/4] Update app/controllers/admin/hackathons_controller.rb Co-authored-by: Matt Almeida --- app/controllers/admin/hackathons_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/hackathons_controller.rb b/app/controllers/admin/hackathons_controller.rb index d198f05f..555ed31e 100644 --- a/app/controllers/admin/hackathons_controller.rb +++ b/app/controllers/admin/hackathons_controller.rb @@ -4,10 +4,10 @@ class Admin::HackathonsController < Admin::BaseController def index @hackathons = Hackathon.all.order(created_at: :desc) - @search = params[:q] + @query = params[:q] @status = params[:status] - @hackathons = @hackathons.where("name ILIKE ?", "%#{Hackathon.sanitize_sql_like(@search)}%") if @search.present? + @hackathons = @hackathons.where("name ILIKE ?", "%#{Hackathon.sanitize_sql_like(@query)}%") if @query.present? @hackathons = @hackathons.where(status: @status) if @status.present? @pagy, @hackathons = pagy(@hackathons) From d7fef9453683f2af5a40e1c905cee255c9063610 Mon Sep 17 00:00:00 2001 From: Matt Almeida Date: Mon, 23 Oct 2023 17:37:33 -0400 Subject: [PATCH 4/4] Update app/views/admin/hackathons/_filters.html.erb --- app/views/admin/hackathons/_filters.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/admin/hackathons/_filters.html.erb b/app/views/admin/hackathons/_filters.html.erb index 125be81d..47a6a60b 100644 --- a/app/views/admin/hackathons/_filters.html.erb +++ b/app/views/admin/hackathons/_filters.html.erb @@ -3,7 +3,7 @@ <%= form.hidden_field :status, value: @status if @status.present? %>
- <%= form.text_field :q, placeholder: "Search", value: @search, style: "flex: 1", class: "mr2" %> + <%= form.text_field :q, placeholder: "Search", value: @query, style: "flex: 1", class: "mr2" %>
<% end %>