Permalink
Browse files

Added a rake task to mark the 20 most popular art pieces with a ranking

  • Loading branch information...
1 parent 8bee525 commit 88666dd46f16c07f18cd3968db7c23951e642e91 @konklone konklone committed Mar 5, 2012
Showing with 22 additions and 3 deletions.
  1. +4 −3 app/models/art.rb
  2. +18 −0 lib/tasks/popular.rake
View
@@ -47,9 +47,10 @@ class Art
field :submitted_at, :type => DateTime
# count of visits, very naive
- field :web_visits
- field :api_visits
- field :total_visits # sum of the previous two, duplicated to make filtering easy
+ field :web_visits, :type => Integer
+ field :api_visits, :type => Integer
+ field :total_visits, :type => Integer # sum of the previous two, duplicated to make filtering easy
+ field :ranking, :type => Integer
index [[:location, Mongo::GEO2D]]
index :commissioned
View
@@ -0,0 +1,18 @@
+namespace :art do
+
+ desc "Rank the top 20 art pieces, by number of views they've received"
+ task :popular => :environment do
+ number = ENV['number'] ? ENV['number'].to_i : 20
+
+ puts "Clearing rankings of existing art..."
+ Art.update_all :ranking => nil
+
+ puts "Ranking the top #{number} pieces of art..."
+ Art.desc(:total_visits).limit(number).each_with_index do |art, i|
+ puts "[#{i+1}] #{art.slug}"
+ art.ranking = i + 1
+ art.save!
+ end
+ end
+
+end

0 comments on commit 88666dd

Please sign in to comment.