Class: Course::Forum::Topic

Inherits:
ApplicationRecord show all
Extended by:
FriendlyId
Defined in:
app/models/course/forum/topic.rb

Defined Under Namespace

Classes: View

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#post_countObject (readonly)

The number of posts in this topic.


32
33
34
35
36
37
# File 'app/models/course/forum/topic.rb', line 32

calculated :post_count, (lambda do
  Course::Discussion::Topic.joins(:posts).
    where('actable_id = course_forum_topics.id').
    where(actable_type: Course::Forum::Topic.name).
    select("count('*')")
end)

#view_countObject (readonly)

The number of views in this topic.


41
42
43
# File 'app/models/course/forum/topic.rb', line 41

calculated :view_count, (lambda do
  Course::Forum::Topic::View.where('topic_id = course_forum_topics.id').select("count('*')")
end)

#vote_countObject (readonly)

The number of votes in this topic.


23
24
25
26
27
28
# File 'app/models/course/forum/topic.rb', line 23

calculated :vote_count, (lambda do
  Course::Discussion::Post::Vote.joins(post: :topic).
    where('course_forum_topics.id = course_discussion_topics.actable_id').
    where('course_discussion_topics.actable_type = ?', Course::Forum::Topic.name).
    select("count('*')")
end)

Class Method Details

.filter_unresolved_forum(forum_ids) ⇒ Object

Filter out the resolved forums from the given ids and keep the unresolved forum ids.


76
77
78
79
# File 'app/models/course/forum/topic.rb', line 76

def self.filter_unresolved_forum(forum_ids)
  # Unscope the default scope of eager loading discussion topics to improve performance.
  unscoped.question.where(resolved: false, forum_id: forum_ids).pluck(:forum_id).to_set
end

.order_by_latest_postObject

Orders the topics by their latest post


47
48
49
# File 'app/models/course/forum/topic.rb', line 47

scope :order_by_latest_post, (lambda do
  order(latest_post_at: :desc)
end)

.with_latest_postObject

Augments all returned records with the latest post.


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/course/forum/topic.rb', line 53

scope :with_latest_post, (lambda do
  topic_ids = distinct(false).pluck('course_discussion_topics.id')
  ids = Course::Discussion::Post.unscope(:order).
        select('max(id)').
        group('course_discussion_posts.topic_id').
        where(topic_id: topic_ids)
  last_posts = Course::Discussion::Post.with_creator.where(id: ids)

  all.tap do |result|
    preloader = ActiveRecord::Associations::Preloader::ManualPreloader.new
    preloader.preload(result, { discussion_topic: :posts }, last_posts)
  end
end)

.with_topic_statisticsObject

Augments all returned records with the number of posts and views in that topic.


69
70
# File 'app/models/course/forum/topic.rb', line 69

scope :with_topic_statistics,
-> { all.calculated(:post_count, :view_count) }

Instance Method Details

#update_resolve_statusObject

Update the resolve boolean status based on correct answer counts.


89
90
91
92
93
94
95
96
# File 'app/models/course/forum/topic.rb', line 89

def update_resolve_status
  status = posts.where(answer: true).count > 0
  if resolved != status
    update_attribute(:resolved, status)
  else
    true
  end
end

#viewed_by(user) ⇒ Object

Create view record for a user


84
85
86
# File 'app/models/course/forum/topic.rb', line 84

def viewed_by(user)
  views.create(user: user)
end