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



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

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)


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

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

#commentable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#extra_preview_htmlObject



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

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



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

def friendly
  initiator.abbreviated_name
end

#has_messages?Boolean

Returns:

  • (Boolean)


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

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

#has_responses?Boolean

Returns:

  • (Boolean)


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

def has_responses?
  responses_count > 0
end

#has_submission?Boolean

Returns:

  • (Boolean)


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

def has_submission?
  submission.solution.present?
end

#has_validated_responses?Boolean

Returns:

  • (Boolean)


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

def has_validated_responses?
  validated_messages_count > 0
end

#initiator?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#last_message_dateObject



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

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


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

def navigable_content_in(_)
  nil
end

#reachable_status_for?(user, status) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#reachable_statuses_for(user) ⇒ Object



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

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

#read_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#responses_countObject



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

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

#submit_message!(message, user) ⇒ Object



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

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)


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

def subscribable?
  opened? || solved?
end

#subscribe_initiator!Object



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

def subscribe_initiator!
  initiator.subscribe_to! self
end

#subscription_for(user) ⇒ Object



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

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

#targetObject



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

def target
  self
end

#try_solve!Object



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

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

#unread_subscriptions(user) ⇒ Object



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

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

#update_counters!Object



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

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



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

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



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

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

#used_in?(organization) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visible_messagesObject



39
40
41
# File 'app/models/discussion.rb', line 39

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