From 268e26625b4c0efd934d0331ebebca6a13a7fef8 Mon Sep 17 00:00:00 2001 From: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Date: Thu, 9 Nov 2023 07:31:07 +0530 Subject: [PATCH] feat: Adds the ability to see the popular articles (#8152) Co-authored-by: Muhsin Keloth Co-authored-by: Pranav Raj S --- app/helpers/portal_helper.rb | 4 +++ .../api/v1/portals/_category-block.html.erb | 2 +- .../v1/portals/_featured_articles.html.erb | 28 +++++++++++++++++++ .../public/api/v1/portals/_hero.html.erb | 2 +- app/views/public/api/v1/portals/show.html.erb | 11 ++++++-- config/locales/en.yml | 1 + spec/helpers/portal_helper_spec.rb | 18 ++++++++++++ 7 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 app/views/public/api/v1/portals/_featured_articles.html.erb diff --git a/app/helpers/portal_helper.rb b/app/helpers/portal_helper.rb index 4f8014ac3..12ba41f95 100644 --- a/app/helpers/portal_helper.rb +++ b/app/helpers/portal_helper.rb @@ -37,4 +37,8 @@ module PortalHelper def generate_gradient_to_bottom(theme) "background-image: linear-gradient(to bottom, transparent, #{theme == 'dark' ? '#151718' : 'white'})" end + + def generate_article_link(portal_slug, article_slug, theme) + "/hc/#{portal_slug}/articles/#{article_slug}#{theme.present? && theme != 'system' ? "?theme=#{theme}" : ''}" + end end diff --git a/app/views/public/api/v1/portals/_category-block.html.erb b/app/views/public/api/v1/portals/_category-block.html.erb index 99f5ccce1..bcf9aebe3 100644 --- a/app/views/public/api/v1/portals/_category-block.html.erb +++ b/app/views/public/api/v1/portals/_category-block.html.erb @@ -24,7 +24,7 @@ <% else %> <% category.articles.published.order(position: :asc).take(5).each do |article| %> - +
<%= article.title %> diff --git a/app/views/public/api/v1/portals/_featured_articles.html.erb b/app/views/public/api/v1/portals/_featured_articles.html.erb new file mode 100644 index 000000000..8d671ccfd --- /dev/null +++ b/app/views/public/api/v1/portals/_featured_articles.html.erb @@ -0,0 +1,28 @@ +<% featured_articles = articles.where(category_id: categories).search_by_status(:published).order_by_views.limit(6) %> +<% if featured_articles.count >= 6 %> +
+ +
+<% end %> diff --git a/app/views/public/api/v1/portals/_hero.html.erb b/app/views/public/api/v1/portals/_hero.html.erb index 7f944e6c8..a96edcf5b 100644 --- a/app/views/public/api/v1/portals/_hero.html.erb +++ b/app/views/public/api/v1/portals/_hero.html.erb @@ -1,6 +1,6 @@ <% if !@is_plain_layout_enabled %>
-
+

<%= portal.header_text %> diff --git a/app/views/public/api/v1/portals/show.html.erb b/app/views/public/api/v1/portals/show.html.erb index c3c3005b1..a6af2931e 100644 --- a/app/views/public/api/v1/portals/show.html.erb +++ b/app/views/public/api/v1/portals/show.html.erb @@ -1,11 +1,16 @@ <%= render "public/api/v1/portals/hero", portal: @portal %> -
-
+
+ <%# Featured Articles %> + <% if !@is_plain_layout_enabled %> +
<%= render "public/api/v1/portals/featured_articles", articles: @portal.articles, categories: @portal.categories.where(locale: @locale), portal: @portal %>
+ <% end %> + <%# Categories with articles %> +
<% @portal.categories.where(locale: @locale).joins(:articles).where(articles:{ status: :published }).order(position: :asc).group('categories.id').each do |category| %> <%= render "public/api/v1/portals/category-block", category: category, portal: @portal %> <% end %>
- + <%# Uncategorized articles %>
<% if @portal.articles.where(status: :published, category_id: nil).count > 0 %> <%= render "public/api/v1/portals/uncategorized-block", category: "Uncategorized", portal: @portal %> diff --git a/config/locales/en.yml b/config/locales/en.yml index af26aa8f5..69c87b3e8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -224,6 +224,7 @@ en: system: System light: Light dark: Dark + featured_articles: Featured Articles 404: title: Page not found description: We couldn't find the page you were looking for. diff --git a/spec/helpers/portal_helper_spec.rb b/spec/helpers/portal_helper_spec.rb index 338a9260b..0f131a279 100644 --- a/spec/helpers/portal_helper_spec.rb +++ b/spec/helpers/portal_helper_spec.rb @@ -101,4 +101,22 @@ describe PortalHelper do end end end + + describe '#generate_article_link' do + context 'when theme is not present' do + it 'returns the correct link' do + expect(helper.generate_article_link('portal_slug', 'article_slug', nil)).to eq( + '/hc/portal_slug/articles/article_slug' + ) + end + end + + context 'when theme is present' do + it 'returns the correct link' do + expect(helper.generate_article_link('portal_slug', 'article_slug', 'dark')).to eq( + '/hc/portal_slug/articles/article_slug?theme=dark' + ) + end + end + end end