Skip to content

Instantly share code, notes, and snippets.

@foucist
Last active May 3, 2016 00:40
Show Gist options
  • Save foucist/8e0d0f4275547f28eed3 to your computer and use it in GitHub Desktop.
Save foucist/8e0d0f4275547f28eed3 to your computer and use it in GitHub Desktop.
class Message::Collection::Count
MENU_TYPES = %w{message gift flirt access_granted request_access}
def initialize(profile)
@profile = profile
end
def self.unread_counts_for(profile)
new(profile).unread_message_counts
end
def unread_message_counts
hash = message_counts_by_type
sort_into_menu_types(hash)
end
private
def message_counts_by_type
array = Message.unread.to(@profile).group(:type).pluck('type, count(type)')
array.reduce({}){|bag, (type, count)| bag[Message.type_mapping(type)] = count; bag}
end
def sort_into_menu_types(hash)
listed, unlisted = hash.slice(*MENU_TYPES), hash.except(*MENU_TYPES)
listed["message"] = listed["message"].to_i + unlisted.values.sum
listed["all"] = listed.values.sum
listed
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment