Sha256: 041a2b98e09945326d7644f3f1d822f69adf123ab767509bd19c0961a5e0ed3a

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Thredded
  class UserTopicDecorator
    extend ActiveModel::Naming
    include ActiveModel::Conversion

    def initialize(user, topic)
      @user = user || NullUser.new
      @topic = topic
    end

    def method_missing(meth, *args)
      if decorated_topic.respond_to?(meth)
        decorated_topic.send(meth, *args)
      else
        super
      end
    end

    def respond_to?(meth)
      decorated_topic.respond_to?(meth)
    end

    def self.decorate_all(user, topics)
      topics.map do |topic|
        new(user, topic)
      end
    end

    def self.model_name
      ActiveModel::Name.new(self, nil, "Topic")
    end

    def persisted?
      false
    end

    def read?
      topic.posts_count == read_status.posts_count
    end

    def farthest_page
      read_status.page
    end

    def farthest_post
      read_status.farthest_post
    end

    def css_class
      if read?
        "read #{decorated_topic.css_class}"
      else
        "unread #{decorated_topic.css_class}"
      end
    end

    def user_link
      if user.valid?
        "<a href='/users/#{topic.user_name}'>#{topic.user_name}</a>".html_safe
      else
        '<a href="#">?</a>'.html_safe
      end
    end


    def decorated_topic
      @decorated_topic ||= TopicDecorator.new(topic)
    end

    private

    attr_reader :topic, :user

    def read_status
      if user.valid?
        @read_status ||= topic.user_topic_reads.select do |reads|
          reads.user_id == user.id
        end
      end

      if @read_status.blank?
        NullTopicRead.new
      else
        @read_status.first
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thredded-0.0.3 app/decorators/thredded/user_topic_decorator.rb