fix: Fetch categories by locale (#6557)

This commit is contained in:
Pranav Raj S
2023-02-27 09:12:14 -08:00
committed by GitHub
parent ce807d3251
commit b141fc1289
2 changed files with 19 additions and 4 deletions

View File

@@ -15,23 +15,32 @@ class Public::Api::V1::Portals::ArticlesController < PublicController
private
def set_article
@article = @category.articles.find(params[:id])
@article = @category.articles.find(permitted_params[:id])
@article.increment_view_count
@parsed_content = render_article_content(@article.content)
end
def set_category
@category = @portal.categories.find_by!(slug: params[:category_slug]) if params[:category_slug].present?
return if permitted_params[:category_slug].blank?
@category = @portal.categories.find_by!(
slug: permitted_params[:category_slug],
locale: permitted_params[:locale]
)
end
def portal
@portal ||= Portal.find_by!(slug: params[:slug], archived: false)
@portal ||= Portal.find_by!(slug: permitted_params[:slug], archived: false)
end
def list_params
params.permit(:query)
end
def permitted_params
params.permit(:slug, :category_slug, :locale, :id)
end
def render_article_content(content)
# rubocop:disable Rails/OutputSafety
CommonMarker.render_html(content).html_safe

View File

@@ -5,7 +5,7 @@ RSpec.describe 'Public Articles API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
let!(:portal) { create(:portal, slug: 'test-portal', config: { allowed_locales: %w[en es] }, custom_domain: 'www.example.com') }
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'en', slug: 'category_slug') }
let!(:category_2) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'es', slug: 'category_2_slug') }
let!(:category_2) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'es', slug: 'category_slug') }
let!(:article) { create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id) }
before do
@@ -44,5 +44,11 @@ RSpec.describe 'Public Articles API', type: :request do
expect(response).to have_http_status(:success)
expect(article.reload.views).to eq 1
end
it 'returns the article with the id with a different locale' do
article_in_locale = create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id)
get "/hc/#{portal.slug}/#{category_2.locale}/#{category_2.slug}/#{article_in_locale.id}"
expect(response).to have_http_status(:success)
end
end
end