add: 整合评论功能
This commit is contained in:
parent
d7dc889023
commit
f6da993c2b
|
@ -3,6 +3,8 @@ require "acts_as_able/version"
|
|||
module ActsAsAble
|
||||
extend ActiveSupport::Autoload
|
||||
autoload :AbleExt, 'acts_as_able/able_ext'
|
||||
autoload :Commentable, 'acts_as_able/commentable'
|
||||
autoload :Commenter, 'acts_as_able/commenter'
|
||||
autoload :Dissable, 'acts_as_able/dissable'
|
||||
autoload :Disser, 'acts_as_able/disser'
|
||||
autoload :Followable, 'acts_as_able/followable'
|
||||
|
@ -14,6 +16,8 @@ module ActsAsAble
|
|||
|
||||
# require 'acts_as_followable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
include ActsAsAble::Commentable
|
||||
include ActsAsAble::Commenter
|
||||
include ActsAsAble::Dissable
|
||||
include ActsAsAble::Disser
|
||||
include ActsAsAble::Followable
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
module ActsAsAble
|
||||
# 被评论的对象
|
||||
module Commentable
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Those call this method has the ability to be unlike by others
|
||||
def acts_as_commentable
|
||||
include ActsAsAble::AbleExt
|
||||
has_many :commenters, as: :commentable, dependent: :destroy, class_name: 'Comment'
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def root_commenters
|
||||
self.commenters.where(parent_id: nil)
|
||||
end
|
||||
|
||||
def comment_count
|
||||
self.commenters.count
|
||||
end
|
||||
|
||||
def commenters_by_type(commenter_type, options = {})
|
||||
ids = Diss.
|
||||
where('commentable_id' => self.id,
|
||||
'commentable_type' => class_name(self),
|
||||
'commenter_type' => commenter_type.name
|
||||
).pluck('commenter_id')
|
||||
return commenter_type.where("id in (?)", ids)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,49 @@
|
|||
module ActsAsAble
|
||||
# 发起评论的对象
|
||||
module Commenter
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Those call this method has the ability to be unlike by others
|
||||
def acts_as_commenter
|
||||
include ActsAsAble::AbleExt
|
||||
has_many :comments, as: :commenter, dependent: :destroy, class_name: 'Comment'
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
# 所有的一级评论
|
||||
def root_comments
|
||||
self.comments.where(parent_id: nil)
|
||||
end
|
||||
|
||||
# 评论某对象
|
||||
def comment(content, obj, parent_comment = nil)
|
||||
if parent_comment.present? && parent_comment.is_a?(Comment)
|
||||
self.comments.create(content: content,commentable_type: class_name(obj), commentable_id: obj.id, parent_id: parent_comment.id )
|
||||
else
|
||||
self.comments.create(content: content, commentable_type: class_name(obj), commentable_id: obj.id,)
|
||||
end
|
||||
end
|
||||
|
||||
# 是否评论了某对象 & 回复了某对象的评论
|
||||
def comment?(obj)
|
||||
!comment_by(obj).blank?
|
||||
end
|
||||
|
||||
# 查看某种类型评论的所有对象
|
||||
def commentings(commentable_type)
|
||||
return commentable_type.constantize.where(id: self.comments.where(commentable_type: commentable_type).pluck(:commentable_id))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def comment_by(obj)
|
||||
self.comments.find_by(commentable_type: class_name(obj), commentable_id: obj.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,7 +8,7 @@ module ActsAsAble
|
|||
|
||||
module ClassMethods
|
||||
# Those call this method has the ability to be unlike by others
|
||||
def acts_as_disable
|
||||
def acts_as_dissable
|
||||
include ActsAsAble::AbleExt
|
||||
has_many :dissers, as: :dissable, dependent: :destroy, class_name: 'Diss'
|
||||
end
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
require 'generators/acts_as_able_generator'
|
||||
|
||||
class ActsAsCommentableGenerator < ActsAsAbleGenerator
|
||||
def create_migration_file
|
||||
migration_template 'comment_migration.rb', 'db/migrate/acts_as_commentable_migration.rb'
|
||||
end
|
||||
|
||||
def create_model
|
||||
template "comment.rb", File.join('app/models', "comment.rb")
|
||||
end
|
||||
end
|
|
@ -2,7 +2,7 @@ require 'generators/acts_as_able_generator'
|
|||
|
||||
class ActsAsDissableGenerator < ActsAsAbleGenerator
|
||||
def create_migration_file
|
||||
migration_template 'diss_migration.rb', 'db/migrate/acts_as_diss_migration.rb'
|
||||
migration_template 'diss_migration.rb', 'db/migrate/acts_as_dissable_migration.rb'
|
||||
end
|
||||
|
||||
def create_model
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'generators/acts_as_able_generator'
|
|||
|
||||
class ActsAsLikableGenerator < ActsAsAbleGenerator
|
||||
def create_migration_file
|
||||
migration_template 'like_migration.rb', 'db/migrate/acts_as_like_migration.rb'
|
||||
migration_template 'like_migration.rb', 'db/migrate/acts_as_likable_migration.rb'
|
||||
end
|
||||
|
||||
def create_model
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
class Comment < ApplicationRecord
|
||||
|
||||
belongs_to :commentable, :polymorphic => true
|
||||
belongs_to :commenter, :polymorphic => true
|
||||
|
||||
belongs_to :parent_comment, optional: true, foreign_key: :parent_id, class_name: 'Comment', counter_cache: :children_comments_count
|
||||
has_many :children_comments, foreign_key: :parent_id, dependent: :destroy, class_name: 'Comment'
|
||||
|
||||
|
||||
end
|
|
@ -0,0 +1,17 @@
|
|||
class ActsAsCommentableMigration < ActiveRecord::Migration[5.0]
|
||||
def self.up
|
||||
create_table :comments, force: true do |t|
|
||||
t.references :commentable, polymorphic: true, null: false
|
||||
t.references :commenter, polymorphic: true, null: false
|
||||
t.integer :parent_id
|
||||
t.integer :children_comments_count, default: 0
|
||||
t.text :content
|
||||
t.timestamps
|
||||
end
|
||||
add_index :comments, :parent_id
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :comments
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue