forked from consuldemocracy/consuldemocracy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.rb
126 lines (94 loc) · 3.25 KB
/
comment.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class Comment < ActiveRecord::Base
include Flaggable
include HasPublicAuthor
include Graphqlable
COMMENTABLE_TYPES = %w(Debate Proposal Budget::Investment Poll::Question Legislation::Question Legislation::Annotation Topic Poll).freeze
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
acts_as_votable
has_ancestry touch: true
attr_accessor :as_moderator, :as_administrator
validates :body, presence: true
validates :user, presence: true
validates :commentable_type, inclusion: { in: COMMENTABLE_TYPES }
validate :validate_body_length
belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true
belongs_to :user, -> { with_hidden }
before_save :calculate_confidence_score
scope :for_render, -> { with_hidden.includes(user: :organization) }
scope :with_visible_author, -> { joins(:user).where("users.hidden_at IS NULL") }
scope :not_as_admin_or_moderator, -> { where("administrator_id IS NULL").where("moderator_id IS NULL")}
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :public_for_api, -> do
where(%{(comments.commentable_type = 'Debate' and comments.commentable_id in (?)) or
(comments.commentable_type = 'Proposal' and comments.commentable_id in (?))},
Debate.public_for_api.pluck(:id),
Proposal.public_for_api.pluck(:id))
end
scope :sort_by_most_voted, -> { order(confidence_score: :desc, created_at: :desc) }
scope :sort_descendants_by_most_voted, -> { order(confidence_score: :desc, created_at: :asc) }
scope :sort_by_newest, -> { order(created_at: :desc) }
scope :sort_descendants_by_newest, -> { order(created_at: :desc) }
scope :sort_by_oldest, -> { order(created_at: :asc) }
scope :sort_descendants_by_oldest, -> { order(created_at: :asc) }
after_create :call_after_commented
def self.build(commentable, user, body, p_id = nil)
new commentable: commentable,
user_id: user.id,
body: body,
parent_id: p_id
end
def self.find_commentable(c_type, c_id)
c_type.constantize.find(c_id)
end
def author_id
user_id
end
def author
user
end
def author=(author)
self.user = author
end
def total_votes
cached_votes_total
end
def total_likes
cached_votes_up
end
def total_dislikes
cached_votes_down
end
def as_administrator?
administrator_id.present?
end
def as_moderator?
moderator_id.present?
end
def after_hide
commentable_type.constantize.with_hidden.reset_counters(commentable_id, :comments)
end
def after_restore
commentable_type.constantize.with_hidden.reset_counters(commentable_id, :comments)
end
def reply?
!root?
end
def call_after_commented
commentable.try(:after_commented)
end
def self.body_max_length
Setting['comments_body_max_length'].to_i
end
def calculate_confidence_score
self.confidence_score = ScoreCalculator.confidence_score(cached_votes_total,
cached_votes_up)
end
private
def validate_body_length
validator = ActiveModel::Validations::LengthValidator.new(
attributes: :body,
maximum: Comment.body_max_length)
validator.validate(self)
end
end