Commit 16f70434a650efc21b1603f004670bf5faeded4d

Authored by Braulio Bhavamitra
2 parents 96e1744b 4e0ad2ae

Merge branch 'profile-activities' into noosfero

app/mailers/scrap_notifier.rb
1 1 class ScrapNotifier < ActionMailer::Base
2 2 def notification(scrap)
3 3 sender, receiver = scrap.sender, scrap.receiver
  4 + # for tests
  5 + return unless receiver.email
  6 +
4 7 @recipient = receiver.name
5 8 @sender = sender.name
6 9 @sender_link = sender.url
... ...
app/models/community.rb
... ... @@ -86,8 +86,8 @@ class Community &lt; Organization
86 86 {:title => _('Community Info and settings'), :icon => 'edit-profile-group'}
87 87 end
88 88  
89   - def activities
90   - Scrap.find_by_sql("SELECT id, updated_at, '#{Scrap.to_s}' AS klass FROM #{Scrap.table_name} WHERE scraps.receiver_id = #{self.id} AND scraps.scrap_id IS NULL UNION SELECT id, updated_at, '#{ActionTracker::Record.to_s}' AS klass FROM #{ActionTracker::Record.table_name} WHERE action_tracker.target_id = #{self.id} and action_tracker.verb != 'join_community' and action_tracker.verb != 'leave_scrap' UNION SELECT at.id, at.updated_at, '#{ActionTracker::Record.to_s}' AS klass FROM #{ActionTracker::Record.table_name} at INNER JOIN articles a ON at.target_id = a.id WHERE a.profile_id = #{self.id} AND at.target_type = 'Article' ORDER BY updated_at DESC")
  89 + def exclude_verbs_on_activities
  90 + %w[join_community leave_scrap]
91 91 end
92 92  
93 93 end
... ...
app/models/enterprise.rb
... ... @@ -194,10 +194,6 @@ class Enterprise &lt; Organization
194 194 true
195 195 end
196 196  
197   - def activities
198   - Scrap.find_by_sql("SELECT id, updated_at, 'Scrap' AS klass FROM scraps WHERE scraps.receiver_id = #{self.id} AND scraps.scrap_id IS NULL UNION SELECT id, updated_at, 'ActionTracker::Record' AS klass FROM action_tracker WHERE action_tracker.target_id = #{self.id} UNION SELECT action_tracker.id, action_tracker.updated_at, 'ActionTracker::Record' AS klass FROM action_tracker INNER JOIN articles ON action_tracker.target_id = articles.id WHERE articles.profile_id = #{self.id} AND action_tracker.target_type = 'Article' ORDER BY updated_at DESC")
199   - end
200   -
201 197 def catalog_url
202 198 { :profile => identifier, :controller => 'catalog'}
203 199 end
... ...
app/models/person.rb
... ... @@ -496,8 +496,8 @@ roles] }
496 496 user.save!
497 497 end
498 498  
499   - def activities
500   - Scrap.find_by_sql("SELECT id, updated_at, '#{Scrap.to_s}' AS klass FROM #{Scrap.table_name} WHERE scraps.receiver_id = #{self.id} AND scraps.scrap_id IS NULL UNION SELECT id, updated_at, '#{ActionTracker::Record.to_s}' AS klass FROM #{ActionTracker::Record.table_name} WHERE action_tracker.user_id = #{self.id} and action_tracker.verb != 'leave_scrap_to_self' and action_tracker.verb != 'add_member_in_community' and action_tracker.verb != 'reply_scrap_on_self' ORDER BY updated_at DESC")
  499 + def exclude_verbs_on_activities
  500 + %w[leave_scrap_to_self add_member_in_community reply_scrap_on_self]
501 501 end
502 502  
503 503 # by default, all fields are private
... ...
app/models/profile.rb
... ... @@ -146,6 +146,7 @@ class Profile &lt; ActiveRecord::Base
146 146  
147 147 acts_as_trackable :dependent => :destroy
148 148  
  149 + has_many :profile_activities
