Sha256: 479e5156d2bff30d5a9778e85dcb9fb6d0ad7ee32d858498bbe0fee383fb732f

Contents?: true

Size: 907 Bytes

Versions: 2

Compression:

Stored size: 907 Bytes

Contents

# frozen_string_literal: true
require_dependency 'thredded/urls_helper'
module Thredded
  # A view model for PostCommon.
  class PostView
    delegate :filtered_content,
             :avatar_url,
             :created_at,
             :user,
             :to_model,
             to: :@post

    # @param post [Thredded::PostCommon]
    # @param policy [#update? #destroy?]
    def initialize(post, policy)
      @post   = post
      @policy = policy
    end

    def can_update?
      @can_update ||= @policy.update?
    end

    def can_destroy?
      @can_destroy ||= @policy.destroy?
    end

    def edit_path
      Thredded::UrlsHelper.edit_post_path(@post)
    end

    def destroy_path
      Thredded::UrlsHelper.delete_post_path(@post)
    end

    def cache_key
      [
        @post,
        @post.user,
        [can_update?, can_destroy?].map { |p| p ? '+' : '-' } * ''
      ]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thredded-0.3.1 app/view_models/thredded/post_view.rb
thredded-0.3.0 app/view_models/thredded/post_view.rb