feat: CRUD operation for associated articles to current article (#4912)

This commit is contained in:
Tejaswini Chile
2022-07-04 20:29:44 +05:30
committed by GitHub
parent 62ed9fe1b4
commit ae59d0a343
28 changed files with 422 additions and 33 deletions

View File

@@ -1,5 +1,6 @@
class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
before_action :portal
before_action :check_authorization
before_action :fetch_article, except: [:index, :create]
def index
@@ -9,6 +10,8 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
def create
@article = @portal.articles.create!(article_params)
@article.associate_root_article(article_params[:associated_article_id])
render json: { error: @article.errors.messages }, status: :unprocessable_entity and return unless @article.valid?
end
def edit; end
@@ -36,7 +39,7 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
def article_params
params.require(:article).permit(
:title, :content, :description, :position, :category_id, :author_id
:title, :content, :description, :position, :category_id, :author_id, :associated_article_id
)
end

View File

@@ -1,5 +1,6 @@
class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseController
before_action :portal
before_action :check_authorization
before_action :fetch_category, except: [:index, :create]
def index

View File

@@ -1,10 +1,16 @@
class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
before_action :fetch_portal, except: [:index, :create]
before_action :check_authorization
def index
@portals = Current.account.portals
end
def add_members
agents = Current.account.agents.where(id: portal_member_params[:member_ids])
@portal.members << agents
end
def show; end
def create
@@ -35,4 +41,8 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
:account_id, :color, :custom_domain, :header_text, :homepage_link, :name, :page_title, :slug, :archived
)
end
def portal_member_params
params.require(:portal).permit(:account_id, member_ids: [])
end
end

View File

@@ -2,31 +2,45 @@
#
# 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 :bigint
# 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
# associated_article_id :bigint
# author_id :bigint
# category_id :integer
# folder_id :integer
# portal_id :integer not null
#
# Indexes
#
# index_articles_on_author_id (author_id)
# index_articles_on_associated_article_id (associated_article_id)
# index_articles_on_author_id (author_id)
#
# Foreign Keys
#
# fk_rails_... (associated_article_id => articles.id)
# fk_rails_... (author_id => users.id)
#
class Article < ApplicationRecord
include PgSearch::Model
has_many :associated_articles,
class_name: :Article,
foreign_key: :associated_article_id,
dependent: :nullify,
inverse_of: 'root_article'
belongs_to :root_article,
class_name: :Article,
foreign_key: :associated_article_id,
inverse_of: :associated_articles,
optional: true
belongs_to :account
belongs_to :category
belongs_to :portal
@@ -71,6 +85,21 @@ class Article < ApplicationRecord
params[:page] || 1
end
def associate_root_article(associated_article_id)
article = portal.articles.find(associated_article_id) if associated_article_id.present?
return if article.nil?
root_article_id = self.class.find_root_article_id(article)
update(associated_article_id: root_article_id) if root_article_id.present?
end
# Make sure we always associate the parent's associated id to avoid the deeper associations od articles.
def self.find_root_article_id(article)
article.associated_article_id || article.id
end
private
def ensure_account_id

View File

@@ -25,9 +25,18 @@ class Portal < ApplicationRecord
has_many :categories, dependent: :destroy_async
has_many :folders, through: :categories
has_many :articles, dependent: :destroy_async
has_many :users, through: :portals_members
has_many :portal_members,
class_name: :PortalMember,
dependent: :destroy_async
has_many :members,
through: :portal_members,
class_name: :User,
dependent: :nullify,
source: :user
validates :account_id, presence: true
validates :name, presence: true
validates :slug, presence: true, uniqueness: true
accepts_nested_attributes_for :members
end

View File

@@ -0,0 +1,20 @@
# == Schema Information
#
# Table name: portal_members
#
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# portal_id :bigint
# user_id :bigint
#
# Indexes
#
# index_portal_members_on_portal_id_and_user_id (portal_id,user_id) UNIQUE
# index_portal_members_on_user_id_and_portal_id (user_id,portal_id) UNIQUE
#
class PortalMember < ApplicationRecord
belongs_to :portal, class_name: 'Portal'
belongs_to :user, class_name: 'User'
validates :user_id, uniqueness: { scope: :portal_id }
end

