Commit 18cbbd31 by Clinton Blackburn

Cleaned Comment model

- Properly grouped statements
- Replaced double quotes with single quotes
- Made set_sk a private method
parent 9f6b0ad7
......@@ -6,6 +6,8 @@ class Comment < Content
include Mongoid::Timestamps
include Mongoid::MagicCounterCache
include ActiveModel::MassAssignmentSecurity
include Tire::Model::Search
include Tire::Model::Callbacks
voteable self, :up => +1, :down => -1
......@@ -17,21 +19,11 @@ class Comment < Content
field :anonymous_to_peers, type: Boolean, default: false
field :commentable_id, type: String
field :at_position_list, type: Array, default: []
field :sk, type: String, default: nil
index({author_id: 1, course_id: 1})
index({_type: 1, comment_thread_id: 1, author_id: 1, updated_at: 1})
field :sk, type: String, default: nil
before_save :set_sk
def set_sk()
# this attribute is explicitly write-once
if self.sk.nil?
self.sk = (self.parent_ids.dup << self.id).join("-")
end
end
include Tire::Model::Search
include Tire::Model::Callbacks
index_name Content::ES_INDEX_NAME
......@@ -48,7 +40,7 @@ class Comment < Content
belongs_to :comment_thread, index: true
belongs_to :author, class_name: "User", index: true
belongs_to :author, class_name: 'User', index: true
attr_accessible :body, :course_id, :anonymous, :anonymous_to_peers, :endorsed, :endorsement
......@@ -59,13 +51,13 @@ class Comment < Content
counter_cache :comment_thread
before_destroy :destroy_children # TODO async
before_destroy :destroy_children
before_create :set_thread_last_activity_at
before_update :set_thread_last_activity_at
before_save :set_sk
def self.hash_tree(nodes)
nodes.map{|node, sub_nodes| node.to_hash.merge("children" => hash_tree(sub_nodes).compact)}
nodes.map { |node, sub_nodes| node.to_hash.merge('children' => hash_tree(sub_nodes).compact) }
end
# This should really go somewhere else, but sticking it here for now. This is
......@@ -76,9 +68,9 @@ class Comment < Content
# actually creates the subtree.
def self.flatten_subtree(x)
if x.is_a? Array
x.flatten.map{|y| self.flatten_subtree(y)}
x.flatten.map { |y| self.flatten_subtree(y) }
elsif x.is_a? Hash
x.to_a.map{|y| self.flatten_subtree(y)}.flatten
x.to_a.map { |y| self.flatten_subtree(y) }.flatten
else
x
end
......@@ -99,17 +91,17 @@ class Comment < Content
self.class.hash_tree(subtree_hash).first
else
as_document.slice(*%w[body course_id endorsed endorsement anonymous anonymous_to_peers created_at updated_at at_position_list])
.merge("id" => _id)
.merge("user_id" => author_id)
.merge("username" => author_username)
.merge("depth" => depth)
.merge("closed" => comment_thread.nil? ? false : comment_thread.closed)
.merge("thread_id" => comment_thread_id)
.merge("parent_id" => parent_ids[-1])
.merge("commentable_id" => comment_thread.nil? ? nil : comment_thread.commentable_id)
.merge("votes" => votes.slice(*%w[count up_count down_count point]))
.merge("abuse_flaggers" => abuse_flaggers)
.merge("type" => "comment")
.merge('id' => _id)
.merge('user_id' => author_id)
.merge('username' => author_username)
.merge('depth' => depth)
.merge('closed' => comment_thread.nil? ? false : comment_thread.closed)
.merge('thread_id' => comment_thread_id)
.merge('parent_id' => parent_ids[-1])
.merge('commentable_id' => comment_thread.nil? ? nil : comment_thread.commentable_id)
.merge('votes' => votes.slice(*%w[count up_count down_count point]))
.merge('abuse_flaggers' => abuse_flaggers)
.merge('type' => 'comment')
end
end
......@@ -155,10 +147,16 @@ class Comment < Content
where(:comment_thread_id.in => thread_ids)
end
private
private
def set_thread_last_activity_at
self.comment_thread.update_attribute(:last_activity_at, Time.now.utc)
end
def set_sk
# this attribute is explicitly write-once
if self.sk.nil?
self.sk = (self.parent_ids.dup << self.id).join("-")
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment