Commit 0dc835a3086938e720cac4b3ad3b6e0bbd9ba8f9
1 parent
c124eb8e
Exists in
master
and in
22 other branches
action_tracker: moving activities_count triggers to a lib
(ActionItem3039)
Showing
6 changed files
with
82 additions
and
77 deletions
Show diff stats
config/initializers/noosfero_extensions.rb
| ... | ... | @@ -0,0 +1,21 @@ |
| 1 | +Rails.configuration.to_prepare do | |
| 2 | + ActionTracker::Record.module_eval do | |
| 3 | + extend CacheCounterHelper | |
| 4 | + | |
| 5 | + after_create do |record| | |
| 6 | + update_cache_counter(:activities_count, record.user, 1) | |
| 7 | + if record.target.kind_of?(Organization) | |
| 8 | + update_cache_counter(:activities_count, record.target, 1) | |
| 9 | + end | |
| 10 | + end | |
| 11 | + | |
| 12 | + after_destroy do |record| | |
| 13 | + if record.created_at >= ActionTracker::Record::RECENT_DELAY.days.ago | |
| 14 | + update_cache_counter(:activities_count, record.user, -1) | |
| 15 | + if record.target.kind_of?(Organization) | |
| 16 | + update_cache_counter(:activities_count, record.target, -1) | |
| 17 | + end | |
| 18 | + end | |
| 19 | + end | |
| 20 | + end | |
| 21 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,60 @@ |
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | |
| 2 | + | |
| 3 | +class ActionTrackerExtTest < ActiveSupport::TestCase | |
| 4 | + should 'increase person activities_count on new activity' do | |
| 5 | + person = fast_create(Person) | |
| 6 | + assert_difference person, :activities_count, 1 do | |
| 7 | + ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => fast_create(Profile) | |
| 8 | + person.reload | |
| 9 | + end | |
| 10 | + end | |
| 11 | + | |
| 12 | + should 'decrease person activities_count on activity removal' do | |
| 13 | + person = fast_create(Person) | |
| 14 | + record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => fast_create(Profile) | |
| 15 | + assert_difference person, :activities_count, -1 do | |
| 16 | + record.destroy | |
| 17 | + person.reload | |
| 18 | + end | |
| 19 | + end | |
| 20 | + | |
| 21 | + should 'not decrease person activities_count on activity removal after the recent delay' do | |
| 22 | + person = fast_create(Person) | |
| 23 | + record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => fast_create(Profile) | |
| 24 | + record.created_at = record.created_at - ActionTracker::Record::RECENT_DELAY.days - 1.day | |
| 25 | + record.save! | |
| 26 | + assert_no_difference person, :activities_count do | |
| 27 | + record.destroy | |
| 28 | + person.reload | |
| 29 | + end | |
| 30 | + end | |
| 31 | + | |
| 32 | + should 'increase organization activities_count on new activity' do | |
| 33 | + person = fast_create(Person) | |
| 34 | + organization = fast_create(Organization) | |
| 35 | + assert_difference organization, :activities_count, 1 do | |
| 36 | + ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => organization | |
| 37 | + organization.reload | |
| 38 | + end | |
| 39 | + end | |
| 40 | + | |
| 41 | + should 'decrease organization activities_count on activity removal' do | |
| 42 | + person = fast_create(Person) | |
| 43 | + organization = fast_create(Organization) | |
| 44 | + record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => organization | |
| 45 | + assert_difference organization, :activities_count, -1 do | |
| 46 | + record.destroy | |
| 47 | + organization.reload | |
| 48 | + end | |
| 49 | + end | |
| 50 | + | |
| 51 | + should 'not decrease organization activities_count on activity removal after the recent delay' do | |
| 52 | + person = fast_create(Person) | |
| 53 | + organization = fast_create(Organization) | |
| 54 | + record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => organization, :created_at => (ActionTracker::Record::RECENT_DELAY + 1).days.ago | |
| 55 | + assert_no_difference organization, :activities_count do | |
| 56 | + record.destroy | |
| 57 | + organization.reload | |
| 58 | + end | |
| 59 | + end | |
| 60 | +end | ... | ... |
test/unit/organization_test.rb
| ... | ... | @@ -406,35 +406,6 @@ class OrganizationTest < ActiveSupport::TestCase |
| 406 | 406 | assert !organization.visible |
| 407 | 407 | end |
| 408 | 408 | |
| 409 | - should 'increase activities_count on new activity' do | |
| 410 | - person = fast_create(Person) | |
| 411 | - organization = fast_create(Organization) | |
| 412 | - assert_difference organization, :activities_count, 1 do | |
| 413 | - ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => organization | |
| 414 | - organization.reload | |
| 415 | - end | |
| 416 | - end | |
| 417 | - | |
| 418 | - should 'decrease activities_count on activity removal' do | |
| 419 | - person = fast_create(Person) | |
| 420 | - organization = fast_create(Organization) | |
| 421 | - record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => organization | |
| 422 | - assert_difference organization, :activities_count, -1 do | |
| 423 | - record.destroy | |
| 424 | - organization.reload | |
| 425 | - end | |
| 426 | - end | |
| 427 | - | |
| 428 | - should 'not decrease activities_count on activity removal after the recent delay' do | |
| 429 | - person = fast_create(Person) | |
| 430 | - organization = fast_create(Organization) | |
| 431 | - record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => organization, :created_at => (ActionTracker::Record::RECENT_DELAY + 1).days.ago | |
| 432 | - assert_no_difference organization, :activities_count do | |
| 433 | - record.destroy | |
| 434 | - organization.reload | |
| 435 | - end | |
| 436 | - end | |
| 437 | - | |
| 438 | 409 | should 'increase members_count on new membership' do |
| 439 | 410 | member = fast_create(Person) |
| 440 | 411 | organization = fast_create(Organization) | ... | ... |
test/unit/person_test.rb
| ... | ... | @@ -1428,33 +1428,4 @@ class PersonTest < ActiveSupport::TestCase |
| 1428 | 1428 | person.reload |
| 1429 | 1429 | end |
| 1430 | 1430 | end |
| 1431 | - | |
| 1432 | - should 'increase activities_count on new activity' do | |
| 1433 | - person = fast_create(Person) | |
| 1434 | - assert_difference person, :activities_count, 1 do | |
| 1435 | - ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => fast_create(Profile) | |
| 1436 | - person.reload | |
| 1437 | - end | |
| 1438 | - end | |
| 1439 | - | |
| 1440 | - should 'decrease activities_count on activity removal' do | |
| 1441 | - person = fast_create(Person) | |
| 1442 | - record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => fast_create(Profile) | |
| 1443 | - assert_difference person, :activities_count, -1 do | |
| 1444 | - record.destroy | |
| 1445 | - person.reload | |
| 1446 | - end | |
| 1447 | - end | |
| 1448 | - | |
| 1449 | - should 'not decrease activities_count on activity removal after the recent delay' do | |
| 1450 | - person = fast_create(Person) | |
| 1451 | - record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => fast_create(Profile) | |
| 1452 | - record.created_at = record.created_at - ActionTracker::Record::RECENT_DELAY.days - 1.day | |
| 1453 | - record.save! | |
| 1454 | - assert_no_difference person, :activities_count do | |
| 1455 | - record.destroy | |
| 1456 | - person.reload | |
| 1457 | - end | |
| 1458 | - end | |
| 1459 | - | |
| 1460 | 1431 | end | ... | ... |
vendor/plugins/action_tracker/lib/action_tracker_model.rb
| 1 | 1 | module ActionTracker |
| 2 | 2 | class Record < ActiveRecord::Base |
| 3 | - | |
| 4 | - extend CacheCounterHelper | |
| 5 | - | |
| 6 | 3 | set_table_name 'action_tracker' |
| 7 | 4 | |
| 8 | 5 | belongs_to :user, :polymorphic => true |
| ... | ... | @@ -16,22 +13,6 @@ module ActionTracker |
| 16 | 13 | validates_presence_of :user |
| 17 | 14 | validate :user_existence |
| 18 | 15 | |
| 19 | - after_create do |record| | |
| 20 | - update_cache_counter(:activities_count, record.user, 1) | |
| 21 | - if record.target.kind_of?(Organization) | |
| 22 | - update_cache_counter(:activities_count, record.target, 1) | |
| 23 | - end | |
| 24 | - end | |
| 25 | - | |
| 26 | - after_destroy do |record| | |
| 27 | - if record.created_at >= RECENT_DELAY.days.ago | |
| 28 | - update_cache_counter(:activities_count, record.user, -1) | |
| 29 | - if record.target.kind_of?(Organization) | |
| 30 | - update_cache_counter(:activities_count, record.target, -1) | |
| 31 | - end | |
| 32 | - end | |
| 33 | - end | |
| 34 | - | |
| 35 | 16 | def user_existence |
| 36 | 17 | errors.add(:user, "user doesn't exists") if user && !user.class.exists?(user) |
| 37 | 18 | end | ... | ... |