myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [2429] trunk: added site viewings/downloads count


From: noreply
Subject: [myexperiment-hackers] [2429] trunk: added site viewings/downloads counters
Date: Wed, 9 Jun 2010 01:11:21 -0400 (EDT)

Revision
2429
Author
dgc
Date
2010-06-09 01:11:21 -0400 (Wed, 09 Jun 2010)

Log Message

added site viewings/downloads counters

Modified Paths

Diff

Modified: trunk/Rakefile (2428 => 2429)


--- trunk/Rakefile	2010-06-08 13:00:36 UTC (rev 2428)
+++ trunk/Rakefile	2010-06-09 05:11:21 UTC (rev 2429)
@@ -23,6 +23,9 @@
 task "myexp:refresh:contributions" do
   require File.dirname(__FILE__) + '/config/environment'
 
+  all_viewings = Viewing.find(:all, :conditions => "accessed_from_site = 1").group_by do |v| v.contribution_id end
+  all_downloads = Download.find(:all, :conditions => "accessed_from_site = 1").group_by do |v| v.contribution_id end
+
   Contribution.find(:all).each do |c|
     c.contributable.update_contribution_rank
     c.contributable.update_contribution_rating
@@ -34,6 +37,9 @@
     c.update_attribute(:created_at, c.contributable.created_at)
     c.update_attribute(:updated_at, c.contributable.updated_at)
 
+    c.update_attribute(:site_viewings_count,  all_viewings[c.id]  ? all_viewings[c.id].length  : 0)
+    c.update_attribute(:site_downloads_count, all_downloads[c.id] ? all_downloads[c.id].length : 0)
+
     ActiveRecord::Base.record_timestamps = true
   end
 end

Modified: trunk/app/models/download.rb (2428 => 2429)


--- trunk/app/models/download.rb	2010-06-08 13:00:36 UTC (rev 2428)
+++ trunk/app/models/download.rb	2010-06-09 05:11:21 UTC (rev 2429)
@@ -1,12 +1,17 @@
 class Download < ActiveRecord::Base
-  belongs_to :contribution,
-             :counter_cache => true
+
+  belongs_to :contribution
+  belongs_to :user
              
-  belongs_to :user,
-             :counter_cache => true
-             
   validates_presence_of :contribution
   
+  after_save { |download|
+    Contribution.increment_counter(:downloads_count,      download.contribution.id)
+    Contribution.increment_counter(:site_downloads_count, download.contribution.id) if download.accessed_from_site
+
+    User.increment_counter(:downloads_count, download.user.id) if download.user
+  }
+
   # returns the 'most recent' Downloads #after# a given time
   # the maximum number of results is set by #limit#
   def self.most_recent(after=(Time.now - 3.hours), limit=10)

Modified: trunk/app/models/viewing.rb (2428 => 2429)


--- trunk/app/models/viewing.rb	2010-06-08 13:00:36 UTC (rev 2428)
+++ trunk/app/models/viewing.rb	2010-06-09 05:11:21 UTC (rev 2429)
@@ -1,12 +1,17 @@
 class Viewing < ActiveRecord::Base
-  belongs_to :contribution,
-             :counter_cache => true
+
+  belongs_to :contribution
+  belongs_to :user
              
-  belongs_to :user,
-             :counter_cache => true
-             
   validates_presence_of :contribution
   
+  after_save { |viewing|
+    Contribution.increment_counter(:viewings_count,      viewing.contribution.id)
+    Contribution.increment_counter(:site_viewings_count, viewing.contribution.id) if viewing.accessed_from_site
+
+    User.increment_counter(:viewings_count, viewing.user.id) if viewing.user
+  }
+
   # returns the 'most recent' Viewings #after# a given time
   # the maximum number of results is set by #limit#
   def self.most_recent(after=(Time.now - 3.hours), limit=10)

Modified: trunk/config/base_schema.xml (2428 => 2429)


--- trunk/config/base_schema.xml	2010-06-08 13:00:36 UTC (rev 2428)
+++ trunk/config/base_schema.xml	2010-06-09 05:11:21 UTC (rev 2429)
@@ -10,8 +10,10 @@
     <column type="integer"  name="policy_id"/>
     <column type="datetime" name="created_at"/>
     <column type="datetime" name="updated_at"/>
-    <column type="integer"  name="downloads_count" default="0"/>
-    <column type="integer"  name="viewings_count"  default="0"/>
+    <column type="integer"  name="downloads_count"      default="0"/>
+    <column type="integer"  name="viewings_count"       default="0"/>
+    <column type="integer"  name="site_downloads_count" default="0"/>
+    <column type="integer"  name="site_viewings_count"  default="0"/>
     <column type="float"    name="rating"/>
     <column type="float"    name="rank"/>
     <column type="string"   name="label"/>

reply via email to

[Prev in Thread] Current Thread [Next in Thread]