149 150 has_many :action_tracker_notifications, :foreign_key => 'profile_id'
150 151 has_many :tracked_notifications, :through => :action_tracker_notifications, :source => :action_tracker, :order => 'updated_at DESC'
151 152 has_many :scraps_received, :class_name => 'Scrap', :foreign_key => :receiver_id, :order => "updated_at DESC", :dependent => :destroy
... ... @@ -976,9 +977,13 @@ private :generate_url, :url_options
976 977 name
977 978 end
978 979  
979   - # Override in your subclasses
  980 + def exclude_verbs_on_activities
  981 + %w[]
  982 + end
  983 +
  984 + # Customize in subclasses
980 985 def activities
981   - []
  986 + self.profile_activities.includes(:activity).order('updated_at DESC')
982 987 end
983 988  
984 989 def may_display_field_to? field, user = nil
... ...
app/models/profile_activity.rb 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +class ProfileActivity < ActiveRecord::Base
  2 +
  3 + self.record_timestamps = false
  4 +
  5 + attr_accessible :profile_id,
  6 + :profile, :activity
  7 +
  8 + belongs_to :profile
  9 + belongs_to :activity, polymorphic: true
  10 +
  11 + # non polymorphic versions
  12 + belongs_to :scrap, foreign_key: :activity_id, class_name: 'Scrap', conditions: {profile_activities: {activity_type: 'Scrap'}}
  13 + belongs_to :action_tracker, foreign_key: :activity_id, class_name: 'ActionTracker::Record', conditions: {profile_activities: {activity_type: 'ActionTracker::Record'}}
  14 +
  15 + before_validation :copy_timestamps
  16 +
  17 + def self.update_activity activity
  18 + profile_activity = ProfileActivity.where(activity_id: activity.id, activity_type: activity.class.base_class.name).first
  19 + profile_activity.send :copy_timestamps
  20 + profile_activity.save!
  21 + profile_activity
  22 + end
  23 +
  24 + protected
  25 +
  26 + def copy_timestamps
  27 + self.created_at = self.activity.created_at
  28 + self.updated_at = self.activity.updated_at
  29 + end
  30 +
  31 +end
... ...
app/models/scrap.rb
... ... @@ -13,6 +13,11 @@ class Scrap &lt; ActiveRecord::Base
13 13 has_many :replies, :class_name => 'Scrap', :foreign_key => 'scrap_id', :dependent => :destroy
14 14 belongs_to :root, :class_name => 'Scrap', :foreign_key => 'scrap_id'
15 15  
  16 + has_many :profile_activities, foreign_key: :activity_id, conditions: {profile_activities: {activity_type: 'Scrap'}}, dependent: :destroy
  17 +
  18 + after_create :create_activity
  19 + after_update :update_activity
  20 +
16 21 scope :all_scraps, lambda {|profile| {:conditions => ["receiver_id = ? OR sender_id = ?", profile, profile], :limit => 30}}
17 22  
18 23 scope :not_replies, :conditions => {:scrap_id => nil}
... ... @@ -23,10 +28,7 @@ class Scrap &lt; ActiveRecord::Base
23 28  
24 29 track_actions :reply_scrap_on_self, :after_create, :keep_params => ['sender.name', 'content'], :if => Proc.new{|s| s.sender != s.receiver && s.sender == s.top_root.receiver}
25 30  
26   - after_create do |scrap|
27   - scrap.root.update_attribute('updated_at', DateTime.now) unless scrap.root.nil?
28   - ScrapNotifier.notification(scrap).deliver if scrap.send_notification?
29   - end
  31 + after_create :send_notification
30 32  
31 33 before_validation :strip_all_html_tags
32 34  
... ... @@ -57,4 +59,21 @@ class Scrap &lt; ActiveRecord::Base
57 59 sender != receiver && (is_root? ? root.receiver.receives_scrap_notification? : receiver.receives_scrap_notification?)
58 60 end
59 61  
  62 + protected
  63 +
  64 + def create_activity
  65 + # do not scrap replies (when scrap_id is not nil)
  66 + return if self.scrap_id.present?
  67 + ProfileActivity.create! profile_id: self.receiver_id, activity: self
  68 + end
  69 +
  70 + def update_activity
  71 + ProfileActivity.update_activity self
  72 + end
  73 +
  74 + def send_notification
  75 + self.root.update_attribute('updated_at', DateTime.now) unless self.root.nil?
  76 + ScrapNotifier.notification(self).deliver if self.send_notification?
  77 + end
  78 +
