2014-03-26 11:09:39 +00:00
|
|
|
|
# == Schema Information
|
|
|
|
|
#
|
|
|
|
|
# Table name: comments
|
|
|
|
|
#
|
|
|
|
|
# id :integer not null, primary key
|
|
|
|
|
# commentable_type :string(255)
|
2020-03-18 03:38:17 +00:00
|
|
|
|
# text :text(65535)
|
|
|
|
|
# text_parsed :text(65535)
|
2014-03-26 11:09:39 +00:00
|
|
|
|
# created_at :datetime
|
|
|
|
|
# updated_at :datetime
|
2020-03-18 03:38:17 +00:00
|
|
|
|
# commentable_id :integer
|
|
|
|
|
# user_id :integer
|
|
|
|
|
#
|
|
|
|
|
# Indexes
|
|
|
|
|
#
|
|
|
|
|
# index_comments_on_commentable_type (commentable_type)
|
|
|
|
|
# index_comments_on_commentable_type_and_commentable_id (commentable_type,commentable_id)
|
|
|
|
|
# index_comments_on_commentable_type_and_id (commentable_type,id)
|
|
|
|
|
# index_comments_on_user_id (user_id)
|
2014-03-26 11:09:39 +00:00
|
|
|
|
#
|
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
|
class Comment < ActiveRecord::Base
|
|
|
|
|
include Extra
|
|
|
|
|
|
2020-03-16 23:57:47 +00:00
|
|
|
|
#attr_protected :id, :updated_at, :created_at, :user_id
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
2019-06-02 01:26:36 +00:00
|
|
|
|
scope :with_userteam, -> { includes({:user => :team}) }
|
|
|
|
|
scope :recent, -> (n) { order("id DESC").limit(n) }
|
|
|
|
|
scope :recent5, -> { order("id DESC").limit(5).group("commentable_id, commentable_type") }
|
|
|
|
|
scope :filtered, -> { where.not({"commentable_type" => 'Issue'}) }
|
|
|
|
|
scope :ordered, -> { order("id ASC") }
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
2020-03-26 02:26:30 +00:00
|
|
|
|
belongs_to :user, :optional => true
|
|
|
|
|
belongs_to :commentable, :polymorphic => true, :optional => true
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
|
|
validates_presence_of :commentable, :user
|
|
|
|
|
validates_length_of :text, :in => 1..10000
|
|
|
|
|
|
|
|
|
|
before_save :parse_text
|
|
|
|
|
|
|
|
|
|
def parse_text
|
2014-03-30 19:50:52 +00:00
|
|
|
|
if self.text
|
|
|
|
|
self.text_parsed = bbcode_to_html(self.text)
|
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def after_create
|
|
|
|
|
# if commentable_type == "Movie" or commentable_type == "Article" and commentable.user and commentable.user.profile.notify_own_stuff
|
|
|
|
|
# Notifications.deliver_comments commentable.user, commentable
|
|
|
|
|
# end
|
2014-03-30 19:50:52 +00:00
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
2014-03-30 19:50:52 +00:00
|
|
|
|
def can_create? cuser
|
|
|
|
|
return false unless cuser
|
|
|
|
|
#errors.add_to_base I18n.t(:comments_locked) if !commentable.lock.nil? and commentable.lock
|
|
|
|
|
errors.add_to_base I18n.t(:bans_mute) if cuser.banned? Ban::TYPE_MUTE
|
|
|
|
|
errors.add_to_base I18n.t(:registered_for_week) unless cuser.verified?
|
|
|
|
|
return errors.count == 0
|
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
2014-03-30 19:50:52 +00:00
|
|
|
|
def can_update? cuser
|
|
|
|
|
cuser and user == cuser or cuser.admin?
|
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
2014-03-30 19:50:52 +00:00
|
|
|
|
def can_destroy? cuser
|
|
|
|
|
cuser and cuser.admin?
|
|
|
|
|
end
|
2020-03-18 03:38:17 +00:00
|
|
|
|
|
|
|
|
|
def self.params params, cuser
|
2020-03-18 19:41:54 +00:00
|
|
|
|
params.require(:comment).permit(:text, :user_id, :commentable_type, :commentable_id)
|
2020-03-18 03:38:17 +00:00
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
|
end
|