Class: Discussion

Inherits:
ApplicationRecord show all
Includes:
Contextualization, WithDiscussionStatus, WithScopedQueries
Defined in:
app/models/discussion.rb

Constant Summary collapse

MODERATOR_REVIEW_AVERAGE_TIME =
17.minutes

Constants included from WithScopedQueries

WithScopedQueries::SCOPING_METHODS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contextualization

#affable_expectation_results, #affable_test_results, #affable_tips, #expectation_results_visible?, #failed_expectation_results, #feedback_html, #first_test_result, #first_test_result_html, #humanized_expectation_results, #iconize, #queries_with_results, #result_html, #results_body_hidden?, #sanitized_affable_test_results, #single_visible_test_result?, #single_visual_result?, #single_visual_result_html, #test_result_html, #visible_expectation_results, #visible_status

Methods inherited from ApplicationRecord

active_between, aggregate_of, all_except, defaults, #delete, #destroy!, enum_prefixed_translations_for, numbered, organic_on, resource_fields, #save, #save_and_notify!, #save_and_notify_changes!, serialize_symbolized_hash_array, teaser_on, #update_and_notify!, update_or_create!, whitelist_attributes, with_temporary_token

Class Method Details

.debatable_for(klazz, params) ⇒ Object



185
186
187
188
# File 'app/models/discussion.rb', line 185

def self.debatable_for(klazz, params)
  debatable_id = params[:"#{klazz.underscore}_id"]
  klazz.constantize.find(debatable_id)
end

Instance Method Details

#authorized?(user) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'app/models/discussion.rb', line 106

def authorized?(user)
  initiator?(user) || user.try(:moderator_here?)
end

#being_accessed_by_moderator?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'app/models/discussion.rb', line 173

def being_accessed_by_moderator?
  last_moderator_access_at.present? && (last_moderator_access_at + MODERATOR_REVIEW_AVERAGE_TIME).future?
end

#commentable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/discussion.rb', line 63

def commentable_by?(user)
  user&.moderator_here? || (opened? && user.present?)
end

#extra_preview_htmlObject



151
152
153
154
155
# File 'app/models/discussion.rb', line 151

def extra_preview_html
  # FIXME this is buggy, because the extra
  # may have changed since the submission of this discussion
  exercise.assignment_for(initiator).extra_preview_html
end

#friendlyObject



83
84
85
# File 'app/models/discussion.rb', line 83

def friendly
  initiator.name
end

#has_messages?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/discussion.rb', line 131

def has_messages?
  visible_messages.exists? || description.present?
end

#has_responses?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/discussion.rb', line 139

def has_responses?
  responses_count > 0
end

#has_submission?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/discussion.rb', line 71

def has_submission?
  submission.solution.present?
end

#has_validated_responses?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'app/models/discussion.rb', line 143

def has_validated_responses?
  validated_messages_count > 0
end

#initiator?(user) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'app/models/discussion.rb', line 110

def initiator?(user)
  user.try(:uid) == initiator.uid
end

#last_message_dateObject



79
80
81
# File 'app/models/discussion.rb', line 79

def last_message_date
  visible_messages.last&.created_at || created_at
end

#last_moderator_access_visible_for?(user) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
# File 'app/models/discussion.rb', line 177

def last_moderator_access_visible_for?(user)
  show_last_moderator_access_for?(user) && being_accessed_by_moderator?
end


51
52
53
# File 'app/models/discussion.rb', line 51

def navigable_content_in(_)
  nil
end

#reachable_status_for?(user, status) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/discussion.rb', line 119

def reachable_status_for?(user, status)
  reachable_statuses_for(user).include? status
end

#reachable_statuses_for(user) ⇒ Object



114
115
116
117
# File 'app/models/discussion.rb', line 114

def reachable_statuses_for(user)
  return [] unless authorized?(user)
  status.reachable_statuses_for(user, self)
end

#read_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/models/discussion.rb', line 75

def read_by?(user)
  subscription_for(user).read
end

#responses_countObject



135
136
137
# File 'app/models/discussion.rb', line 135

def responses_count
  visible_messages.where.not(sender: initiator.uid).count
end

#show_last_moderator_access_for?(user) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
# File 'app/models/discussion.rb', line 181

def show_last_moderator_access_for?(user)
  user&.moderator_here? && last_moderator_access_by != user
end

#submit_message!(message, user) ⇒ Object



99
100
101
102
103
104
# File 'app/models/discussion.rb', line 99

def submit_message!(message, user)
  message.merge!(sender: user.uid)
  messages.create(message)
  user.subscribe_to! self
  unread_subscriptions(user)
end

#subscribable?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/discussion.rb', line 67

def subscribable?
  opened? || solved?
end

#subscribe_initiator!Object



147
148
149
# File 'app/models/discussion.rb', line 147

def subscribe_initiator!
  initiator.subscribe_to! self
end

#subscription_for(user) ⇒ Object



87
88
89
# File 'app/models/discussion.rb', line 87

def subscription_for(user)
  subscriptions.find_by(user: user)
end

#targetObject



55
56
57
# File 'app/models/discussion.rb', line 55

def target
  self
end

#try_solve!Object



45
46
47
48
49
# File 'app/models/discussion.rb', line 45

def try_solve!
  if opened?
    update! status: reachable_statuses_for(initiator).first
  end
end

#unread_subscriptions(user) ⇒ Object



95
96
97
# File 'app/models/discussion.rb', line 95

def unread_subscriptions(user)
  subscriptions.where.not(user: user).map(&:unread!)
end

#update_counters!Object



157
158
159
160
161
162
163
164
# File 'app/models/discussion.rb', line 157

def update_counters!
  messages_query = messages_by_updated_at
  validated_messages = messages_query.select &:validated?
  has_moderator_response = messages_query.find { |it| it.validated? || it.question? }&.validated?
  update! messages_count: messages_query.count,
          validated_messages_count: validated_messages.count,
          requires_moderator_response: !has_moderator_response
end

#update_last_moderator_access!(user) ⇒ Object



166
167
168
169
170
171
# File 'app/models/discussion.rb', line 166

def update_last_moderator_access!(user)
  if user&.moderator_here? && !last_moderator_access_visible_for?(user)
    update! last_moderator_access_at: Time.now,
            last_moderator_access_by: user
  end
end

#update_status!(status, user) ⇒ Object



123
124
125
126
127
128
129
# File 'app/models/discussion.rb', line 123

def update_status!(status, user)
  if reachable_status_for?(user, status)
    update! status: status,
            status_updated_by: user,
            status_updated_at: Time.now
  end
end

#upvote_for(user) ⇒ Object



91
92
93
# File 'app/models/discussion.rb', line 91

def upvote_for(user)
  upvotes.find_by(user: user)
end

#used_in?(organization) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/models/discussion.rb', line 59

def used_in?(organization)
  organization == self.organization
end

#visible_messagesObject



41
42
43
# File 'app/models/discussion.rb', line 41

def visible_messages
  messages.where.not(deletion_motive: :self_deleted).or(messages.where(deletion_motive: nil))
end