From 596b611fc0d1842f45dc2b9f5ed515b65dae96fa Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Tue, 2 Aug 2022 15:28:27 +0530 Subject: [PATCH] fix: search with status and author (#5152) --- .../api/v1/accounts/articles_controller.rb | 2 +- app/models/article.rb | 7 ++++++- .../api/v1/accounts/portals/_portal.json.jbuilder | 6 +++++- spec/models/article_spec.rb | 14 ++++++++++++-- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/accounts/articles_controller.rb b/app/controllers/api/v1/accounts/articles_controller.rb index 8e3aadf5a..8576ec294 100644 --- a/app/controllers/api/v1/accounts/articles_controller.rb +++ b/app/controllers/api/v1/accounts/articles_controller.rb @@ -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 diff --git a/app/models/article.rb b/app/models/article.rb index f619726a6..3ad63bcaa 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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 diff --git a/app/views/api/v1/accounts/portals/_portal.json.jbuilder b/app/views/api/v1/accounts/portals/_portal.json.jbuilder index bd0d0b127..2f37f0a71 100644 --- a/app/views/api/v1/accounts/portals/_portal.json.jbuilder +++ b/app/views/api/v1/accounts/portals/_portal.json.jbuilder @@ -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 diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index bcc928214..6a66838ef 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -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