Commit 43378edcae1546a3d521af6d9389111e2c6d239e

Authored by Leandro Santos
Committed by Antonio Terceiro
1 parent 0233ac67

bugfix

db/migrate/20100928000952_aggressive_indexing_strategy2.rb 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +class AggressiveIndexingStrategy2 < ActiveRecord::Migration
  2 + def self.up
  3 + execute("delete from action_tracker_notifications where id not in (select distinct(atn.id) from action_tracker_notifications as atn JOIN action_tracker_notifications as t ON (t.profile_id = atn.profile_id and t.action_tracker_id = atn.action_tracker_id and atn.id < t.id))")
  4 + add_index(:action_tracker_notifications, :profile_id)
  5 + add_index(:action_tracker_notifications, :action_tracker_id)
  6 + add_index(:action_tracker_notifications, [:profile_id, :action_tracker_id], :unique => true)
  7 + end
  8 +
  9 + def self.down
  10 + remove_index(:action_tracker_notifications, :profile_id)
  11 + remove_index(:action_tracker_notifications, :action_tracker_id)
  12 + remove_index(:action_tracker_notifications, [:profile_id, :action_tracker_id])
  13 + end
  14 +end
... ...
lib/notify_activity_to_profiles_job.rb
... ... @@ -12,17 +12,18 @@ class NotifyActivityToProfilesJob &lt; Struct.new(:tracked_action_id)
12 12 tracked_action = ActionTracker::Record.find(tracked_action_id)
13 13 target = tracked_action.target
14 14 if target.is_a?(Community) && NOTIFY_ONLY_COMMUNITY.include?(tracked_action.verb)
15   - ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) values (#{target.id}, #{tracked_action.id})")
  15 + ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id)
16 16 return
17 17 end
18 18  
19   - ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) values (#{tracked_action.user.id}, #{tracked_action.id})")
20   - ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select friend_id, #{tracked_action.id} from friendships where person_id=#{tracked_action.user.id}")
  19 + ActionTrackerNotification.create(:profile_id => tracked_action.user.id, :action_tracker_id => tracked_action.id)
  20 +
  21 + ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select f.friend_id, #{tracked_action.id} from friendships as f where person_id=#{tracked_action.user.id} and f.friend_id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id})")
21 22  
22 23 if target.is_a?(Community)
23 24 ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select distinct profiles.id, #{tracked_action.id} from role_assignments, profiles where profiles.type = 'Person' and profiles.id = role_assignments.accessor_id and profiles.id != #{tracked_action.user.id} and role_assignments.resource_type = 'Profile' and role_assignments.resource_id = #{target.id}")
24 25  
25   - ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) values (#{target.id}, #{tracked_action.id})") unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb)
  26 + ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb)
26 27 end
27 28 end
28 29 end
... ...
test/unit/action_tracker_notification_test.rb
... ... @@ -48,4 +48,32 @@ class ActionTrackerNotificationTest &lt; ActiveSupport::TestCase
48 48 assert_equal count, ActionTrackerNotification.count
49 49 end
50 50  
  51 + should "the action_tracker_id be unique on scope of profile" do
  52 + atn = fast_create(ActionTrackerNotification, :action_tracker_id => 1, :profile_id => 1)
  53 + assert atn.valid?
  54 +
  55 + atn = ActionTrackerNotification.new(:action_tracker_id => 1, :profile_id => 1)
  56 + atn.valid?
  57 + assert atn.errors.invalid?(:action_tracker_id)
  58 +
  59 + atn.profile_id = 2
  60 + atn.valid?
  61 + assert !atn.errors.invalid?(:action_tracker_id)
  62 + end
  63 +
  64 + should "the action_tracker_id be unique on scope of profile when created by ActionTracker::Record association" do
  65 + at = fast_create(ActionTracker::Record)
  66 + person = fast_create(Person)
  67 + assert_equal [], at.action_tracker_notifications
  68 + at.action_tracker_notifications<< ActionTrackerNotification.new(:profile => person)
  69 + at.reload
  70 +
  71 + assert_equal 1, at.action_tracker_notifications.count
  72 + last_notification = at.action_tracker_notifications.first
  73 +
  74 + at.action_tracker_notifications<< ActionTrackerNotification.new(:profile => person)
  75 + at.reload
  76 + assert_equal [last_notification], at.action_tracker_notifications
  77 + end
  78 +
