From f6da993c2bc70a8d8276a2caeffbbbf2b23855d8 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Mon, 17 May 2021 17:11:56 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=95=B4=E5=90=88=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/acts_as_able.rb | 4 ++ lib/acts_as_able/commentable.rb | 36 ++++++++++++++ lib/acts_as_able/commenter.rb | 49 +++++++++++++++++++ lib/acts_as_able/dissable.rb | 2 +- .../acts_as_commentable_generator.rb | 11 +++++ lib/generators/acts_as_dissable_generator.rb | 2 +- lib/generators/acts_as_likable_generator.rb | 2 +- lib/generators/templates/comment.rb | 10 ++++ lib/generators/templates/comment_migration.rb | 17 +++++++ 9 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 lib/acts_as_able/commentable.rb create mode 100644 lib/acts_as_able/commenter.rb create mode 100644 lib/generators/acts_as_commentable_generator.rb create mode 100644 lib/generators/templates/comment.rb create mode 100644 lib/generators/templates/comment_migration.rb diff --git a/lib/acts_as_able.rb b/lib/acts_as_able.rb index 7b8d5d2..603d4dd 100644 --- a/lib/acts_as_able.rb +++ b/lib/acts_as_able.rb @@ -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 diff --git a/lib/acts_as_able/commentable.rb b/lib/acts_as_able/commentable.rb new file mode 100644 index 0000000..aa2296a --- /dev/null +++ b/lib/acts_as_able/commentable.rb @@ -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 diff --git a/lib/acts_as_able/commenter.rb b/lib/acts_as_able/commenter.rb new file mode 100644 index 0000000..49db9b5 --- /dev/null +++ b/lib/acts_as_able/commenter.rb @@ -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 diff --git a/lib/acts_as_able/dissable.rb b/lib/acts_as_able/dissable.rb index 21e443a..5d6d318 100644 --- a/lib/acts_as_able/dissable.rb +++ b/lib/acts_as_able/dissable.rb @@ -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 diff --git a/lib/generators/acts_as_commentable_generator.rb b/lib/generators/acts_as_commentable_generator.rb new file mode 100644 index 0000000..c564f11 --- /dev/null +++ b/lib/generators/acts_as_commentable_generator.rb @@ -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 diff --git a/lib/generators/acts_as_dissable_generator.rb b/lib/generators/acts_as_dissable_generator.rb index 43ca20e..d5c8ff6 100644 --- a/lib/generators/acts_as_dissable_generator.rb +++ b/lib/generators/acts_as_dissable_generator.rb @@ -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 diff --git a/lib/generators/acts_as_likable_generator.rb b/lib/generators/acts_as_likable_generator.rb index 9bf92d1..2404941 100644 --- a/lib/generators/acts_as_likable_generator.rb +++ b/lib/generators/acts_as_likable_generator.rb @@ -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 diff --git a/lib/generators/templates/comment.rb b/lib/generators/templates/comment.rb new file mode 100644 index 0000000..2c436c1 --- /dev/null +++ b/lib/generators/templates/comment.rb @@ -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 diff --git a/lib/generators/templates/comment_migration.rb b/lib/generators/templates/comment_migration.rb new file mode 100644 index 0000000..b3fa252 --- /dev/null +++ b/lib/generators/templates/comment_migration.rb @@ -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