Commit af13f3d6 by Clinton Blackburn

Cleaned Content model

- Removed dead code
- Made set_username private
- Replaced double quotes with single quotes
parent 18cbbd31
class Content class Content
include Mongoid::Document include Mongoid::Document
include Mongo::Voteable include Mongo::Voteable
field :visible, type: Boolean, default: true field :visible, type: Boolean, default: true
field :abuse_flaggers, type: Array, default: [] field :abuse_flaggers, type: Array, default: []
field :historical_abuse_flaggers, type: Array, default: [] #preserve abuse flaggers after a moderator unflags field :historical_abuse_flaggers, type: Array, default: [] #preserve abuse flaggers after a moderator unflags
field :author_username, type: String, default: nil field :author_username, type: String, default: nil
index({_type: 1, course_id: 1, pinned: -1, created_at: -1 }, {background: true} ) index({_type: 1, course_id: 1, pinned: -1, created_at: -1}, {background: true})
index({_type: 1, course_id: 1, pinned: -1, comment_count: -1, created_at: -1}, {background: true}) index({_type: 1, course_id: 1, pinned: -1, comment_count: -1, created_at: -1}, {background: true})
index({_type: 1, course_id: 1, pinned: -1, "votes.point" => -1, created_at: -1}, {background: true}) index({_type: 1, course_id: 1, pinned: -1, 'votes.point' => -1, created_at: -1}, {background: true})
index({_type: 1, course_id: 1, pinned: -1, last_activity_at: -1, created_at: -1}, {background: true}) index({_type: 1, course_id: 1, pinned: -1, last_activity_at: -1, created_at: -1}, {background: true})
# To be added during Mongo Mania
# index({_type: -1, course_id: 1, pinned: -1, created_at: -1 }, {background: true} )
# index({_type: -1, course_id: 1, pinned: -1, comment_count: -1, created_at: -1}, {background: true})
# index({_type: -1, course_id: 1, pinned: -1, "votes.point" => -1, created_at: -1}, {background: true})
# index({_type: -1, course_id: 1, pinned: -1, last_activity_at: -1, created_at: -1}, {background: true})
index({comment_thread_id: 1, sk: 1}, {sparse: true}) index({comment_thread_id: 1, sk: 1}, {sparse: true})
index({comment_thread_id: 1, endorsed: 1}, {sparse: true}) index({comment_thread_id: 1, endorsed: 1}, {sparse: true})
index({commentable_id: 1}, {sparse: true, background: true}) index({commentable_id: 1}, {sparse: true, background: true})
...@@ -36,10 +27,7 @@ class Content ...@@ -36,10 +27,7 @@ class Content
end end
before_save :set_username before_save :set_username
def set_username
# avoid having to look this attribute up later, since it does not change
self.author_username = author.username
end
def author_with_anonymity(attr=nil, attr_when_anonymous=nil) def author_with_anonymity(attr=nil, attr_when_anonymous=nil)
if not attr if not attr
...@@ -52,7 +40,7 @@ class Content ...@@ -52,7 +40,7 @@ class Content
def self.flagged def self.flagged
#return an array of flagged content #return an array of flagged content
holder = [] holder = []
Content.where(:abuse_flaggers.ne => [],:abuse_flaggers.exists => true).each do |c| Content.where(:abuse_flaggers.ne => [], :abuse_flaggers.exists => true).each do |c|
holder << c holder << c
end end
holder holder
...@@ -62,61 +50,65 @@ class Content ...@@ -62,61 +50,65 @@ class Content
#take a hash of criteria (what) and return a hash of hashes #take a hash of criteria (what) and return a hash of hashes
#course => user => count #course => user => count
contributors = {} map = 'function(){emit(this.author_id,1)}'
reduce = 'function(k, vals) { var sum = 0; for(var i in vals) sum += vals[i]; return sum; }'
map = "function(){emit(this.author_id,1)}"
reduce = "function(k, vals) { var sum = 0; for(var i in vals) sum += vals[i]; return sum; }"
contributors = [] contributors = []
self.where(what).map_reduce(map,reduce).out(replace: "results").each do |d| self.where(what).map_reduce(map, reduce).out(replace: 'results').each do |d|
contributors << d contributors << d
end end
#now sort and limit them #now sort and limit them
#first sort destructively #first sort destructively
contributors.sort! {|a,b| -a["value"] <=> -b["value"]} contributors.sort! { |a, b| -a['value'] <=> -b['value'] }
#then trim it #then trim it
contributors = contributors[0..(count - 1)] contributors = contributors[0..(count - 1)]
contributors contributors
end end
def self.summary what def self.summary what
#take a hash of criteria (what) and return a hash of hashes #take a hash of criteria (what) and return a hash of hashes
#of total users, votes, comments, endorsements, #of total users, votes, comments, endorsements,
answer = {} answer = {}
vote_count = 0 vote_count = 0
thread_count = 0 thread_count = 0
comment_count = 0 comment_count = 0
contributors = [] contributors = []
content = self.where(what) content = self.where(what)
content.each do |c| content.each do |c|
contributors << c.author_id contributors << c.author_id
contributors << c["votes"]["up"] contributors << c['votes']['up']
contributors << c["votes"]["down"] contributors << c['votes']['down']
vote_count += c["votes"]["count"] vote_count += c['votes']['count']
if c._type == "CommentThread" if c._type == 'CommentThread'
thread_count += 1 thread_count += 1
elsif c._type == "Comment" elsif c._type == 'Comment'
comment_count += 1 comment_count += 1
end end
end end
#uniquify contributors #uniquify contributors
contributors = contributors.uniq contributors = contributors.uniq
#assemble the answer and ship #assemble the answer and ship
answer["vote_count"] = vote_count answer['vote_count'] = vote_count
answer["thread_count"] = thread_count answer['thread_count'] = thread_count
answer["comment_count"] = comment_count answer['comment_count'] = comment_count
answer["contributor_count"] = contributors.count answer['contributor_count'] = contributors.count
answer answer
end end
private
def set_username
# avoid having to look this attribute up later, since it does not change
self.author_username = author.username
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