Fix incoming links dashboard data when topic record can't be found

This commit is contained in:
Neil Lalonde 2013-05-01 19:10:31 -04:00
parent 38ed86d0c5
commit 3c7afeb233
2 changed files with 9 additions and 7 deletions

View File

@ -39,7 +39,7 @@ class IncomingLinksReport
num_visits.keys.each do |username|
report.data << {username: username, num_visits: num_visits[username], num_topics: num_topics[username]}
end
report.data.sort_by! {|x| x[:num_visits]}.reverse![0,10]
report.data = report.data.sort_by {|x| x[:num_visits]}.reverse[0,10]
end
def self.per_user
@ -68,7 +68,7 @@ class IncomingLinksReport
num_visits.keys.each do |domain|
report.data << {domain: domain, num_visits: num_visits[domain], num_topics: num_topics[domain], num_users: num_users[domain]}
end
report.data.sort_by! {|x| x[:num_visits]}.reverse![0,10]
report.data = report.data.sort_by {|x| x[:num_visits]}.reverse[0,10]
end
def self.per_domain
@ -93,12 +93,14 @@ class IncomingLinksReport
num_visits = link_count_per_topic
num_visits = num_visits.to_a.sort_by {|x| x[1]}.last(10).reverse # take the top 10
report.data = []
topics = Topic.select('id, slug, title').find(num_visits.map {|z| z[0]})
topics = Topic.select('id, slug, title').where('id in (?)', num_visits.map {|z| z[0]}).all
num_visits.each do |topic_id, num_visits|
topic = topics.find {|t| t.id == topic_id}
report.data << {topic_id: topic_id, topic_title: topic.title, topic_slug: topic.slug, num_visits: num_visits}
if topic
report.data << {topic_id: topic_id, topic_title: topic.title, topic_slug: topic.slug, num_visits: num_visits}
end
end
report.data.sort_by! {|x| x[:num_visits]}.reverse![0,10]
report.data
end
def self.link_count_per_topic

View File

@ -88,8 +88,8 @@ describe IncomingLinksReport do
it 'with some IncomingLink records, it returns correct data' do
topic1 = Fabricate.build(:topic, id: 123); topic2 = Fabricate.build(:topic, id: 234)
IncomingLinksReport.stubs(:link_count_per_topic).returns({topic1.id => 8, topic2.id => 3})
Topic.stubs(:select).returns(Topic) # bypass the select method
Topic.stubs(:find).returns([topic1, topic2])
Topic.stubs(:select).returns(Topic); Topic.stubs(:where).returns(Topic) # bypass some activerecord methods
Topic.stubs(:all).returns([topic1, topic2])
top_referred_topics[:data][0].should == {topic_id: topic1.id, topic_title: topic1.title, topic_slug: topic1.slug, num_visits: 8 }
top_referred_topics[:data][1].should == {topic_id: topic2.id, topic_title: topic2.title, topic_slug: topic2.slug, num_visits: 3 }
end