feat: CRUD operation for associated articles to current article (#4912)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user