Skip to content
Browse files

Scoped elvolunteer man-hours count to campaigns belonging to logged i…

…n user
  • Loading branch information...
1 parent e27f547 commit 6e21238a9b2da76dea191ec8af6ab2990a330d91 @konklone konklone committed
Showing with 28 additions and 4 deletions.
  1. +7 −3 app/models/user.rb
  2. +1 −1 app/views/layouts/partials/_stats.html.erb
  3. +20 −0 test/unit/user_test.rb
View
10 app/models/user.rb
@@ -32,17 +32,21 @@ def campaign_points(campaign)
end
def campaigns_completed_tasks_count
- campaigns.all.map {|c| c.tasks.completed.count}.sum
+ campaigns.all.map {|campaign| campaign.tasks.completed.count}.sum
end
def campaigns_percent_complete
- completed = campaigns.all.map {|c| c.tasks.completed.count}.sum
+ completed = campaigns.all.map {|campaign| campaign.tasks.completed.count}.sum
runs = campaigns.sum(:runs).to_f
runs <= 0 ? 0 : ((completed / runs) * 100).to_i
end
def campaigns_participants_count
- campaigns.all.map {|c| User.participants_in(c)}.flatten.uniq.size
+ campaigns.all.map {|campaign| User.participants_in campaign}.flatten.uniq.size
+ end
+
+ def campaigns_elapsed_seconds
+ campaigns.all.map {|campaign| campaign.tasks.sum :elapsed_seconds}.sum
end
def manager?
View
2 app/views/layouts/partials/_stats.html.erb
@@ -13,7 +13,7 @@
<p>tasks completed</p>
</li>
<li>
- <span><%= to_hours Task.sum(:elapsed_seconds) %></span>
+ <span><%= to_hours current_user.campaigns_elapsed_seconds %></span>
<p>hours spent on tasks</p>
</li>
</ul>
View
20 test/unit/user_test.rb
@@ -2,6 +2,26 @@
class UserTest < ActiveSupport::TestCase
+ test '#campaigns_elapsed_seconds returns total elapsed seconds of completed tasks for the campaigns belonging to this user' do
+ admin1 = Factory :user
+ admin2 = Factory :user
+ campaign_one = Factory :campaign, :creator => admin1
+ campaign_two = Factory :campaign, :creator => admin1
+ campaign_three = Factory :campaign, :creator => admin2
+ campaign_four = Factory :campaign, :creator => admin2
+ user1 = Factory :user
+ user2 = Factory :user
+
+ Factory :completed_task, :campaign => campaign_one, :user => user1, :elapsed_seconds => 2
+ Factory :completed_task, :campaign => campaign_two, :user => user1, :elapsed_seconds => 2
+ Factory :completed_task, :campaign => campaign_three, :user => user1, :elapsed_seconds => 2
+ Factory :completed_task, :campaign => campaign_three, :user => user2, :elapsed_seconds => 2
+ Factory :completed_task, :campaign => campaign_four, :user => user2, :elapsed_seconds => 2
+
+ assert_equal 4, admin1.campaigns_elapsed_seconds
+ assert_equal 6, admin2.campaigns_elapsed_seconds
+ end
+
test '#campaigns_completed_tasks_count returns number of completed tasks for the campaigns belonging to this user' do
admin1 = Factory :user
admin2 = Factory :user

0 comments on commit 6e21238

Please sign in to comment.
Something went wrong with that request. Please try again.