Modified: branches/event_logging/app/helpers/application_helper.rb (1880 => 1881)
--- branches/event_logging/app/helpers/application_helper.rb 2008-10-22 13:49:43 UTC (rev 1880)
+++ branches/event_logging/app/helpers/application_helper.rb 2008-10-22 17:14:09 UTC (rev 1881)
@@ -584,11 +584,17 @@
# when set to "false" will pick up also all related contributors;
# 3-4) before, after - Time objects; the news will be generated in a time slice [after..before]
# 5) limit - number of news entries to generate
- def news(contributor, restrict_contributor=true, before=Time.now, after=Time.now-1.week, limit=30)
+ def news(contributor, restrict_contributor=true, before=Time.now, after=Time.now-DEFAULT_NEWS_TIMEFRAME, limit=DEFAULT_NEWS_COUNT)
hash = {}
- # news_array = contributor_news(contributor, before, after, 0, (restrict_contributor ? contributor : nil)).sort! { |a, b|
- news_array = contributor_news_from_log(contributor, before, after, restrict_contributor, false)
+ # choose news generation method:
+ # - new (faster / more news types), but requires ActivityLogs to be enabled
+ # - old (slow), but collects news items from all across the DB
+ if USE_EVENT_LOG && GENERATE_NEWS_FROM_EVENT_LOG
+ news_array = contributor_news_from_log(contributor, before, after, restrict_contributor, false, limit)
+ else
+ news_array = contributor_news(contributor, before, after, 0, (restrict_contributor ? contributor : nil))
+ end
news_array.sort! { |a, b|
b[0] <=> a[0]
@@ -1386,8 +1392,9 @@
# 4) contributor_news_only - set to "true" to get events relevant for the current user only;
# OR "false" to get events for friends and group members where contributor is a member of, too
# 5) return_raw_events - used for recursive calls; when set to "true" a set of events from ActivityLog table is returned;
- # "false" will cause the events to be interpreted and returned as news items.
- def contributor_news_from_log(contributor, before, after, contributor_news_only, return_raw_events)
+ # "false" will cause the events to be interpreted and returned as news item;
+ # 6) limit - the number of news entries to produce (the lower the value, the more positive effect on performance this has)
+ def contributor_news_from_log(contributor, before, after, contributor_news_only, return_raw_events, limit)
# DEBUG
# puts "=================================="
@@ -1440,19 +1447,24 @@
# get events for current contributor
+ # (no need to impose order on the DB query, as more queries will be run [for "related_contributors"]
+ # and subsequent sorting will be required anyway)
events = ActivityLog.find(:all, :conditions => ["created_at > ? AND created_at < ? AND ((activity_loggable_type = ? AND activity_loggable_id = ?) OR (culprit_type = ? AND culprit_id = ?) OR (referenced_type = ? AND referenced_id = ?))", after, before, contributor.class.to_s, contributor.id, contributor.class.to_s, contributor.id, contributor.class.to_s, contributor.id])
# if any "related_contributors", get events for each
+ # (can't use the "limit" parameter just yet, because events for "related_contributors" may be newer
+ # than some of the contributor in the original query)
events_for_related_contributor = []
related_contributors.each do |c|
- events_for_related_contributor = contributor_news_from_log(c, before, after, true, true)
+ events_for_related_contributor = contributor_news_from_log(c, before, after, true, true, limit)
events.concat(events_for_related_contributor) unless events_for_related_contributor.empty?
end
return events if return_raw_events
- # remove any duplicates and sort by date ascending
+ # remove any duplicates (which may arise when getting same event log entry from friends' related events),
+ # then sort by date descending
events = events.uniq.sort { |a, b|
- a.created_at <=> b.created_at
+ b.created_at <=> a.created_at
}
# produce news from event list
@@ -1461,6 +1473,10 @@
events.each do |e|
news_item = contributor_news_from_log!(e)
rtn << news_item unless news_item.nil?
+
+ # no need to interpret more events than is required by the method call;
+ # -> therefore, stop once the desired number is reached
+ break if (rtn.length == limit)
end
return rtn