Class: Discussion

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

Constant Summary

Constants included from WithScopedQueries

WithScopedQueries::SCOPING_METHODS

Constants included from WithResponsibleModerator

WithResponsibleModerator::MODERATOR_MAX_RESPONSIBLE_TIME

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 included from WithResponsibleModerator

#any_responsible?, #can_toggle_responsible?, #current_responsible_visible_for?, #no_current_responsible?, #responsible?, #toggle_responsible!

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_pg_retry, with_temporary_token

Class Method Details

.debatable_for(klazz, params) ⇒ Object



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

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)


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

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

#commentable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#extra_preview_htmlObject



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

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



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

def friendly
  initiator.abbreviated_name
end

#has_messages?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/models/discussion.rb', line 137

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

#has_responses?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'app/models/discussion.rb', line 145

def has_responses?
  responses_count > 0
end

#has_submission?Boolean

Returns:

  • (Boolean)


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

def has_submission?
  submission.solution.present?
end

#has_validated_responses?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'app/models/discussion.rb', line 149

def has_validated_responses?
  validated_messages_count > 0
end

#initiator?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#last_message_dateObject



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

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


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

def navigable_content_in(_)
  nil
end

#reachable_status_for?(user, status) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/discussion.rb', line 123

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

#reachable_statuses_for(user) ⇒ Object



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

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

#read_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#requires_attention?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'app/models/discussion.rb', line 157

def requires_attention?
  no_current_responsible? && status.requires_attention_for?(self)
end

#requires_attention_for?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

def requires_attention_for?(user)
  user&.moderator_here? && requires_attention?
end

#responses_countObject



141
142
143
# File 'app/models/discussion.rb', line 141

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

#submit_message!(message, user) ⇒ Object



102
103
104
105
106
107
108
# File 'app/models/discussion.rb', line 102

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

#subscribable?Boolean

Returns:

  • (Boolean)


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

def subscribable?
  opened? || solved?
end

#subscribe_initiator!Object



161
162
163
# File 'app/models/discussion.rb', line 161

def subscribe_initiator!
  initiator.subscribe_to! self
end

#subscription_for(user) ⇒ Object



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

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

#targetObject



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

def target
  self
end

#try_solve!Object



48
49
50
51
52
# File 'app/models/discussion.rb', line 48

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

#unread_subscriptions(user) ⇒ Object



98
99
100
# File 'app/models/discussion.rb', line 98

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

#update_counters!Object



171
172
173
174
175
176
177
178
# File 'app/models/discussion.rb', line 171

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_status!(status, user) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'app/models/discussion.rb', line 127

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

    no_responsible! if responsible?(user)
  end
end

#upvote_for(user) ⇒ Object



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

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

#used_in?(organization) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visible_messagesObject



44
45
46
# File 'app/models/discussion.rb', line 44

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