51 79 end
... ...
test/unit/article_test.rb
... ... @@ -1056,4 +1056,141 @@ class ArticleTest &lt; Test::Unit::TestCase
1056 1056 assert_equal false, a.is_trackable?
1057 1057 end
1058 1058  
  1059 + should 'not create more than one notification track action to community when update more than one artile' do
  1060 + community = fast_create(Community)
  1061 + p1 = Person.first || fast_create(Person)
  1062 + community.add_member(p1)
  1063 + assert p1.is_member_of?(community)
  1064 + Article.destroy_all
  1065 + ActionTracker::Record.destroy_all
  1066 + article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => community.id
  1067 + assert article.published?
  1068 + assert_kind_of Community, article.profile
  1069 + assert_equal 1, ActionTracker::Record.count
  1070 + ta = ActionTracker::Record.last
  1071 + assert_equal 'Tracked Article 1', ta.get_name.last
  1072 + assert_equal article.url, ta.get_url.last
  1073 + assert p1, ta.user
  1074 + assert community, ta.target
  1075 + process_delayed_job_queue
  1076 + assert_equal 2, ActionTrackerNotification.count
  1077 +
  1078 + article = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => community.id
  1079 + assert article.published?
  1080 + assert_kind_of Community, article.profile
  1081 + assert_equal 1, ActionTracker::Record.count
  1082 + ta = ActionTracker::Record.last
  1083 + assert_equal 'Tracked Article 2', ta.get_name.last
  1084 + assert_equal article.url, ta.get_url.last
  1085 + assert_equal p1, ta.user
  1086 + assert_equal community, ta.target
  1087 + process_delayed_job_queue
  1088 + assert_equal 2, ActionTrackerNotification.count
  1089 + end
  1090 +
  1091 + should 'create the notification to the member when one member has the notification and the other no' do
  1092 + community = fast_create(Community)
  1093 + p1 = Person.first || fast_create(Person)
  1094 + community.add_member(p1)
  1095 + assert p1.is_member_of?(community)
  1096 + Article.destroy_all
  1097 + ActionTracker::Record.destroy_all
  1098 + article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => community.id
  1099 + assert article.published?
  1100 + assert_kind_of Community, article.profile
  1101 + assert_equal 1, ActionTracker::Record.count
  1102 + ta = ActionTracker::Record.first
  1103 + assert_equal 'Tracked Article 1', ta.get_name.last
  1104 + assert_equal article.url, ta.get_url.last
  1105 + assert p1, ta.user
  1106 + assert community, ta.target
  1107 + process_delayed_job_queue
  1108 + assert_equal 2, ActionTrackerNotification.count
  1109 +
  1110 + p2 = fast_create(Person)
  1111 + community.add_member(p2)
  1112 + process_delayed_job_queue
  1113 + assert_equal 5, ActionTrackerNotification.count
  1114 +
  1115 + article = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => community.id
  1116 + assert article.published?
  1117 + assert_kind_of Community, article.profile
  1118 + assert_equal 3, ActionTracker::Record.count
  1119 + ta = ActionTracker::Record.first
  1120 + assert_equal 'Tracked Article 2', ta.get_name.last
  1121 + assert_equal article.url, ta.get_url.last
  1122 + assert_equal p1, ta.user
  1123 + assert_equal community, ta.target
  1124 + process_delayed_job_queue
  1125 + assert_equal 6, ActionTrackerNotification.count
  1126 + end
  1127 +
  1128 + should 'not create more than one notification track action to friends when update more than one artile' do
  1129 + p1 = Person.first || fast_create(Person)
  1130 + friend = fast_create(Person)
  1131 + p1.add_friend(friend)
  1132 + Article.destroy_all
  1133 + ActionTracker::Record.destroy_all
  1134 + ActionTrackerNotification.destroy_all
  1135 + article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => p1.id
  1136 + assert article.published?
  1137 + assert_kind_of Person, article.profile
  1138 + assert_equal 1, ActionTracker::Record.count
  1139 + ta = ActionTracker::Record.last
  1140 + assert_equal 'Tracked Article 1', ta.get_name.last
  1141 + assert_equal article.url, ta.get_url.last
  1142 + assert p1, ta.user
  1143 + assert p1, ta.target
  1144 + process_delayed_job_queue
  1145 + assert_equal 2, ActionTrackerNotification.count
  1146 +
  1147 + article = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => p1.id
  1148 + assert article.published?
  1149 + assert_kind_of Person, article.profile
  1150 + assert_equal 1, ActionTracker::Record.count
  1151 + ta = ActionTracker::Record.last
  1152 + assert_equal 'Tracked Article 2', ta.get_name.last
  1153 + assert_equal article.url, ta.get_url.last
  1154 + assert_equal p1, ta.user
  1155 + assert_equal p1, ta.target
  1156 + process_delayed_job_queue
  1157 + assert_equal 2, ActionTrackerNotification.count
  1158 + end
  1159 +
  1160 + should 'create the notification to the friend when one friend has the notification and the other no' do
  1161 + p1 = Person.first || fast_create(Person)
  1162 + f1 = fast_create(Person)
  1163 + p1.add_friend(f1)
  1164 + Article.destroy_all
  1165 + ActionTracker::Record.destroy_all
  1166 + ActionTrackerNotification.destroy_all
  1167 + article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => p1.id
  1168 + assert article.published?
  1169 + assert_kind_of Person, article.profile
  1170 + assert_equal 1, ActionTracker::Record.count
  1171 + ta = ActionTracker::Record.first
  1172 + assert_equal 'Tracked Article 1', ta.get_name.last
  1173 + assert_equal article.url, ta.get_url.last
  1174 + assert p1, ta.user
  1175 + assert p1, ta.target
  1176 + process_delayed_job_queue
  1177 + assert_equal 2, ActionTrackerNotification.count
  1178 +
  1179 + f2 = fast_create(Person)
  1180 + p1.add_friend(f2)
  1181 + process_delayed_job_queue
  1182 + assert_equal 5, ActionTrackerNotification.count
  1183 + article = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => p1.id
  1184 + assert article.published?
  1185 + assert_kind_of Person, article.profile
  1186 + assert_equal 2, ActionTracker::Record.count
  1187 + ta = ActionTracker::Record.first
  1188 + assert_equal 'Tracked Article 2', ta.get_name.last
  1189 + assert_equal article.url, ta.get_url.last
  1190 + assert_equal p1, ta.user
  1191 + assert_equal p1, ta.target
  1192 + process_delayed_job_queue
  1193 + assert_equal 6, ActionTrackerNotification.count
  1194 + end
  1195 +
1059 1196 end
... ...