discourse/app/serializers/basic_topic_serializer.rb

54 lines
1.2 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
require_dependency 'age_words'
class BasicTopicSerializer < ApplicationSerializer
include ActionView::Helpers
2013-02-07 10:45:24 -05:00
attributes :id, :title, :reply_count, :posts_count, :highest_post_number, :image_url, :created_at,
2013-02-05 14:16:51 -05:00
:last_posted_at, :age, :unseen, :last_read_post_number, :unread, :new_posts
def age
AgeWords.age_words(Time.now - (object.created_at || Time.now))
end
def seen
2013-02-07 10:45:24 -05:00
object.user_data.present?
2013-02-05 14:16:51 -05:00
end
def unseen
return false if scope.blank?
2013-02-07 10:45:24 -05:00
return false if scope.user.blank?
2013-02-05 14:16:51 -05:00
return false if object.user_data.present?
return false if object.created_at < scope.user.created_at
# Only mark things as new since your last visit
2013-02-07 10:45:24 -05:00
if scope.user.previous_visit_at.present?
2013-02-05 14:16:51 -05:00
return false if object.created_at < scope.user.previous_visit_at
end
true
end
def last_read_post_number
object.user_data.last_read_post_number
end
alias :include_last_read_post_number? :seen
def unread
unread_helper.unread_posts
end
alias :include_unread? :seen
def new_posts
unread_helper.new_posts
end
alias :include_new_posts? :seen
protected
def unread_helper
@unread_helper ||= Unread.new(object, object.user_data)
end
2013-02-07 10:45:24 -05:00
end