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

@@ -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