fix: search with status and author (#5152)
This commit is contained in:
parent
d9b102cff0
commit
596b611fc0
4 changed files with 24 additions and 5 deletions
|
@ -46,7 +46,7 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
|
|||
end
|
||||
|
||||
def list_params
|
||||
params.permit(:locale, :query, :page, :category_slug)
|
||||
params.permit(:locale, :query, :page, :category_slug, :status, :author_id)
|
||||
end
|
||||
|
||||
def set_current_page
|
||||
|
|
|
@ -58,6 +58,8 @@ class Article < ApplicationRecord
|
|||
|
||||
scope :search_by_category_slug, ->(category_slug) { where(categories: { slug: category_slug }) if category_slug.present? }
|
||||
scope :search_by_category_locale, ->(locale) { where(categories: { locale: locale }) if locale.present? }
|
||||
scope :search_by_author, ->(author_id) { where(author_id: author_id) if author_id.present? }
|
||||
scope :search_by_status, ->(status) { where(status: status) if status.present? }
|
||||
|
||||
# TODO: if text search slows down https://www.postgresql.org/docs/current/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS
|
||||
pg_search_scope(
|
||||
|
@ -77,7 +79,10 @@ class Article < ApplicationRecord
|
|||
def self.search(params)
|
||||
records = joins(
|
||||
:category
|
||||
).search_by_category_slug(params[:category_slug]).search_by_category_locale(params[:locale])
|
||||
).search_by_category_slug(
|
||||
params[:category_slug]
|
||||
).search_by_category_locale(params[:locale]).search_by_author(params[:author_id]).search_by_status(params[:status])
|
||||
|
||||
records = records.text_search(params[:query]) if params[:query].present?
|
||||
records.page(current_page(params))
|
||||
end
|
||||
|
|
|
@ -19,7 +19,11 @@ json.portal_members do
|
|||
end
|
||||
|
||||
json.meta do
|
||||
json.articles_count portal.articles.size
|
||||
json.all_articles_count portal.articles.size
|
||||
json.archived_articles_count portal.articles.archived.size
|
||||
json.published_count portal.articles.published.size
|
||||
json.draft_articles_count portal.articles.draft.size
|
||||
json.mine_articles_count portal.articles.search_by_author(current_user.id).size if current_user.present?
|
||||
json.categories_count portal.categories.size
|
||||
json.default_locale portal.default_locale
|
||||
end
|
||||
|
|
|
@ -30,8 +30,8 @@ RSpec.describe Article, type: :model do
|
|||
create(:article, category_id: category_1.id, title: 'title 1', content: 'This is the content', portal_id: portal_1.id, author_id: user.id)
|
||||
create(:article, category_id: category_2.id, title: 'title 2', portal_id: portal_2.id, author_id: user.id)
|
||||
create(:article, category_id: category_2.id, title: 'title 3', portal_id: portal_1.id, author_id: user.id)
|
||||
create(:article, category_id: category_3.id, title: 'title 6', portal_id: portal_2.id, author_id: user.id)
|
||||
create(:article, category_id: category_2.id, title: 'title 7', portal_id: portal_1.id, author_id: user.id)
|
||||
create(:article, category_id: category_3.id, title: 'title 6', portal_id: portal_2.id, author_id: user.id, status: :published)
|
||||
create(:article, category_id: category_2.id, title: 'title 7', portal_id: portal_1.id, author_id: user.id, status: :published)
|
||||
end
|
||||
|
||||
context 'when no parameters passed' do
|
||||
|
@ -53,6 +53,10 @@ RSpec.describe Article, type: :model do
|
|||
params = { query: 'this', locale: 'en', category_slug: 'category_1' }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
|
||||
params = { status: 'published' }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(portal_1.articles.published.size)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -107,6 +111,12 @@ RSpec.describe Article, type: :model do
|
|||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
|
||||
it 'returns records with author and category_slug' do
|
||||
params = { category_slug: 'category_2', author_id: user.id }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with pagination' do
|
||||
|
|
Loading…
Reference in a new issue