Created
April 26, 2015 15:05
-
-
Save oleglukashev/73850217488a0fb3f29f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CalculateCountEvents | |
extend ActiveSupport::Concern | |
def count_new_events | |
if self.has_role? :traveler | |
count_discussions_events + count_applications_events | |
else | |
count_discussions_events + count_orders_events | |
end | |
end | |
def count_orders_events | |
count = 0 | |
self.orders.each do |order| | |
unless order.is_viewed.present? | |
count += 1 | |
else | |
count += has_unviewed_histories_or_messages order | |
end | |
end | |
count | |
end | |
def count_applications_events | |
count = 0 | |
self.applications.each do |application| | |
count += has_unviewed_histories_or_messages application | |
end | |
count | |
end | |
def count_discussions_events | |
count = 0 | |
self.discussion_users.each do |discussion_user| | |
discussion = discussion_user.discussion | |
if discussion.discussion_messages.unviewed_for_user(self.id).size > 0 | |
count += 1 | |
end | |
end | |
count | |
end | |
def has_unviewed_histories_or_messages order | |
if count_unviewed_order_status_histories(order) > 0 | |
1 | |
else | |
if count_unviewed_order_messages(order) > 0 | |
1 | |
else | |
0 | |
end | |
end | |
end | |
def count_unviewed_order_status_histories order | |
order.order_status_histories.unviewed_for_user(self.id).size | |
end | |
def count_unviewed_order_messages order | |
order.order_messages.unviewed_for_user(self.id).size | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Пока без кэширования