feat: APIs for Articles (#4777)

Fixes: #4802
This commit is contained in:
Tejaswini Chile
2022-06-13 15:56:49 +05:30
committed by GitHub
parent 2198930185
commit ae72757d23
23 changed files with 511 additions and 42 deletions

View File

@@ -2,25 +2,34 @@
#
# Table name: articles
#
# id :bigint not null, primary key
# content :text
# description :text
# status :integer
# title :string
# views :integer
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# author_id :integer
# category_id :integer
# folder_id :integer
# portal_id :integer not null
# id :bigint not null, primary key
# content :text
# description :text
# status :integer
# title :string
# views :integer
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# author_id :bigint
# category_id :integer
# folder_id :integer
# portal_id :integer not null
#
# Indexes
#
# index_articles_on_author_id (author_id)
#
# Foreign Keys
#
# fk_rails_... (author_id => users.id)
#
class Article < ApplicationRecord
include PgSearch::Model
belongs_to :account
belongs_to :category
belongs_to :portal
belongs_to :folder
belongs_to :author, class_name: 'User'
before_validation :ensure_account_id
@@ -32,6 +41,36 @@ class Article < ApplicationRecord
enum status: { draft: 0, published: 1 }
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? }
# TODO: if text search slows down https://www.postgresql.org/docs/current/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS
pg_search_scope(
:text_search,
against: %i[
title
description
content
],
using: {
tsearch: {
prefix: true
}
}
)
def self.search(params)
records = joins(
:category
).search_by_category_slug(params[:category_slug]).search_by_category_locale(params[:locale])
records.text_search(params[:query]) if params[:query].present?
records.page(current_page(params))
end
def self.current_page(params)
params[:page] || 1
end
private
def ensure_account_id