Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams
Created October 14, 2009 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrchrisadams/209955 to your computer and use it in GitHub Desktop.
Save mrchrisadams/209955 to your computer and use it in GitHub Desktop.
class ActivityCollator
def self.for(user, filter, limit)
new(user).list(filter, limit)
end
def initialize(user)
@user = user
end
def list(filter, limit)
# WARNING this wont scale, will need to move to a notification based approach at some point
# The following code exposes the classic Twitter scaling problem
# before that a hand crafted SQL statement might work too and it should probably be cached as well
# Heres how this algo works
# Theres a max of 14 items that fit on to the home page, so get 14 of each possible content type and then
# throw away all but the latest 14
result = []
result << list_own_thoughts(limit) if filter.blank? or filter == "thoughts"
result << list_comments_on_supported_items(limit) if filter.blank? or filter == "comments"
result << list_actions_on_supported_thoughts(limit) if filter.blank? or filter == "actions"
result << list_intents_on_supported_actions(limit) if filter.blank? or filter == "intents"
result << list_stories_on_supported_actions(limit) if filter.blank? or filter == "stories"
result << list_comments_on_my_items(limit) if filter.blank? or filter == "comments"
result << list_actions_on_my_thoughts(limit) if filter.blank? or filter == "actions"
result << list_intents_on_my_items(limit) if filter.blank? or filter == "intents"
result << list_stories_on_my_items(limit) if filter.blank? or filter == "stories"
result.flatten.uniq.sort_by(&:updated_at).reverse[0..limit]
end
private
def list_own_thoughts(limit)
@user.thoughts.all :limit => limit, :order => "updated_at desc"
end
def list_comments_on_supported_items(limit)
["Thought", "Action", "Feedback"].map do |type|
Comment.all :limit => limit,
:order => "created_at desc",
:conditions => { :commentable_type => type, :commentable_id => @user.send("supported_#{type.downcase.pluralize}").map(&:id) }
end.flatten
end
def list_actions_on_supported_thoughts(limit)
Action.all :limit => limit,
:order => "updated_at desc",
:conditions => { :thought_id => @user.supported_thoughts.map(&:id) }
end
def list_intents_on_supported_actions(limit)
Intent.all :limit => limit,
:order => "created_at desc",
:conditions => { :action_id => @user.supported_actions.map(&:id), :completed => false }
end
def list_stories_on_supported_actions(limit)
Intent.all :limit => limit,
:order => "created_at desc",
:conditions => { :action_id => @user.supported_actions.map(&:id), :completed => true }
end
def list_comments_on_my_items(limit)
["Thought", "Action", "Feedback"].map do |type|
Comment.all(:limit => limit,
:order => "created_at desc",
:conditions => { :commentable_type => type, :commentable_id => @user.send(type.downcase.pluralize).map(&:id) })
end.flatten
end
def list_actions_on_my_thoughts(limit)
Action.all :limit => limit,
:order => "created_at desc",
:conditions => { :thought_id => @user.thoughts.map(&:id) }
end
def list_intents_on_my_items(limit)
Intent.all :limit => limit,
:order => "updated_at desc",
:conditions => { :action_id => @user.actions.map(&:id), :completed => false }
end
def list_stories_on_my_items(limit)
Intent.all :limit => limit,
:order => "updated_at desc",
:conditions => { :action_id => @user.actions.map(&:id), :completed => true }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment