Skip to content

Commit

Permalink
Refactor Search Term Display into a Reusable Component
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lucianghinda committed Sep 7, 2023
1 parent d8aa8a5 commit 2988f42
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 51 deletions.
13 changes: 3 additions & 10 deletions app/views/authors/index.html.erb
Original file line number Diff line number Diff line change
@@ -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? %>
<div class="pb-12">
<strong>Search Term: </strong><%= params[:search_term] %>
</div>
<% end %>

<%= render SearchTermComponent.new(search_term: params[:search_term]) %>
<div class="flex flex-wrap justify-between mb-8 xl:mb-16 gap-x-8 gap-y-2 md:gap-8">
<span>
<h2 class="h2 mb-8">All</h2>
<h2 class="mb-8 h2">All</h2>
</span>

<span>
<%= form_tag(authors_path, method: "get", id: 'search_form', remote: true) do %>
<div class="input-group">
<span class="input-group-append">
<%= 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])) %>
</span>
<span class="input-group-append">
<button type="submit" class="btn btn-primary button-rounded", id="submit-search-btn", title="Search Authors">Search</button>
Expand Down
6 changes: 1 addition & 5 deletions app/views/books/_index_nav.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<% if params[:search_term].present? %>
<div class="pb-12">
<strong>Search Term: </strong><%= params[:search_term] %>
</div>
<% end %>
<%= render SearchTermComponent.new(search_term: params[:search_term]) %>
<div class="flex flex-wrap justify-between mb-8 xl:mb-16 gap-x-8 gap-y-2 md:gap-8">
<div class="flex flex-wrap gap-8 gap-y-2">
<span class="h2">
Expand Down
15 changes: 15 additions & 0 deletions app/views/components/search_term_component.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 1 addition & 5 deletions app/views/courses/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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? %>
<div class="pb-12">
<strong>Search Term: </strong><%= params[:search_term] %>
</div>
<% end %>
<%= render SearchTermComponent.new(search_term: params[:search_term]) %>
<div class="flex flex-wrap justify-between mb-8 xl:mb-16 gap-x-8 gap-y-2 md:gap-8">
<div>
<h2 class="mb-8 h2">All Courses</h2>
Expand Down
6 changes: 1 addition & 5 deletions app/views/newsletters/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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? %>
<div class="pb-12">
<strong>Search Term: </strong><%= params[:search_term] %>
</div>
<% end %>
<%= render SearchTermComponent.new(search_term: params[:search_term]) %>
<div class="flex flex-wrap justify-between mb-8 xl:mb-16 gap-x-8 gap-y-2 md:gap-8">
<div>
<h2 class="mb-8 h2">All Newsletters</h2>
Expand Down
14 changes: 3 additions & 11 deletions app/views/podcasts/index.html.erb
Original file line number Diff line number Diff line change
@@ -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? %>
<div class="pb-12">
<strong>Search Term: </strong><%= params[:search_term] %>
</div>
<% end %>

<%= render SearchTermComponent.new(search_term: params[:search_term]) %>
<div class="flex flex-wrap justify-between mb-8 xl:mb-16 gap-x-8 gap-y-2 md:gap-8">
<span>
<h2 class="h2 mb-8">All Podcasts</h2>
<h2 class="mb-8 h2">All Podcasts</h2>
</span>

<span>
<%= form_tag(podcasts_path, method: "get", id: 'search_form', remote: true) do %>
<div class="input-group">
<span class="input-group-append">
<%= 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])) %>
</span>
<span class="input-group-append">
<button type="submit" class="btn btn-primary button-rounded", id="submit-search-btn", title="Search Podcasts">Search</button>
Expand All @@ -28,6 +21,5 @@
<% end %>
</span>
</div>

<%= render partial: "podcasts/list", locals: { list: @podcasts } %>
<% end %>
6 changes: 1 addition & 5 deletions app/views/screencasts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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? %>
<div class="pb-12">
<strong>Search Term: </strong><%= params[:search_term] %>
</div>
<% end %>
<%= render SearchTermComponent.new(search_term: params[:search_term]) %>
<div class="flex flex-wrap justify-between mb-8 xl:mb-16 gap-x-8 gap-y-2 md:gap-8">
<div>
<h2 class="mb-8 h2">All Screencasts</h2>
Expand Down
6 changes: 1 addition & 5 deletions app/views/tags/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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? %>
<div class="pb-12">
<strong>Search Term: </strong><%= params[:search_term] %>
</div>
<% end %>
<%= render SearchTermComponent.new(search_term: params[:search_term]) %>
<div class="flex flex-wrap justify-between mb-8 xl:mb-16 gap-x-8 gap-y-2 md:gap-8">
<span>
<h2 class="mb-8 h2">All Topics</h2>
Expand Down
6 changes: 1 addition & 5 deletions app/views/youtubes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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? %>
<div class="pb-12">
<strong>Search Term: </strong><%= params[:search_term] %>
</div>
<% end %>
<%= render SearchTermComponent.new(search_term: params[:search_term]) %>
<div class="flex flex-wrap justify-between mb-8 xl:mb-16 gap-x-8 gap-y-2 md:gap-8">
<div>
<h2 class="mb-8 h2">All YouTube Courses</h2>
Expand Down
11 changes: 11 additions & 0 deletions test/components/previews/search_term_component_preview.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2988f42

Please sign in to comment.