Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 162 lines (127 sloc) 4.209 kb
504a31b @nono WIP redaction space
nono authored
1 # == Schema Information
2 #
3 # Table name: comments
4 #
5 # id :integer(4) not null, primary key
6 # node_id :integer(4)
7 # user_id :integer(4)
8 # state :string(255) default("published"), not null
9 # title :string(255)
10 # body :text
11 # wiki_body :text
12 # score :integer(4) default(0)
13 # answered_to_self :boolean(1) default(FALSE)
14 # materialized_path :string(1022)
15 # created_at :datetime
16 # updated_at :datetime
17 #
18
4759316 @nono Added some comments on the models
nono authored
19 # The users can comment any content.
20 # Those comments are threaded and can be noted.
21 #
c92bd13 @nono New resource: comments
nono authored
22 class Comment < ActiveRecord::Base
23 belongs_to :user
f49e8e0 @nono Refactoring comments
nono authored
24 belongs_to :node, :touch => :last_commented_at, :counter_cache => :comments_count
ec46a7b @nono Add relevance on comments (they are like votes for nodes)
nono authored
25 has_many :relevances
c92bd13 @nono New resource: comments
nono authored
26
a1616d6 @nono Use the rails_best_practices gem to refactor code
nono authored
27 delegate :content, :content_type, :to => :node
28
32970df @nono Cache wikified body in database for comments, diaries, interviews, po…
nono authored
29 attr_accessible :title, :wiki_body, :node_id, :parent_id
837c0b6 @nono Attr_accessible on models
nono authored
30
7831d67 @nono named_scope is now just scope
nono authored
31 scope :published, where(:state => 'published')
32 scope :descendants, lambda { |path| where("materialized_path LIKE ?", "#{path}_%") }
33 scope :on_dashboard, published.where(:answered_to_self => false).order('created_at DESC')
34 scope :footer, published.order('created_at DESC').limit(12)
e8248bb @nono RSS feed for comments (by content)
nono authored
35
32970df @nono Cache wikified body in database for comments, diaries, interviews, po…
nono authored
36 validates_presence_of :title, :message => "Le titre est obligatoire"
37 validates_presence_of :wiki_body, :message => "Vous ne pouvez pas poster un commentaire vide"
710f7ca @nono Use the wiki creole syntax for comments, diaries and posts in the forums
nono authored
38
b728e8a @nono We can submit a news (again)
nono authored
39 wikify_attr :body
97b5cc1 @nono Use wikitext instead of WikiCreole for the wiki syntax
nono authored
40
177c669 @nono Sphinx indexation
nono authored
41 ### Sphinx ####
42
6eb5740 @nono Comment out sphinx indexes while TS is not compatible with Rails3
nono authored
43 # TODO Rails 3
44 # define_index do
45 # indexes title, body
46 # indexes user.name, :as => :user
47 # where "state = 'published'"
48 # set_property :field_weights => { :title => 5, :user => 2, :body => 1 }
49 # set_property :delta => :datetime, :threshold => 75.minutes
50 # end
177c669 @nono Sphinx indexation
nono authored
51
c258edb @nono Show new comments
nono authored
52 ### Reading status ###
e6825b0 @nono Tracks when a user reads a content
nono authored
53
c258edb @nono Show new comments
nono authored
54 # Returns true if this comment has been read by the given user,
55 # but also for anonymous users
56 def read_by?(user)
57 return true if user.nil?
7831d67 @nono named_scope is now just scope
nono authored
58 r = Reading.where(:user_id => user.id, :node_id => node_id).first
c258edb @nono Show new comments
nono authored
59 r && r.updated_at >= created_at
60 end
61
9dfd715 @nono Refactoring the comments: from nested set to materialized path (work …
nono authored
62 ### Threads ###
63
6634bf5 @nono Yihaa, the comments are shown correctly in their threads
nono authored
64 PATH_SIZE = 12 # Each id in the materialized_path is coded on 12 chars
65 MAX_DEPTH = 1022 / PATH_SIZE
66
67 after_create :generate_materialized_path
68 def generate_materialized_path
e34a9c9 @nono Style : use present? instead of negation + blank?
nono authored
69 parent = Comment.find(parent_id) if parent_id.present?
6634bf5 @nono Yihaa, the comments are shown correctly in their threads
nono authored
70 parent_path = parent ? parent.materialized_path : ''
71 self.materialized_path = "%s%0#{PATH_SIZE}d" % [parent_path, self.id]
d603856 @nono Do not show duplicate comments thread on the dashboard
nono authored
72 self.answered_to_self = is_an_answer_to_self?
6634bf5 @nono Yihaa, the comments are shown correctly in their threads
nono authored
73 save
74 end
75
d195fa8 @nono Link to parent for non-root comments
nono authored
76 def parent_id
c478954 @nono Fix some broken features (too strict security)
nono authored
77 @parent_id ||= materialized_path && materialized_path[-2 * PATH_SIZE .. - PATH_SIZE - 1].to_i
f49e8e0 @nono Refactoring comments
nono authored
78 @parent_id
d195fa8 @nono Link to parent for non-root comments
nono authored
79 end
6634bf5 @nono Yihaa, the comments are shown correctly in their threads
nono authored
80
9dfd715 @nono Refactoring the comments: from nested set to materialized path (work …
nono authored
81 def parent_id=(parent_id)
6634bf5 @nono Yihaa, the comments are shown correctly in their threads
nono authored
82 @parent_id = parent_id
9dfd715 @nono Refactoring the comments: from nested set to materialized path (work …
nono authored
83 return if parent_id.blank?
84 parent = Comment.find(parent_id)
85 self.title ||= parent ? "Re: #{parent.title}" : ''
86 end
87
6634bf5 @nono Yihaa, the comments are shown correctly in their threads
nono authored
88 def depth
89 (materialized_path.length / PATH_SIZE) - 1
90 end
91
92 def root?
93 depth == 0
9dfd715 @nono Refactoring the comments: from nested set to materialized path (work …
nono authored
94 end
95
d603856 @nono Do not show duplicate comments thread on the dashboard
nono authored
96 def is_an_answer_to_self?
97 return false if root?
7831d67 @nono named_scope is now just scope
nono authored
98 ret = Comment.where(:node_id => node_id, :user_id => user_id).
99 where("LOCATE(materialized_path, ?) > 0").
100 where("id != ?", self.id).
101 exists?
d603856 @nono Do not show duplicate comments thread on the dashboard
nono authored
102 end
103
fd75851 @nono New page: dashboard
nono authored
104 ### Calculations ###
105
649632b @nono Users karma and nb votes
nono authored
106 before_create :default_score
107 def default_score
108 self.score = Math.log10(user.account.karma).to_i - 1
109 end
110
fd75851 @nono New page: dashboard
nono authored
111 def nb_answers
112 self.class.published.descendants(materialized_path).count
113 end
114
115 def last_answer
7831d67 @nono named_scope is now just scope
nono authored
116 self.class.published.descendants(materialized_path).order('created_at DESC').first
fd75851 @nono New page: dashboard
nono authored
117 end
118
4761e2a @nono Authorizations
nono authored
119 ### ACL ###
120
121 def readable_by?(user)
122 state != 'deleted' || (user && user.admin?)
123 end
124
125 def creatable_by?(user)
126 node && node.content && node.content.commentable_by?(user)
127 end
128
129 def editable_by?(user)
130 user && (user.moderator? || user.admin?)
131 end
132
133 def deletable_by?(user)
134 user && (user.moderator? || user.admin?)
135 end
136
ec46a7b @nono Add relevance on comments (they are like votes for nodes)
nono authored
137 def votable_by?(user)
3d1f2ee @nono Do not allow users to vote on old contents/comments
nono authored
138 user && !deleted? && self.user != user &&
139 (Time.now - created_at) < 3.months &&
649632b @nono Users karma and nb votes
nono authored
140 user.account.nb_votes > 0 &&
3d1f2ee @nono Do not allow users to vote on old contents/comments
nono authored
141 !user.relevances.exists?(:comment_id => id)
ec46a7b @nono Add relevance on comments (they are like votes for nodes)
nono authored
142 end
143
4761e2a @nono Authorizations
nono authored
144 ### Workflow ###
145
698f781 @nono Moderation of diaries
nono authored
146 def mark_as_deleted
5a410b5 @nono Moderators can edit/delete comments
nono authored
147 self.state = 'deleted'
4761e2a @nono Authorizations
nono authored
148 save
149 end
150
5a410b5 @nono Moderators can edit/delete comments
nono authored
151 def deleted?
152 state == 'deleted'
153 end
154
a1616d6 @nono Use the rails_best_practices gem to refactor code
nono authored
155 ### Presentation ###
156
157 def user_name
158 user.try :name
159 end
160
c92bd13 @nono New resource: comments
nono authored
161 end
Something went wrong with that request. Please try again.