Commit 003ec648373f02965cae959353227f97f128985c

Authored by Victor Costa
1 parent e8c93b5f

Caching ranking results

Showing 1 changed file with 8 additions and 7 deletions   Show diff stats
lib/proposals_discussion_plugin/api.rb
@@ -4,15 +4,16 @@ class ProposalsDiscussionPlugin::API < Grape::API @@ -4,15 +4,16 @@ class ProposalsDiscussionPlugin::API < Grape::API
4 4
5 get ':id/ranking' do 5 get ':id/ranking' do
6 article = find_article(environment.articles, params[:id]) 6 article = find_article(environment.articles, params[:id])
7 - proposals = article.proposals.map do |proposal|  
8 - effective_support = (proposal.votes_for - proposal.votes_against)/proposal.hits.to_f  
9 - effective_participation = (proposal.votes_for + proposal.votes_against)/proposal.hits.to_f 7 + Rails.cache.fetch("#{article.cache_key}/proposals_ranking", expires_in: 1.hours) do
  8 + proposals = article.proposals.map do |proposal|
  9 + effective_support = (proposal.votes_for - proposal.votes_against)/proposal.hits.to_f
  10 + effective_participation = (proposal.votes_for + proposal.votes_against)/proposal.hits.to_f
10 11
11 - {:id => proposal.id, :abstract => proposal.abstract, :votes_for => proposal.votes_for, :votes_against => proposal.votes_against, :hits => proposal.hits, :effective_support => effective_support, :effective_participation => effective_participation} 12 + {:id => proposal.id, :abstract => proposal.abstract, :votes_for => proposal.votes_for, :votes_against => proposal.votes_against, :hits => proposal.hits, :effective_support => effective_support, :effective_participation => effective_participation}
  13 + end
  14 + proposals = proposals.sort_by { |p| p[:effective_support] }.reverse
  15 + {:proposals => proposals, :updated_at => DateTime.now}
12 end 16 end
13 -  
14 - proposals = proposals.sort_by { |p| p[:effective_support] }.reverse  
15 - {:proposals => proposals, :updated_at => DateTime.now}  
16 end 17 end
17 18
18 end 19 end