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
|
||||
|
||||
@@ -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
|
||||
|
||||
20
app/models/portal_member.rb
Normal file
20
app/models/portal_member.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user