Sha256: 18bf8437fc83e1fd51767cb4d913c35418aa5be7b6e9eab88a4c816bf7ba1456

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true
require_dependency 'thredded/topic_policy'
module Thredded
  class PostPolicy
    # The scope of readable posts.
    # MessageboardPolicy must be applied separately.
    class Scope
      # @param user [Thredded.user_class]
      # @param scope [ActiveRecord::Relation<Thredded::Post>]
      def initialize(user, scope)
        @user = user
        @scope = scope
      end

      # @return [ActiveRecord::Relation<Thredded::Post>]
      def resolve
        @scope.moderation_state_visible_to_user(@user)
      end
    end

    # @param user [Thredded.user_class]
    # @param post [Thredded::Post]
    def initialize(user, post)
      @user = user
      @post = post
    end

    def create?
      @user.thredded_admin? || !@post.postable.locked? && messageboard_policy.post?
    end

    def read?
      TopicPolicy.new(@user, @post.postable).read? && @post.moderation_state_visible_to_user?(@user)
    end

    def update?
      @user.thredded_admin? || own_post? || messageboard_policy.moderate?
    end

    def destroy?
      @post.postable.first_post != @post && update?
    end

    delegate :moderate?, to: :messageboard_policy

    private

    def messageboard_policy
      @messageboard_policy ||= MessageboardPolicy.new(@user, @post.messageboard)
    end

    def own_post?
      @user.id == @post.user_id
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
thredded-0.8.4 app/policies/thredded/post_policy.rb
thredded-0.8.2 app/policies/thredded/post_policy.rb
thredded-0.7.0 app/policies/thredded/post_policy.rb
thredded-0.6.3 app/policies/thredded/post_policy.rb
thredded-0.6.2 app/policies/thredded/post_policy.rb
thredded-0.6.1 app/policies/thredded/post_policy.rb
thredded-0.6.0 app/policies/thredded/post_policy.rb