60 79 end
... ...
app/views/profile/_profile_activities_list.html.erb
1 1 <% unless activities.nil? %>
2   - <% activities.each do |a| %>
3   - <% activity = a.klass.constantize.find(a.id) %>
  2 + <% activities.each do |profile_activity| %>
  3 + <% activity = profile_activity.activity %>
4 4 <% if activity.kind_of?(ActionTracker::Record) %>
5 5 <%= render :partial => 'profile_activity', :locals => { :activity => activity, :tab_action => 'wall' } if activity.visible? %>
6 6 <% else %>
... ...
db/migrate/20150216213259_create_profile_activity.rb 0 → 100644
... ... @@ -0,0 +1,26 @@
  1 +class CreateProfileActivity < ActiveRecord::Migration
  2 + def up
  3 + ActiveRecord::Base.transaction do
  4 + create_table :profile_activities do |t|
  5 + t.integer :profile_id
  6 + t.integer :activity_id
  7 + t.string :activity_type
  8 + t.timestamps
  9 + end
  10 + add_index :profile_activities, :profile_id
  11 + add_index :profile_activities, [:activity_id, :activity_type]
  12 + add_index :profile_activities, :activity_type
  13 +
  14 + Scrap.find_each batch_size: 50 do |scrap|
  15 + scrap.send :create_activity
  16 + end
  17 + ActionTracker::Record.find_each batch_size: 50 do |action_tracker|
  18 + action_tracker.send :create_activity
  19 + end
  20 + end
  21 + end
  22 +
  23 + def down
  24 + drop_table :profile_activities
  25 + end
  26 +end
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(:version => 20150625234824) do
  14 +ActiveRecord::Schema.define(:version => 20150712130827) do
15 15  
16 16 create_table "abuse_reports", :force => true do |t|
17 17 t.integer "reporter_id"
... ... @@ -489,6 +489,18 @@ ActiveRecord::Schema.define(:version =&gt; 20150625234824) do
489 489 add_index "products", ["product_category_id"], :name => "index_products_on_product_category_id"
490 490 add_index "products", ["profile_id"], :name => "index_products_on_profile_id"
491 491  
  492 + create_table "profile_activities", :force => true do |t|
  493 + t.integer "profile_id"
  494 + t.integer "activity_id"
  495 + t.string "activity_type"
  496 + t.datetime "created_at", :null => false
  497 + t.datetime "updated_at", :null => false
  498 + end
  499 +
  500 + add_index "profile_activities", ["activity_id", "activity_type"], :name => "index_profile_activities_on_activity_id_and_activity_type"
  501 + add_index "profile_activities", ["activity_type"], :name => "index_profile_activities_on_activity_type"
  502 + add_index "profile_activities", ["profile_id"], :name => "index_profile_activities_on_profile_id"
  503 +
492 504 create_table "profile_suggestions", :force => true do |t|
493 505 t.integer "person_id"
494 506 t.integer "suggestion_id"
... ... @@ -555,6 +567,8 @@ ActiveRecord::Schema.define(:version =&gt; 20150625234824) do
555 567 add_index "profiles", ["identifier"], :name => "index_profiles_on_identifier"
556 568 add_index "profiles", ["members_count"], :name => "index_profiles_on_members_count"
557 569 add_index "profiles", ["region_id"], :name => "index_profiles_on_region_id"
  570 + add_index "profiles", ["user_id", "type"], :name => "index_profiles_on_user_id_and_type"
  571 + add_index "profiles", ["user_id"], :name => "index_profiles_on_user_id"
558 572  
559 573 create_table "qualifier_certifiers", :force => true do |t|
560 574 t.integer "qualifier_id"
... ...
lib/noosfero/action_tracker_ext.rb
1 1 Rails.configuration.to_prepare do
2   - ActionTracker::Record.module_eval do
  2 + ActionTracker::Record.class_eval do
3 3 extend CacheCounterHelper
4 4  
5 5 after_create do |record|
... ... @@ -9,6 +9,11 @@ Rails.configuration.to_prepare do
9 9 end
10 10 end
11 11  
  12 + has_many :profile_activities, foreign_key: :activity_id, conditions: {profile_activities: {activity_type: 'ActionTracker::Record'}}, dependent: :destroy
  13 +
  14 + after_create :create_activity
  15 + after_update :update_activity
  16 +
