Class: Course::Discussion::Post

Inherits:
ApplicationRecord show all
Extended by:
OrderingConcern
Includes:
ForumParticipationConcern
Defined in:
app/models/course/discussion/post.rb

Defined Under Namespace

Modules: OrderingConcern Classes: Vote

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OrderingConcern

ordered_topologically

Instance Attribute Details

#downvotesObject (readonly)

The number of downvotes for the given post.


51
52
53
54
55
# File 'app/models/course/discussion/post.rb', line 51

calculated :downvotes, (lambda do
  Vote.downvotes.
    select('count(id)').
    where('post_id = course_discussion_posts.id')
end)

#upvotesObject (readonly)

The number of upvotes for the given post.


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

calculated :upvotes, (lambda do
  Vote.upvotes.
    select('count(id)').
    where('post_id = course_discussion_posts.id')
end)

Class Method Details

.with_user_votes(user) ⇒ Object

Preloads the given posts with votes from the given user.

Parameters:

  • user (User)

    The user to load votes for.


29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/course/discussion/post.rb', line 29

scope :with_user_votes, (lambda do |user|
  post_ids = pluck('course_discussion_posts.id')
  votes = Course::Discussion::Post::Vote.
    where('course_discussion_post_votes.post_id IN (?)', post_ids).
    where('course_discussion_post_votes.creator_id = ?', user.id)

  all.tap do |result|
    preloader = ActiveRecord::Associations::Preloader::ManualPreloader.new
    preloader.preload(result, :votes, votes)
  end
end)

Instance Method Details

#author_nameString

Use the CourseUser name if available, else fallback to the User name.

Returns:

  • (String)

    The CourseUser/User name of the post author.


103
104
105
106
# File 'app/models/course/discussion/post.rb', line 103

def author_name
  course_user = topic.course.course_users.for_user(creator).first
  course_user&.name || creator.name
end

#cast_vote!(user, vote) ⇒ Object

Allows a user to cast a vote for this post.

Parameters:

  • user (User)

    The user casting the vote.

  • vote (Integer)

    0, 1 indicating whether this is a downvote, no vote, or upvote.


77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/course/discussion/post.rb', line 77

def cast_vote!(user, vote)
  vote = vote <=> 0
  vote_record = votes.find_by(creator: user)

  if vote == 0
    vote_record&.destroy!
  else
    vote_record ||= votes.build(creator: user)
    vote_record.vote_flag = vote > 0
    vote_record.save!
  end
end

#toggle_answerObject

Mark/unmark post as the correct answer.


91
92
93
94
95
96
97
98
# File 'app/models/course/discussion/post.rb', line 91

def toggle_answer
  self.class.transaction do
    raise ActiveRecord::Rollback unless update_column(:answer, !answer)
    raise ActiveRecord::Rollback unless topic.specific.update_resolve_status
  end

  true
end

#vote_for(user) ⇒ Course::Discussion::Post::Vote?

Gets the vote cast by the given user for the current post.

Parameters:

  • user (User)

    The user to retrieve the vote for.

Returns:


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

def vote_for(user)
  votes.loaded? ? votes.find { |vote| vote.creator == user } : votes.find_by(creator: user)
end

#vote_tallyInteger

Calculates the total number of votes given to this post.

Returns:

  • (Integer)

60
61
62
# File 'app/models/course/discussion/post.rb', line 60

def vote_tally
  upvotes - downvotes
end