View File

@@ -2,10 +2,10 @@
#
# Table name: related_categories
#
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# category_id :bigint
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# category_id :bigint
# related_category_id :bigint
#
# Indexes

View File

@@ -89,10 +89,17 @@ class User < ApplicationRecord
has_many :notification_settings, dependent: :destroy_async
has_many :notification_subscriptions, dependent: :destroy_async
has_many :notifications, dependent: :destroy_async
has_many :portals, through: :portals_members
has_many :team_members, dependent: :destroy_async
has_many :teams, through: :team_members
has_many :articles, foreign_key: 'author_id', dependent: :nullify
has_many :portal_members,
class_name: :PortalMember,
dependent: :destroy_async
has_many :portals,
through: :portals_members,
class_name: :Portal,
dependent: :nullify,
source: :portal
before_validation :set_password_and_uid, on: :create

View File

@@ -0,0 +1,31 @@
class ArticlePolicy < ApplicationPolicy
def index?
@account_user.administrator? || @account.users.include?(@user)
end
def update?
@account_user.administrator? || portal_member?
end
def show?
@account_user.administrator? || portal_member?
end
def edit?
@account_user.administrator? || portal_member?
end
def create?
@account_user.administrator? || portal_member?
end
def destroy?
@account_user.administrator? || portal_member?
end
private
def portal_member?
@record.first.portal.members.include?(@user)
end
end

View File

@@ -0,0 +1,31 @@
class CategoryPolicy < ApplicationPolicy
def index?
@account_user.administrator? || @account.users.include?(@user)
end
def update?
@account_user.administrator? || portal_member?
end
def show?
@account_user.administrator? || portal_member?
end
def edit?
@account_user.administrator? || portal_member?
end
def create?
@account_user.administrator? || portal_member?
end
def destroy?
@account_user.administrator? || portal_member?
end
private
def portal_member?
@record.first.portal.members.include?(@user)
end
end

View File

@@ -0,0 +1,35 @@
class PortalPolicy < ApplicationPolicy
def index?
@account_user.administrator? || @account.users.include?(@user)
end
def update?
@account_user.administrator?
end
def show?
@account_user.administrator? || portal_member?
end
def edit?
@account_user.administrator?
end
def create?
@account_user.administrator?
end
def destroy?
@account_user.administrator?
end
def add_members?
@account_user.administrator?
end
private
def portal_member?
@record.first.members.include?(@user)
end
end

View File

@@ -11,6 +11,7 @@ if article.portal.present?
json.partial! 'api/v1/accounts/portals/portal.json.jbuilder', portal: article.portal
end
end
json.views article.views
if article.author.present?
@@ -18,3 +19,11 @@ if article.author.present?
json.partial! 'api/v1/models/agent.json.jbuilder', resource: article.author
end
end
json.associated_articles do
if article.associated_articles.any?
json.array! article.associated_articles.each do |associated_article|
json.partial! 'api/v1/accounts/articles/associated_article.json.jbuilder', article: associated_article
end
end
end

View File

@@ -0,0 +1,21 @@
json.id article.id
json.category_id article.category_id
json.title article.title
json.content article.content
json.description article.description
json.status article.status
json.account_id article.account_id
if article.portal.present?
json.portal do
json.partial! 'api/v1/accounts/portals/portal.json.jbuilder', portal: article.portal
end
end
json.views article.views
if article.author.present?
json.author do
json.partial! 'api/v1/models/agent.json.jbuilder', resource: article.author
end
end

View File

@@ -8,3 +8,11 @@ json.page_title portal.page_title
json.slug portal.slug
json.archived portal.archived
json.config portal.config
json.portal_members do
if portal.members.any?
json.array! portal.members.each do |member|
json.partial! 'api/v1/models/agent.json.jbuilder', resource: member
end
end
end

View File

@@ -0,0 +1 @@
json.partial! 'portal', portal: @portal

View File

@@ -1 +1,3 @@
json.array! @portals, partial: 'portal', as: :portal
json.payload do
json.array! @portals, partial: 'portal', as: :portal
end