12 17 after_destroy do |record|
13 18 if record.created_at >= ActionTracker::Record::RECENT_DELAY.days.ago
14 19 ActionTracker::Record.update_cache_counter(:activities_count, record.user, -1)
... ... @@ -17,5 +22,17 @@ Rails.configuration.to_prepare do
17 22 end
18 23 end
19 24 end
  25 +
  26 + protected
  27 +
  28 + def create_activity
  29 + target = if self.target.is_a? Profile then self.target else self.target.profile rescue self.user end
  30 + return if self.verb.in? target.exclude_verbs_on_activities
  31 + ProfileActivity.create! profile: target, activity: self
  32 + end
  33 + def update_activity
  34 + ProfileActivity.update_activity self
  35 + end
  36 +
20 37 end
21 38 end
... ...
test/functional/profile_controller_test.rb
... ... @@ -724,7 +724,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
724 724 40.times{create(Scrap, defaults_for_scrap(:sender => p1, :receiver => p1))}
725 725 login_as(p1.identifier)
726 726 get :index, :profile => p1.identifier
727   - assert_equal 15, assigns(:activities).count
  727 + assert_equal 15, assigns(:activities).size
728 728 end
729 729  
730 730 should 'not see the friends activities in the current profile' do
... ... @@ -747,7 +747,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
747 747 login_as(profile.identifier)
748 748 get :index, :profile => p3.identifier
749 749 assert_not_nil assigns(:activities)
750   - assert_equivalent [scrap1, article1.activity], assigns(:activities).map { |a| a.klass.constantize.find(a.id) }
  750 + assert_equivalent [scrap1, article1.activity], assigns(:activities).map(&:activity)
751 751 end
752 752  
753 753 should 'see all the activities in the current profile network' do
... ... @@ -759,18 +759,17 @@ class ProfileControllerTest &lt; ActionController::TestCase
759 759 p3.add_friend(p1)
760 760 p1.add_friend(p3)
761 761  
762   - ActionTracker::Record.delete_all
  762 + ActionTracker::Record.destroy_all
763 763  
764 764 User.current = p1.user
765   - create(Scrap,defaults_for_scrap(:sender => p1, :receiver => p1))
  765 + create(Scrap, sender: p1, receiver: p1)
766 766 a1 = ActionTracker::Record.last
767 767  
768 768 User.current = p2.user
769   - create(Scrap, defaults_for_scrap(:sender => p2, :receiver => p3))
770   - a2 = ActionTracker::Record.last
  769 + create(Scrap, sender: p2, receiver: p3)
771 770  
772 771 User.current = p3.user
773   - create(Scrap, defaults_for_scrap(:sender => p3, :receiver => p1))
  772 + create(Scrap, sender: p3, receiver: p1)
774 773 a3 = ActionTracker::Record.last
775 774  
776 775 process_delayed_job_queue
... ... @@ -933,9 +932,9 @@ class ProfileControllerTest &lt; ActionController::TestCase
933 932 p1 = fast_create(Person)
934 933 p2 = fast_create(Person)
935 934 p3 = fast_create(Person)
936   - s1 = fast_create(Scrap, :sender_id => p1.id, :receiver_id => p2.id)
937   - s2 = fast_create(Scrap, :sender_id => p2.id, :receiver_id => p1.id)
938   - s3 = fast_create(Scrap, :sender_id => p3.id, :receiver_id => p1.id)
  935 + s1 = create(Scrap, :sender_id => p1.id, :receiver_id => p2.id)
  936 + s2 = create(Scrap, :sender_id => p2.id, :receiver_id => p1.id)
  937 + s3 = create(Scrap, :sender_id => p3.id, :receiver_id => p1.id)
939 938  
940 939 @controller.stubs(:logged_in?).returns(true)
941 940 user = mock()
... ... @@ -944,7 +943,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
944 943 @controller.stubs(:current_user).returns(user)
945 944 Person.any_instance.stubs(:follows?).returns(true)
946 945 get :index, :profile => p1.identifier
947   - assert_equal [s2,s3], assigns(:activities)
  946 + assert_equal [s3,s2], assigns(:activities).map(&:activity)
