Class: MyForum::Topic

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

Instance Method Summary collapse

Instance Method Details

#infoObject



9
10
11
12
13
14
# File 'app/models/my_forum/topic.rb', line 9

def info
  author  = (post = posts.first).user.
  created = post.created_at

  { author: author, created: created }
end

#mark_as_read(current_user, last_post) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'app/models/my_forum/topic.rb', line 30

def mark_as_read(current_user, last_post)
  return true unless current_user

  log = LogReadMark.find_or_create_by(user_id: current_user.id, topic_id: self.id)

  if last_post.id.to_i > log.post_id.to_i
    log.post_id = last_post.id
    log.save
  end
end

#ownerObject



16
17
18
# File 'app/models/my_forum/topic.rb', line 16

def owner
  posts.first.user
end

#unread?(current_user, last_post) ⇒ Boolean

Returns:

  • (Boolean)


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

def unread?(current_user, last_post)
  return false unless current_user
  return false if current_user.created_at > last_post.created_at

  logged_post = LogReadMark.where(user_id: current_user.id, topic_id: self.id).first
  return true unless logged_post

  last_post.id > logged_post.post_id
end