From d8aa8a55517dcbb096808abc6087f99f77781262 Mon Sep 17 00:00:00 2001 From: Lucian Ghinda Date: Thu, 7 Sep 2023 06:23:53 +0300 Subject: [PATCH] Add seach component to all other pages (#105) This will replace the HTML search from all pages with the SearchComponent The SearchComponent encapsulates the search form logic, making the code more readable and easier to change. It also takes care of HTML escaping the search_term, improving security. --- app/views/courses/index.html.erb | 29 ++++++---------------------- app/views/newsletters/index.html.erb | 26 ++++++------------------- app/views/screencasts/index.html.erb | 27 ++++++-------------------- app/views/tags/index.html.erb | 26 ++++--------------------- app/views/youtubes/index.html.erb | 27 ++++++-------------------- 5 files changed, 28 insertions(+), 107 deletions(-) diff --git a/app/views/courses/index.html.erb b/app/views/courses/index.html.erb index 3256cf21..4eaf59cb 100644 --- a/app/views/courses/index.html.erb +++ b/app/views/courses/index.html.erb @@ -1,39 +1,22 @@ <% content_for :hero do %> <%= render "shared/page_title", title: "Courses about Ruby and Ruby on Rails", description: "Our collection of Ruby and Ruby on Rails courses cover everything from the basics to advanced topics like metaprogramming and web scraping. Courses will show you how to build powerful, production-ready web applications with this popular framework. Best of all, you can find courses for free. Ready to get started?", count: "#{Course.count} courses" %> <% end %> - <% content_for :content do %> <% if params[:search_term].present? %>
Search Term: <%= params[:search_term] %>
<% end %> -
- -

All Courses

-
- - - <%= form_tag(courses_path, method: "get", id: 'search_form', remote: true) do %> -
- - <%= text_field_tag(:search_term, nil, placeholder: 'Search name', class: 'form-control', value: html_escape(params[:search_term])) %> - - - - <%= link_to courses_path, type: :submit, class: "btn btn-warning", id: "clear-search-btn", title: "Reset", style: "margin-left: 10px; color: black" do %> X <% end %> - -
- <% end %> -
+
+

All Courses

+
+
+ <%= render(SearchComponent.new(path: courses_path, search_term: params[:search_term])) %> +
-
<%= render partial: "courses/list", locals: { list: @courses } %>
<%== pagy_nav(@pagy) %> <% end %> - - - diff --git a/app/views/newsletters/index.html.erb b/app/views/newsletters/index.html.erb index ad41adac..1f1ce3b0 100644 --- a/app/views/newsletters/index.html.erb +++ b/app/views/newsletters/index.html.erb @@ -1,33 +1,19 @@ <% content_for :hero do %> <%= render "shared/page_title", title: "Newsletters about Ruby and Ruby on Rails", description: "Get the latest news, tips, and tricks delivered right to your inbox. Read about interesting articles and blog posts about life and programming.", count: "#{Newsletter.count} newsletters" %> <% end %> - <% content_for :content do %> <% if params[:search_term].present? %>
Search Term: <%= params[:search_term] %>
<% end %> -
- -

All Newsletters

-
- - - <%= form_tag(newsletters_path, method: "get", id: 'search_form', remote: true) do %> -
- - <%= text_field_tag(:search_term, nil, placeholder: 'Search name', class: 'form-control', value: html_escape(params[:search_term])) %> - - - - <%= link_to newsletters_path, type: :submit, class: "btn btn-warning", id: "clear-search-btn", title: "Reset", style: "margin-left: 10px; color: black" do %> X <% end %> - -
- <% end %> -
+
+

All Newsletters

+
+
+ <%= render SearchComponent.new(path: newsletters_path, search_term: params[:search_term]) %> +
- <%= render partial: "newsletters/list", locals: { list: @newsletters } %> <% end %> diff --git a/app/views/screencasts/index.html.erb b/app/views/screencasts/index.html.erb index 292de2fd..54707fec 100644 --- a/app/views/screencasts/index.html.erb +++ b/app/views/screencasts/index.html.erb @@ -1,34 +1,19 @@ <% content_for :hero do %> <%= render "shared/page_title", title: "Screencasts about Ruby and Ruby on Rails", description: "If you're looking for some helpful resources on learning Ruby and Ruby on Rails development, I recommend checking out some screencast tutorials. There's a lot of great information out there that can help you get started and become a better developer. Happy learning!", count: "#{Screencast.count} courses" %> <% end %> - <% content_for :content do %> <% if params[:search_term].present? %>
Search Term: <%= params[:search_term] %>
<% end %> -
- -

All Screencasts

-
- - - <%= form_tag(screencasts_path, method: "get", id: 'search_form', remote: true) do %> -
- - <%= text_field_tag(:search_term, nil, placeholder: 'Search name', class: 'form-control', value: html_escape(params[:search_term])) %> - - - - <%= link_to screencasts_path, type: :submit, class: "btn btn-warning", id: "clear-search-btn", title: "Reset", style: "margin-left: 10px; color: black" do %> X <% end %> - -
- <% end %> -
+
+

All Screencasts

+
+
+ <%= render SearchComponent.new(path: screencasts_path, search_term: params[:search_term]) %> +
- <%= render partial: "screencasts/list", locals: { list: @screencasts } %> <% end %> - diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index ce16c215..2502a821 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -1,37 +1,19 @@ <% content_for :hero do %> <%= render "shared/page_title", title: "What would you like to learn more about? ", description: "If you're looking to learn more about programming, we've got a few resources that might interest you. Whatever your interest, we're sure we can find a book or course that's perfect for you.", count: "#{Tag.count} topics" %> <% end %> - <% content_for :content do %> <% if params[:search_term].present? %>
Search Term: <%= params[:search_term] %>
<% end %> -
-

All Topics

-
- - - <%= form_tag(tags_path, method: "get", id: 'search_form', remote: true) do %> -
- - <%= text_field_tag(:search_term, nil, placeholder: 'Search name', class: 'form-control', value: html_escape(params[:search_term])) %> - - - - <%= link_to tags_path, type: :submit, class: "btn btn-warning", id: "clear-search-btn", title: "Reset", style: "margin-left: 10px; color: black" do %> X <% end %> - -
- <% end %> +

All Topics

+
+ <%= render(SearchComponent.new(path: tags_path, search_term: params[:search_term])) %> +
- <%= render partial: "tags/list", locals: { list: @tags } %> <% end %> - - - - diff --git a/app/views/youtubes/index.html.erb b/app/views/youtubes/index.html.erb index 927cb95b..d6e683d0 100644 --- a/app/views/youtubes/index.html.erb +++ b/app/views/youtubes/index.html.erb @@ -1,34 +1,19 @@ <% content_for :hero do %> <%= render "shared/page_title", title: "YouTube Courses about Ruby and Ruby on Rails", description: "If you're interested in learning Ruby or Ruby on Rails, or if you want to brush up on your testing skills or learn more about other themes, you're in luck. There are plenty of free and paid books available on these topics. Whether you're a beginner or an expert, you're sure to find something that interests you.", count: "#{Youtube.count} YouTube Courses" %> <% end %> - <% content_for :content do %> <% if params[:search_term].present? %>
Search Term: <%= params[:search_term] %>
<% end %> -
- -

All YouTube Courses

-
- - - <%= form_tag(youtubes_path, method: "get", id: 'search_form', remote: true) do %> -
- - <%= text_field_tag(:search_term, nil, placeholder: 'Search name', class: 'form-control', value: html_escape(params[:search_term])) %> - - - - <%= link_to youtubes_path, type: :submit, class: "btn btn-warning", id: "clear-search-btn", title: "Reset", style: "margin-left: 10px; color: black" do %> X <% end %> - -
- <% end %> -
+
+

All YouTube Courses

+
+
+ <%= render SearchComponent.new(path: youtubes_path, search_term: params[:search_term]) %> +
- <%= render "youtubes/list" %> <% end %> -