This commit is contained in:
r888888888
2014-07-08 10:20:01 -07:00
parent 3cac0458df
commit a6d69e63be
6 changed files with 114 additions and 10 deletions

View File

@@ -0,0 +1,2 @@
module ForumTopicVisitsHelper
end

View File

@@ -105,16 +105,8 @@ class ForumTopic < ActiveRecord::Base
super + [:text_index]
end
def read_by?(user, read_forum_topic_ids)
if read_forum_topic_ids.any? {|topic_id, timestamp| id.to_s == topic_id && updated_at.to_i > timestamp.to_i}
return false
end
if read_forum_topic_ids.any? {|topic_id, timestamp| id.to_s == topic_id && updated_at.to_i <= timestamp.to_i}
return true
end
return false if user.last_forum_read_at.nil?
return true if updated_at < user.last_forum_read_at
return false
def check!(user)
ForumTopicVisit.check!(user, self)
end
def mark_as_read(read_forum_topic_ids)

View File

@@ -0,0 +1,30 @@
class ForumTopicVisit < ActiveRecord::Base
def self.check!(user, topic)
match = where(:user_id => user.id, :forum_topic_id => topic.id).first
result = false
if match
if match.last_read_at < topic.updated_at
result = true
end
match.update_attribute(:last_read_at, topic.updated_at)
else
create(:user_id => user.id, :forum_topic_id => topic.id, :last_read_at => topic.updated_at)
end
result
end
def self.check_list!(user, topics)
matches = where(:user_id => user.id, :forum_topic_id => topics.map(&:id)).to_a.inject({}) do |hash, x|
hash[x.forum_topic_id] = x
hash
end
topics.each do |topic|
if matches[topic.id]
matches[topic.id].update_attribute(:last_read_at, topic.updated_at)
else
create(:user_id => user.id,, :forum_topic_id => topic.id, :last_read_at => topic.updated_at)
end
end
matches
end
end