948 947 end
949 948  
950 949 should 'the activities be the received scraps in community profile' do
... ... @@ -952,9 +951,9 @@ class ProfileControllerTest &lt; ActionController::TestCase
952 951 p1 = fast_create(Person)
953 952 p2 = fast_create(Person)
954 953 p3 = fast_create(Person)
955   - s1 = fast_create(Scrap, :sender_id => p1.id, :receiver_id => p2.id)
956   - s2 = fast_create(Scrap, :sender_id => p2.id, :receiver_id => c.id)
957   - s3 = fast_create(Scrap, :sender_id => p3.id, :receiver_id => c.id)
  954 + s1 = create(Scrap, :sender_id => p1.id, :receiver_id => p2.id)
  955 + s2 = create(Scrap, :sender_id => p2.id, :receiver_id => c.id)
  956 + s3 = create(Scrap, :sender_id => p3.id, :receiver_id => c.id)
958 957  
959 958 @controller.stubs(:logged_in?).returns(true)
960 959 user = mock()
... ... @@ -963,12 +962,12 @@ class ProfileControllerTest &lt; ActionController::TestCase
963 962 @controller.stubs(:current_user).returns(user)
964 963 Person.any_instance.stubs(:follows?).returns(true)
965 964 get :index, :profile => c.identifier
966   - assert_equivalent [s2,s3], assigns(:activities)
  965 + assert_equivalent [s2,s3], assigns(:activities).map(&:activity)
967 966 end
968 967  
969 968 should 'the activities be paginated in people profiles' do
970 969 p1= fast_create(Person)
971   - 40.times{fast_create(Scrap, :receiver_id => p1.id, :created_at => Time.now)}
  970 + 40.times{create(Scrap, :receiver_id => p1.id, :created_at => Time.now)}
972 971  
973 972 @controller.stubs(:logged_in?).returns(true)
974 973 user = mock()
... ... @@ -978,13 +977,13 @@ class ProfileControllerTest &lt; ActionController::TestCase
978 977 Person.any_instance.stubs(:follows?).returns(true)
979 978 assert_equal 40, p1.scraps_received.not_replies.count
980 979 get :index, :profile => p1.identifier
981   - assert_equal 15, assigns(:activities).count
  980 + assert_equal 15, assigns(:activities).size
982 981 end
983 982  
984 983 should 'the activities be paginated in community profiles' do
985 984 p1= fast_create(Person)
986 985 c = fast_create(Community)
987   - 40.times{fast_create(Scrap, :receiver_id => c.id)}
  986 + 40.times{create(Scrap, :receiver_id => c.id)}
988 987  
989 988 @controller.stubs(:logged_in?).returns(true)
990 989 user = mock()
... ... @@ -994,7 +993,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
994 993 Person.any_instance.stubs(:follows?).returns(true)
995 994 assert_equal 40, c.scraps_received.not_replies.count
996 995 get :index, :profile => c.identifier
997   - assert_equal 15, assigns(:activities).count
  996 + assert_equal 15, assigns(:activities).size
998 997 end
999 998  
1000 999 should "the owner of activity could remove it" do
... ... @@ -1140,7 +1139,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
1140 1139 get :view_more_activities, :profile => profile.identifier, :page => 2
1141 1140 assert_response :success
1142 1141 assert_template '_profile_activities_list'
1143   - assert_equal 10, assigns(:activities).count
  1142 + assert_equal 10, assigns(:activities).size
1144 1143 end
1145 1144  
1146 1145 should "be logged in to access the view_more_activities action" do
... ... @@ -1193,8 +1192,8 @@ class ProfileControllerTest &lt; ActionController::TestCase
1193 1192 should "not index display scraps replies" do
1194 1193 login_as(profile.identifier)
1195 1194 Scrap.destroy_all
1196   - scrap = fast_create(Scrap, :sender_id => profile.id, :receiver_id => profile.id)
1197   - 20.times {fast_create(Scrap, :sender_id => profile.id, :receiver_id => profile.id, :scrap_id => scrap.id)}
  1195 + scrap = create(Scrap, :sender_id => profile.id, :receiver_id => profile.id)
  1196 + 20.times {create(Scrap, :sender_id => profile.id, :receiver_id => profile.id, :scrap_id => scrap.id)}
