Sha256: 96c731a53a6079825920e081cf5c21ccca0fb9c80a1acfb899cd1186152ca403

Contents?: true

Size: 1.6 KB

Versions: 30

Compression:

Stored size: 1.6 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? &&
          # Users are allowed to post in unlocked topics of a locked messageboard
          # only if they would be allowed to post if the messageboard wasn't locked.
          @user.thredded_can_write_messageboards.include?(@post.messageboard)
    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

30 entries across 30 versions & 2 rubygems

Version Path
thredded-1.1.0 app/policies/thredded/post_policy.rb
thredded-1.0.1 app/policies/thredded/post_policy.rb
thredded-1.0.0 app/policies/thredded/post_policy.rb
thredded-0.16.16 app/policies/thredded/post_policy.rb
thredded-0.16.15 app/policies/thredded/post_policy.rb
thredded-0.16.14 app/policies/thredded/post_policy.rb
thredded-0.16.13 app/policies/thredded/post_policy.rb
thredded-0.16.12 app/policies/thredded/post_policy.rb
thredded-0.16.11 app/policies/thredded/post_policy.rb
thredded-0.16.10 app/policies/thredded/post_policy.rb
thredded-0.16.9 app/policies/thredded/post_policy.rb
thredded-0.16.8 app/policies/thredded/post_policy.rb
thredded-0.16.7 app/policies/thredded/post_policy.rb
thredded-0.16.6 app/policies/thredded/post_policy.rb
thredded-0.16.5 app/policies/thredded/post_policy.rb
thredded-0.16.4 app/policies/thredded/post_policy.rb
thredded-0.16.3 app/policies/thredded/post_policy.rb
thredded-0.16.1 app/policies/thredded/post_policy.rb
thredded-0.16.0 app/policies/thredded/post_policy.rb
thredded-0.15.5 app/policies/thredded/post_policy.rb