chore: Add articles metadata (#5125)

This commit is contained in:
Tejaswini Chile
2022-07-28 13:59:16 +05:30
committed by GitHub
parent 7f48cffeb3
commit 41df70fb96
19 changed files with 74 additions and 15 deletions

View File

@@ -2,10 +2,11 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
before_action :portal
before_action :check_authorization
before_action :fetch_article, except: [:index, :create]
before_action :set_current_page, only: [:index]
def index
@articles = @portal.articles
@articles = @articles.search(list_params) if params[:payload].present?
@articles = @articles.search(list_params) if list_params.present?
end
def create
@@ -45,8 +46,10 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
end
def list_params
params.require(:payload).permit(
:category_slug, :locale, :query, :page
)
params.permit(:locale, :query, :page, :category_slug)
end
def set_current_page
@current_page = params[:page] || 1
end
end

View File

@@ -2,6 +2,7 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle
before_action :portal
before_action :check_authorization
before_action :fetch_category, except: [:index, :create]
before_action :set_current_page, only: [:index]
def index
@categories = @portal.categories.search(params)
@@ -49,4 +50,8 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle
:name, :description, :position, :slug, :locale, :parent_category_id, :associated_category_id
)
end
def set_current_page
@current_page = params[:page] || 1
end
end

View File

@@ -3,6 +3,7 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
before_action :fetch_portal, except: [:index, :create]
before_action :check_authorization
before_action :set_current_page, only: [:index]
def index
@portals = Current.account.portals
@@ -66,4 +67,8 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
def portal_member_params
params.require(:portal).permit(:account_id, member_ids: [])
end
def set_current_page
@current_page = params[:page] || 1
end
end

View File

@@ -4,7 +4,7 @@ class Public::Api::V1::Portals::ArticlesController < ApplicationController
def index
@articles = @portal.articles
@articles = @articles.search(list_params) if params[:payload].present?
@articles = @articles.search(list_params) if list_params.present?
end
def show; end
@@ -20,6 +20,6 @@ class Public::Api::V1::Portals::ArticlesController < ApplicationController
end
def list_params
params.require(:payload).permit(:query)
params.permit(:query)
end
end

View File

@@ -46,7 +46,7 @@ class Portal < ApplicationRecord
scope :active, -> { where(archived: false) }
CONFIG_JSON_KEYS = %w[allowed_locales].freeze
CONFIG_JSON_KEYS = %w[allowed_locales default_locale].freeze
def file_base_data
{
@@ -60,9 +60,14 @@ class Portal < ApplicationRecord
}
end
def default_locale
config['default_locale'] || 'en'
end
private
def config_json_format
config['default_locale'] = 'en'
denied_keys = config.keys - CONFIG_JSON_KEYS
errors.add(:cofig, "in portal on #{denied_keys.join(',')} is not supported.") if denied_keys.any?
end

View File

@@ -1,3 +1,8 @@
json.payload do
json.array! @articles, partial: 'article', as: :article
end
json.meta do
json.current_page @current_page
json.articles_count @articles.size
end

View File

@@ -26,4 +26,6 @@ if category.root_category.present?
end
end
json.articles_count category.articles.published.try(:count)
json.meta do
json.articles_count category.articles.size
end

View File

@@ -1,3 +1,8 @@
json.payload do
json.array! @categories, partial: 'category', as: :category
end
json.meta do
json.current_page @current_page
json.categories_count @categories.size
end

View File

@@ -17,3 +17,9 @@ json.portal_members do
end
end
end
json.meta do
json.articles_count portal.articles.size
json.categories_count portal.categories.size
json.default_locale portal.default_locale
end

View File

@@ -1,3 +1,8 @@
json.payload do
json.array! @portals, partial: 'portal', as: :portal
end
json.meta do
json.current_page @current_page
json.portals_count @portals.size
end

View File

@@ -24,4 +24,6 @@ if category.root_category.present?
end
end
json.articles_count category.articles.published.try(:count)
json.meta do
json.articles_count category.articles.published.size
end

View File

@@ -8,7 +8,13 @@ json.slug portal.slug
json.categories do
if portal.categories.any?
json.array! portal.categories.each do |category|
json.partial! 'categories/category.json.jbuilder', category: category
json.partial! 'api/v1/models/category.json.jbuilder', category: category
end
end
end
json.meta do
json.articles_count portal.articles.published.size
json.categories_count portal.categories.size
json.default_locale portal.default_locale
end

View File

@@ -1,3 +1,7 @@
json.payload do
json.array! @articles, partial: 'public/api/v1/models/article.json.jbuilder', as: :article
end
json.meta do
json.articles_count @articles.published.size
end