-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathuser_seen_subject.rb
33 lines (28 loc) · 995 Bytes
/
user_seen_subject.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class UserSeenSubject < ActiveRecord::Base
belongs_to :user
belongs_to :workflow
validates_presence_of :user, :workflow
def self.add_seen_subjects_for_user(user: nil, workflow: nil, subject_ids: nil)
uss = where(user: user, workflow: workflow)
if uss.exists?
uss.update_all(["subject_ids = uniq(subject_ids + array[?])", subject_ids])
else
uss.create!(subject_ids: subject_ids)
end
end
def self.count_user_activity(user_id, workflow_ids=[])
workflow_counts = activity_by_workflow(user_id, workflow_ids)
workflow_counts.values.sum
end
def self.activity_by_workflow(user_id, workflow_ids=[])
workflow_ids = Array.wrap(workflow_ids)
scope = self.where(user_id: user_id)
unless workflow_ids.empty?
scope = scope.where(workflow_id: workflow_ids)
end
scope.group(:workflow_id).sum("cardinality(subject_ids)").as_json
end
def subjects_seen?(ids)
Array.wrap(ids).any? { |id| subject_ids.include?(id) }
end
end