Class: MyForum::Forum

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/my_forum/forum.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unread_topics_with_latest_post_info(user_id:, page: 0, per_page: 30) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/my_forum/forum.rb', line 30

def self.(user_id:, page: 0, per_page: 30)
  log_topic_ids = LogReadMark.where(user_id: user_id).pluck(:topic_id)
  log_topic_ids = [0] if log_topic_ids.empty?

  Topic.paginate_by_sql("
    SELECT
      my_forum_topics.*,
      my_forum_topics.latest_post_created_at AS last_post_time,
      my_forum_topics.latest_post_login AS last_post_user_login
    FROM my_forum_topics
    WHERE my_forum_topics.id NOT IN (#{log_topic_ids.join(',')}) AND my_forum_topics.deleted IS FALSE
    ORDER BY my_forum_topics.id DESC
  ", page: page, per_page: per_page)
end

Instance Method Details

#has_unread_posts?(current_user) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
# File 'app/models/my_forum/forum.rb', line 7

def has_unread_posts?(current_user)
  return false unless current_user

  latest_post_ids = self.topics.where('latest_post_created_at >= ?', current_user.created_at).pluck(:latest_post_id)
  read_log = []
  read_log = LogReadMark.where("user_id = ? AND post_id IN (?)", current_user.id, latest_post_ids).pluck(:post_id) unless latest_post_ids.blank?

  !(latest_post_ids - read_log).empty?
end

#topics_with_latest_post_info(page: 0, per_page: 30) ⇒ Object

Forum index page



18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/my_forum/forum.rb', line 18

def (page: 0, per_page: 30)
  Topic.paginate_by_sql("
    SELECT
      my_forum_topics.*,
      my_forum_topics.latest_post_created_at AS last_post_time,
      my_forum_topics.latest_post_login AS last_post_user_login
    FROM my_forum_topics
    WHERE my_forum_topics.forum_id = #{self.id} AND my_forum_topics.deleted IS FALSE
    ORDER BY my_forum_topics.pinned DESC, my_forum_topics.latest_post_created_at DESC
  ", page: page, per_page: per_page)
end

#unread_topics_with_userObject



45
46
47
48
# File 'app/models/my_forum/forum.rb', line 45

def unread_topics_with_user
  log_topic_ids = LogReadMark.where(user_id: current_user.id).pluck(:topic_id)
  Topic.where('id NOT IN (?)', log_topic_ids).order('updated_at DESC').paginate(:page => params[:page], :per_page => 30)
end