Sha256: e346c342c4ac435f93152ec6bcf373e71b174b866d98ef25e97c905961cea91c

Contents?: true

Size: 1.38 KB

Versions: 12

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

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?
      Thredded::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.first_post_in_topic? && update?
    end

    def anonymous?
      @user.thredded_anonymous?
    end

    delegate :moderate?, to: :messageboard_policy

    private

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

    def own_post?
      !anonymous? && @user.id == @post.user_id
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
thredded-0.13.8 app/policies/thredded/post_policy.rb
thredded-0.13.7 app/policies/thredded/post_policy.rb
thredded-0.13.6 app/policies/thredded/post_policy.rb
thredded-0.13.5 app/policies/thredded/post_policy.rb
thredded-0.13.4 app/policies/thredded/post_policy.rb
thredded-0.13.3 app/policies/thredded/post_policy.rb
thredded-0.13.2 app/policies/thredded/post_policy.rb
thredded-0.13.1 app/policies/thredded/post_policy.rb
thredded-0.13.0 app/policies/thredded/post_policy.rb
thredded-0.12.4 app/policies/thredded/post_policy.rb
thredded-0.12.3 app/policies/thredded/post_policy.rb
thredded-0.12.2 app/policies/thredded/post_policy.rb