From 2988f42457edcf2d1c3d1761e8ed3546761ec956 Mon Sep 17 00:00:00 2001 From: Lucian Ghinda Date: Thu, 7 Sep 2023 16:34:05 +0300 Subject: [PATCH] Refactor Search Term Display into a Reusable Component This commit refactors the display of the search term into a reusable component, SearchTermComponent. This component is now used across various views (authors, books, courses, newsletters, podcasts, screencasts, tags, youtubes) to display the search term, improving code reusability and maintainability. The search term is also properly escaped to prevent potential Cross-Site Scripting (XSS) attacks. System tests for course search functionality have been updated accordingly. --- app/views/authors/index.html.erb | 13 +++---------- app/views/books/_index_nav.html.erb | 6 +----- app/views/components/search_term_component.rb | 15 +++++++++++++++ app/views/courses/index.html.erb | 6 +----- app/views/newsletters/index.html.erb | 6 +----- app/views/podcasts/index.html.erb | 14 +++----------- app/views/screencasts/index.html.erb | 6 +----- app/views/tags/index.html.erb | 6 +----- app/views/youtubes/index.html.erb | 6 +----- .../previews/search_term_component_preview.rb | 11 +++++++++++ 10 files changed, 38 insertions(+), 51 deletions(-) create mode 100644 app/views/components/search_term_component.rb create mode 100644 test/components/previews/search_term_component_preview.rb diff --git a/app/views/authors/index.html.erb b/app/views/authors/index.html.erb index 9a1db437..33039f8b 100644 --- a/app/views/authors/index.html.erb +++ b/app/views/authors/index.html.erb @@ -1,24 +1,17 @@ <% content_for :hero do %> <%= render "shared/page_title", title: "People", description: "There are many people writing about Ruby, Ruby on Rails. Some of them are very experienced developers who have been using Ruby for years, and some are new developers who are just starting to learn the language. I've compiled a list of some of the best and brightest minds in the industry.", count: "#{Author.count} authors" %> <% end %> - <% content_for :content do %> - <% if params[:search_term].present? %> -
- Search Term: <%= params[:search_term] %> -
- <% end %> - + <%= render SearchTermComponent.new(search_term: params[:search_term]) %>
-

All

+

All

- <%= form_tag(authors_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])) %> + <%= text_field_tag(:search_term, nil, placeholder: 'Search name', class: 'form-control', value: html_escape(params[:search_term])) %> diff --git a/app/views/books/_index_nav.html.erb b/app/views/books/_index_nav.html.erb index bbc299c4..39870050 100644 --- a/app/views/books/_index_nav.html.erb +++ b/app/views/books/_index_nav.html.erb @@ -1,8 +1,4 @@ -<% if params[:search_term].present? %> -
- Search Term: <%= params[:search_term] %> -
-<% end %> +<%= render SearchTermComponent.new(search_term: params[:search_term]) %>
diff --git a/app/views/components/search_term_component.rb b/app/views/components/search_term_component.rb new file mode 100644 index 00000000..8b0ad82a --- /dev/null +++ b/app/views/components/search_term_component.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class SearchTermComponent < ApplicationComponent + def initialize(search_term: nil) + @search_term = ERB::Util.html_escape(search_term) + end + + def template + return unless @search_term.present? + + div(class: "pb-12") do + strong { "Search Term: #{@search_term}" } + end + end +end diff --git a/app/views/courses/index.html.erb b/app/views/courses/index.html.erb index 4eaf59cb..36150d6f 100644 --- a/app/views/courses/index.html.erb +++ b/app/views/courses/index.html.erb @@ -2,11 +2,7 @@ <%= 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 %> + <%= render SearchTermComponent.new(search_term: params[:search_term]) %>

All Courses

diff --git a/app/views/newsletters/index.html.erb b/app/views/newsletters/index.html.erb index 1f1ce3b0..4ff82f19 100644 --- a/app/views/newsletters/index.html.erb +++ b/app/views/newsletters/index.html.erb @@ -2,11 +2,7 @@ <%= 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 %> + <%= render SearchTermComponent.new(search_term: params[:search_term]) %>

All Newsletters

diff --git a/app/views/podcasts/index.html.erb b/app/views/podcasts/index.html.erb index 4c1a404e..bd7ce257 100644 --- a/app/views/podcasts/index.html.erb +++ b/app/views/podcasts/index.html.erb @@ -1,24 +1,17 @@ <% content_for :hero do %> <%= render "shared/page_title", title: "Podcasts about Ruby and Ruby on Rails", description: "If you're looking for some interesting podcasts to listen to on your commute or while you're working out, check out our list of the best Ruby, Ruby on Rails, and programming podcasts. From fun discussions about life as a programmer to in-depth interviews with Ruby on Rails experts, these podcasts have something for everyone.", count: "#{Podcast.count} podcasts" %> <% end %> - <% content_for :content do %> - <% if params[:search_term].present? %> -
- Search Term: <%= params[:search_term] %> -
- <% end %> - + <%= render SearchTermComponent.new(search_term: params[:search_term]) %>
-

All Podcasts

+

All Podcasts

- <%= form_tag(podcasts_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])) %> + <%= text_field_tag(:search_term, nil, placeholder: 'Search name', class: 'form-control', value: html_escape(params[:search_term])) %> @@ -28,6 +21,5 @@ <% end %>
- <%= render partial: "podcasts/list", locals: { list: @podcasts } %> <% end %> diff --git a/app/views/screencasts/index.html.erb b/app/views/screencasts/index.html.erb index 54707fec..f146cfc1 100644 --- a/app/views/screencasts/index.html.erb +++ b/app/views/screencasts/index.html.erb @@ -2,11 +2,7 @@ <%= 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 %> + <%= render SearchTermComponent.new(search_term: params[:search_term]) %>

All Screencasts

diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index 2502a821..18a493ff 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -2,11 +2,7 @@ <%= 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 %> + <%= render SearchTermComponent.new(search_term: params[:search_term]) %>

All Topics

diff --git a/app/views/youtubes/index.html.erb b/app/views/youtubes/index.html.erb index d6e683d0..5b07113a 100644 --- a/app/views/youtubes/index.html.erb +++ b/app/views/youtubes/index.html.erb @@ -2,11 +2,7 @@ <%= 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 %> + <%= render SearchTermComponent.new(search_term: params[:search_term]) %>

All YouTube Courses

diff --git a/test/components/previews/search_term_component_preview.rb b/test/components/previews/search_term_component_preview.rb new file mode 100644 index 00000000..9d945d48 --- /dev/null +++ b/test/components/previews/search_term_component_preview.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class SearchTermComponentPreview < Lookbook::Preview + def default + render SearchTermComponent.new(search_term: "Ruby") + end + + def when_search_term_is_nil_or_empty + render SearchTermComponent.new() + end +end