socialforge/app/models/blog_comment.rb

108 lines
3.5 KiB
Ruby
Raw Normal View History

2016-04-08 18:52:26 +08:00
#encoding: utf-8
2015-10-24 15:34:43 +08:00
class BlogComment < ActiveRecord::Base
# attr_accessible :title, :body
2016-03-31 20:46:36 +08:00
require 'net/http'
require 'json'
2015-10-24 15:34:43 +08:00
include Redmine::SafeAttributes
belongs_to :blog
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
has_many :blog_messages, :class_name => 'BlogMessage', :as => :blog_message, :dependent => :destroy
2015-10-24 15:34:43 +08:00
acts_as_tree :counter_cache => :comments_count, :order => "#{BlogComment.table_name}.sticky desc ,#{BlogComment.table_name}.created_on ASC"
acts_as_attachable
belongs_to :last_reply, :class_name => 'BlogComment', :foreign_key => 'last_comment_id'
# 虚拟关联
has_many :user_acts, :class_name => 'UserAcivity',:as =>:act
2016-09-23 16:44:49 +08:00
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
has_one :praise_tread_cache, as: :object, dependent: :destroy
2015-10-24 15:34:43 +08:00
acts_as_watchable
validates_presence_of :title, :content
validates_length_of :title, :maximum => 255
#validate :cannot_reply_to_locked_comment, :on => :create
safe_attributes 'title', 'content',"sticky", "locked", "root_id"
2015-10-24 15:34:43 +08:00
after_save :add_user_activity
2016-01-12 16:13:49 +08:00
after_update :update_activity
2016-06-02 13:56:11 +08:00
after_create :update_parent_time
before_destroy :destroy_user_activity
2015-10-29 11:53:52 +08:00
scope :like, lambda {|arg|
if arg.blank?
where(nil)
else
pattern = "%#{arg.to_s.strip.downcase}%"
where(" LOWER(title) LIKE :p ", :p => pattern)
end
}
2016-01-12 16:13:49 +08:00
#动态更新
def update_activity
user_activity = UserActivity.where("act_type='BlogComment' and act_id =?",self.id).first
if user_activity
user_activity.updated_at = Time.now
user_activity.save
end
end
#在个人动态里面增加当前动态
def add_user_activity
if self.parent_id.nil? #只有发博文才插入动态
user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'").first
if user_activity
user_activity.save
else
user_activity = UserActivity.new
user_activity.act_id = self.id
user_activity.act_type = self.class.to_s
user_activity.container_type = "Blog"
user_activity.container_id = self.blog_id
user_activity.user_id = self.author_id
user_activity.save
end
end
end
def destroy_user_activity
user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'")
user_activity.destroy_all
end
2015-10-24 15:34:43 +08:00
def deleted_attach_able_by? user
(user && user.logged? && (self.author == user) ) || user.admin?
end
def update_parent_time
if !self.parent.nil?
self.root.update_attribute(:updated_on, self.updated_on)
end
end
2015-10-24 15:34:43 +08:00
def project
end
2016-03-31 20:46:36 +08:00
def creator_user
self.author
end
def created_time
self.created_at
end
def content_detail
self.content
end
2016-03-31 20:46:36 +08:00
#博客回复微信模板消息
# def blog_wechat_message
# ws = WechatService.new
# if self.parent_id.nil?
# self.author.watcher_users.each do |watcher|
# content = strip_html self.author.try(:realname) + " 发表了博客:" + self.title.html_safe, 200
# ws.message_update_template watcher.id, "blog_comment", self.id, "#{l(:label_new_blog_template)}", content, format_time(self.created_at)
# end
# else
# content = strip_html self.content.html_safe, 200
# ws.comment_template self.parent.author_id, "blog_comment", self.parent_id, "#{l(:label_blog_comment_template)}", self.author.try(:realname), format_time(self.created_at), content
# end
# end
2015-10-24 15:34:43 +08:00
end