forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_participation.rb
More file actions
61 lines (52 loc) · 1.89 KB
/
Copy pathcontent_participation.rb
File metadata and controls
61 lines (52 loc) · 1.89 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#
# Copyright (C) 2012 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
class ContentParticipation < ActiveRecord::Base
include Workflow
ACCESSIBLE_ATTRIBUTES = [:content, :user, :workflow_state].freeze
belongs_to :content, polymorphic: [:submission]
belongs_to :user
after_save :update_participation_count
validates_presence_of :content_type, :content_id, :user_id, :workflow_state
workflow do
state :unread
state :read
end
def self.create_or_update(opts={})
opts = opts.with_indifferent_access
content = opts.delete(:content)
user = opts.delete(:user)
return nil unless user && content
participant = nil
unique_constraint_retry do
participant = content.content_participations.where(:user_id => user).first
participant ||= content.content_participations.build(:user => user, :workflow_state => "unread")
participant.attributes = opts.slice(*ACCESSIBLE_ATTRIBUTES)
participant.save if participant.new_record? || participant.changed?
end
participant
end
def update_participation_count
return unless workflow_state_changed?
ContentParticipationCount.create_or_update({
:context => content.context,
:user => user,
:content_type => content_type,
:offset => (workflow_state == "unread" ? 1 : -1),
})
end
end