Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added port for Posting#top

  • Loading branch information...
commit 9a36ab1dd01e579afd9cbf97f0a3183abad4aa59 1 parent 367c98a
@useruby useruby authored
View
34 lib/posting.rb
@@ -46,27 +46,21 @@ def self.unfetched
# Top posting by watched user activity in last 24 hours
def self.top
- #watched = Avatar.fields(:name => 0).where(:nwx.gt => 0).all
- #watched = watched.map {|x| x.id}
- #pids = Comment.collection.group(
- # ['pid'],
- # {
- # 'posted_at' => {'$gte' => Time.now - 2.day},
- # 'avatar_id' => {'$in' => watched}
- # },
- # {:cmts => 0.0},
- # "function(d, p) { p.cmts++ }"
- #)
- #pids.sort! { |a, b| b['cmts'] <=> a['cmts']}
- #query_pids = pids.map {|x| x['pid']}
- #query = Posting.where(:valid.ne => false, :pid => {'$in' => query_pids})
- #res = query.sort(:updated_at.desc).limit(21).all
- #pidmap = {}
- #res.each {|posting| pidmap[posting.pid] = posting}
+ watched = Avatar.where('nwx > 0').all
+ watched = watched.map {|x| x.id}
+
+ two_days_before = (Time.now - 2.day).strftime '%Y-%m-%d'
+ pids = Comment.select('pid, count(*) as cmts').where("posted_at > #{two_days_before} and avatar_id in (#{watched.join(',')})").group(:pid).all
+
+ pids.sort! { |a, b| b['cmts'] <=> a['cmts']}
+ query_pids = pids.map {|x| x['pid']}
+ res = Posting.where("pid in (#{query_pids.join(',')}) and valid <> 0").order('updated_at desc').limit(21).all
+ pidmap = {}
+ res.each {|posting| pidmap[posting.pid] = posting}
- #sorted = []
- #pids.each { |pinfo| sorted << [pidmap[pinfo['pid']], pinfo['cmts'].to_i] }
- #sorted
+ sorted = []
+ pids.each { |pinfo| sorted << [pidmap[pinfo['pid']], pinfo['cmts'].to_i] }
+ sorted
end
end
View
26 spec/factories/comments.rb
@@ -3,7 +3,31 @@
pid 1
cid 1
text 'Test Comment'
- association :avatar, :factory => :patio11_avatar
+ avatar {|avatar| Avatar.find_by_name('patio11') || Factory.create(:patio11_avatar)}
+ end
+
+ factory :comment_cid_3, :class => Comment do
+ pid 1
+ cid 3
+ text 'Test Comment 3'
+ avatar {|avatar| avatar = Avatar.find_by_name('patio11') || Factory.create(:patio11_avatar)}
+ posted_at {|posted_at| posted_at = Time.now}
+ end
+
+ factory :comment_cid_4, :class => Comment do
+ pid 1
+ cid 4
+ text 'Test Comment 4'
+ avatar {|avatar| avatar = Avatar.find_by_name('patio11') || Factory.create(:patio11_avatar)}
+ posted_at {|posted_at| posted_at = Time.now}
+ end
+
+ factory :comment_cid_5, :class => Comment do
+ pid 1
+ cid 5
+ text 'Test Comment 5'
+ avatar {|avatar| avatar = Avatar.find_by_name('patio11') || Factory.create(:patio11_avatar)}
+ posted_at {|posted_at| posted_at = Time.now}
end
end
View
6 spec/models/posting_spec.rb
@@ -3,6 +3,10 @@
describe Posting do
before :all do
@post_pid_1 = Factory :post_pid_1
+
+ [3,4,5].each do |cid|
+ Factory "comment_cid_#{cid}".to_sym
+ end
end
describe '#add' do
@@ -26,7 +30,7 @@
end
end
- describe '#top' do
+ describe '#top', :wip => true do
it 'should return top posting by watched user activity in last 24 hours' do
Posting.top.should_not be_empty
end
Please sign in to comment.
Something went wrong with that request. Please try again.