1198 1197 profile.reload
1199 1198 get :index, :profile => profile.identifier
1200 1199 assert_tag 'ul', :attributes => {:class => 'profile-wall-activities-comments scrap-replies'}, :children => {:count => 0 }
... ... @@ -1340,7 +1339,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
1340 1339 login_as(profile.identifier)
1341 1340 get :index, :profile => profile.identifier
1342 1341  
1343   - assert_equivalent [scrap,activity], assigns(:activities).map {|a| a.klass.constantize.find(a.id)}
  1342 + assert_equivalent [scrap,activity], assigns(:activities).map(&:activity)
1344 1343 end
1345 1344  
1346 1345 should "be logged in to leave comment on an activity" do
... ...
test/unit/action_tracker_ext_test.rb
1 1 require_relative "../test_helper"
2 2  
3 3 class ActionTrackerExtTest < ActiveSupport::TestCase
  4 +
4 5 should 'increase person activities_count on new activity' do
5 6 person = fast_create(Person)
6 7 assert_difference 'person.activities_count', 1 do
... ... @@ -61,4 +62,12 @@ class ActionTrackerExtTest &lt; ActiveSupport::TestCase
61 62 organization.reload
62 63 end
63 64 end
  65 +
  66 + should 'create profile activity' do
  67 + person = fast_create(Profile)
  68 + organization = fast_create(Enterprise)
  69 + record = create ActionTracker::Record, :verb => :create_product, :user => person, :target => organization
  70 + assert_equal record, organization.activities.first.activity
  71 + end
  72 +
64 73 end
... ...
test/unit/community_test.rb
... ... @@ -368,7 +368,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
368 368 scrap = create(Scrap, defaults_for_scrap(:sender => person, :receiver => community, :content => 'A scrap'))
369 369 activity = ActionTracker::Record.last
370 370  
371   - assert_equal [scrap], community.activities.map { |a| a.klass.constantize.find(a.id) }
  371 + assert_equal [scrap], community.activities.map(&:activity)
372 372 end
373 373  
374 374 should 'return tracked_actions of community as activities' do
... ... @@ -378,7 +378,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
378 378 User.current = person.user
379 379 assert_difference 'ActionTracker::Record.count', 1 do
380 380 article = create(TinyMceArticle, :profile => community, :name => 'An article about free software')
381   - assert_equal [article.activity], community.activities.map { |a| a.klass.constantize.find(a.id) }
  381 + assert_equal [article.activity], community.activities.map(&:activity)
382 382 end
383 383 end
384 384  
... ... @@ -393,12 +393,12 @@ class CommunityTest &lt; ActiveSupport::TestCase
393 393 assert_not_includes community.activities.map { |a| a.klass.constantize.find(a.id) }, article.activity
394 394 end
395 395  
396   -
  396 +
397 397 should 'check if a community admin user is really a community admin' do
398 398 c = fast_create(Community, :name => 'my test profile', :identifier => 'mytestprofile')
399 399 admin = create_user('adminuser').person
400 400 c.add_admin(admin)
401   -
  401 +
402 402 assert c.is_admin?(admin)
403 403 end
404 404  
... ...
test/unit/enterprise_test.rb
... ... @@ -468,7 +468,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
468 468 activity = ActionTracker::Record.last
469 469 scrap = create(Scrap, defaults_for_scrap(:sender => person, :receiver => enterprise, :content => 'A scrap'))
470 470  
471   - assert_equal [scrap], enterprise.activities.map { |a| a.klass.constantize.find(a.id) }
  471 + assert_equal [scrap], enterprise.activities.map(&:activity)
472 472 end
473 473  
474 474 should 'return tracked_actions of community as activities' do
... ... @@ -478,7 +478,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
478 478 User.current = person.user
479 479 article = create(TinyMceArticle, :profile => enterprise, :name => 'An article about free software')
480 480  
481   - assert_equal [article.activity], enterprise.activities.map { |a| a.klass.constantize.find(a.id) }
  481 + assert_equal [article.activity], enterprise.activities.map(&:activity)
482 482 end
483 483  
484 484 should 'not return tracked_actions of other community as activities' do
... ... @@ -489,7 +489,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
489 489 User.current = person.user
490 490 article = create(TinyMceArticle, :profile => enterprise2, :name => 'Another article about free software')
491 491  
492   - assert_not_includes enterprise.activities.map { |a| a.klass.constantize.find(a.id) }, article.activity
  492 + assert_not_includes enterprise.activities.map(&:activity), article.activity
493 493 end
494 494  
495 495 should 'provide URL to catalog area' do
... ... @@ -503,7 +503,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
503 503 c = fast_create(Enterprise, :name => 'my test profile', :identifier => 'mytestprofile')
504 504 admin = create_user('adminuser').person
505 505 c.add_admin(admin)
506   -
  506 +
507 507 assert c.is_admin?(admin)
508 508 end
509 509  
... ...
test/unit/person_notifier_test.rb
... ... @@ -168,7 +168,8 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
168 168 a.verb = verb
169 169 a.user = @member
170 170 a.created_at = @member.notifier.notify_from + 1.day
171   - a.target = fast_create(Forum)
  171 + profile = create(Community)
  172 + a.target = create(Forum, profile: profile)
172 173 a.comments_count = 0
173 174 a.params = {
174 175 'name' => 'home', 'url' => '/', 'lead' => '',
... ...
test/unit/person_test.rb
... ... @@ -916,7 +916,7 @@ class PersonTest &lt; ActiveSupport::TestCase
916 916 refute p2.is_member_of?(community)
917 917 process_delayed_job_queue
918 918  
919   - action_tracker = fast_create(ActionTracker::Record, :verb => 'create_article')
  919 + action_tracker = create(ActionTracker::Record, user: p1, verb: 'create_article')
920 920 action_tracker.target = community
921 921 action_tracker.user = p4
922 922 action_tracker.save!
... ... @@ -1254,7 +1254,7 @@ class PersonTest &lt; ActiveSupport::TestCase
1254 1254 User.current = person.user
1255 1255 article = create(TinyMceArticle, :profile => person, :name => 'An article about free software')
1256 1256  
1257   - assert_equivalent [scrap,article.activity], person.activities.map { |a| a.klass.constantize.find(a.id) }
  1257 + assert_equivalent [scrap,article.activity], person.activities.map { |a| a.activity }
1258 1258 end
1259 1259  
1260 1260 should 'not return tracked_actions and scraps from others as activities' do
... ... @@ -1273,7 +1273,7 @@ class PersonTest &lt; ActiveSupport::TestCase
1273 1273 create(TinyMceArticle, :profile => person, :name => 'An article about free software')
1274 1274 person_activity = ActionTracker::Record.last
1275 1275  
1276   - assert_equivalent [person_scrap,person_activity], person.activities.map { |a| a.klass.constantize.find(a.id) }
  1276 + assert_equivalent [person_scrap,person_activity], person.activities.map { |a| a.activity }
1277 1277 end
1278 1278  
1279 1279 should 'grant every permission over profile for its admin' do
... ...
test/unit/profile_activity_test.rb 0 → 100644
... ... @@ -0,0 +1,21 @@
  1 +require_relative '../test_helper'
  2 +
  3 +class ProfileActivityTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + super
  7 + end
  8 +
  9 + should 'use timestamps from activity' do
  10 + profile = fast_create Person
  11 + target = fast_create Person
  12 +
  13 + ActionTracker::Record.attr_accessible :created_at, :updated_at
  14 + tracker = ActionTracker::Record.create! verb: :leave_scrap, user: profile, target: target, created_at: Time.now-2.days, updated_at: Time.now-1.day
  15 +
  16 + pa = ProfileActivity.create! profile: profile, activity: tracker
  17 + assert_equal pa.created_at, pa.activity.created_at
  18 + assert_equal pa.updated_at, pa.activity.updated_at
  19 + end
  20 +
  21 +end
... ...
test/unit/scrap_notifier_test.rb
... ... @@ -58,7 +58,7 @@ class ScrapNotifierTest &lt; ActiveSupport::TestCase
58 58 should 'not deliver mail if is a reply on a community' do
59 59 community = fast_create(Community)
60 60 person = create_user.person
61   - scrap = fast_create(Scrap, :receiver_id => community.id, :sender_id => @sender.id)
  61 + scrap = create(Scrap, receiver_id: community.id, sender_id: @sender.id)
62 62 assert_no_difference 'ActionMailer::Base.deliveries.size' do
63 63 Scrap.create!(:sender_id => person.id, :receiver_id => @sender.id, :scrap_id => scrap.id, :content => 'Hi myself!')
64 64 end
... ...
test/unit/scrap_test.rb
... ... @@ -3,8 +3,8 @@ require_relative &quot;../test_helper&quot;
3 3 class ScrapTest < ActiveSupport::TestCase
4 4  
5 5 def setup
6   - Person.delete_all
7   - Scrap.delete_all
  6 + Person.destroy_all
  7 + Scrap.destroy_all
8 8 ActionTracker::Record.destroy_all
9 9 end
10 10  
... ... @@ -126,16 +126,16 @@ class ScrapTest &lt; ActiveSupport::TestCase
126 126 p1 = create_user.person
127 127 p2 = create_user.person
128 128 p1.add_friend(p2)
129   - ActionTrackerNotification.delete_all
130   - Delayed::Job.delete_all
  129 + process_delayed_job_queue
131 130 s = Scrap.new
132 131 s.sender= p1
133 132 s.receiver= p2
134 133 s.content = 'some content'
135 134 s.save!
136   - process_delayed_job_queue
137   - assert_equal 2, ActionTrackerNotification.count
138   - ActionTrackerNotification.all.map{|a|a.profile}.map do |profile|
  135 + assert_difference 'ActionTrackerNotification.count', 2 do
  136 + process_delayed_job_queue
  137 + end
  138 + ActionTrackerNotification.all.map(&:profile).map do |profile|
139 139 assert [p1,p2].include?(profile)
140 140 end
141 141 end
... ... @@ -151,7 +151,9 @@ class ScrapTest &lt; ActiveSupport::TestCase
151 151 s.receiver= c
152 152 s.content = 'some content'
153 153 s.save!
154   - process_delayed_job_queue
  154 + assert_difference 'ActionTrackerNotification.count', 2 do
  155 + process_delayed_job_queue
  156 + end
155 157 assert_equal 2, ActionTrackerNotification.count
156 158 ActionTrackerNotification.all.map{|a|a.profile}.map do |profile|
157 159 assert [p,c].include?(profile)
... ... @@ -183,8 +185,9 @@ class ScrapTest &lt; ActiveSupport::TestCase
183 185 s.receiver= p1
184 186 s.content = 'some content'
185 187 s.save!
186   - process_delayed_job_queue
187   - assert_equal 2, ActionTrackerNotification.count
  188 + assert_difference 'ActionTrackerNotification.count', 2 do
  189 + process_delayed_job_queue
  190 + end
188 191 ActionTrackerNotification.all.map{|a|a.profile}.map do |profile|
189 192 assert [p1,p2].include?(profile)
190 193 end
... ... @@ -218,7 +221,7 @@ class ScrapTest &lt; ActiveSupport::TestCase
218 221  
219 222 should "update the scrap on reply creation" do
220 223 person = create_user.person
221   - s = fast_create(Scrap, :updated_at => DateTime.parse('2010-01-01'))
  224 + s = create(Scrap, sender: person, receiver: person, updated_at: DateTime.parse('2010-01-01'))
222 225 assert_equal DateTime.parse('2010-01-01'), s.updated_at.strftime('%Y-%m-%d')
223 226 DateTime.stubs(:now).returns(DateTime.parse('2010-09-07'))
224 227 s1 = create(Scrap, :content => 'some content', :sender => person, :receiver => person, :scrap_id => s.id)
... ... @@ -288,7 +291,7 @@ class ScrapTest &lt; ActiveSupport::TestCase
288 291  
289 292 should 'create activity with reply_scrap_on_self when top_root scrap receiver is the same as sender' do
290 293 s, r = create_user.person, create_user.person
291   - root = fast_create(Scrap, :sender_id => s.id, :receiver_id => r.id)
  294 + root = create(Scrap, :sender_id => s.id, :receiver_id => r.id)
292 295 assert_difference 'ActionTracker::Record.count', 1 do
293 296 reply = create(Scrap, :sender => r, :receiver => s, :scrap_id => root.id, :content => 'sample')
294 297 end
... ... @@ -296,4 +299,10 @@ class ScrapTest &lt; ActiveSupport::TestCase
296 299 assert_equal 'reply_scrap_on_self', activity.verb.to_s
297 300 end
298 301  
  302 + should 'create profile activity' do
  303 + p1, p2 = create_user.person, create_user.person
  304 + s = create Scrap, :sender => p1, :receiver => p2, :content => "Hello!"
  305 + assert_equal s, p2.activities.first.activity
  306 + end
  307 +
299 308 end
... ...