Sha256: 894bc15a250ea664269647010b74a599a1d2aeb59d855b1ef75a1f9a3e3d8824

Contents?: true

Size: 1.15 KB

Versions: 10

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true
module Thredded
  class TopicPolicy
    # The scope of readable topics.
    # MessageboardPolicy must be applied separately.
    class Scope
      # @param user [Thredded.user_class]
      # @param scope [ActiveRecord::Relation<Thredded::Topic>]
      def initialize(user, scope)
        @user = user
        @scope = scope
      end

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

    # @param user [Thredded.user_class]
    # @param topic [Thredded::Topic]
    def initialize(user, topic)
      @user                = user
      @topic               = topic
      @messageboard_policy = Thredded::MessageboardPolicy.new(user, topic.messageboard)
    end

    def create?
      @messageboard_policy.post?
    end

    def read?
      @messageboard_policy.read? && @topic.moderation_state_visible_to_user?(@user)
    end

    def update?
      @user.thredded_admin? || @topic.user_id == @user.id || moderate?
    end

    def destroy?
      @user.thredded_admin?
    end

    def moderate?
      @messageboard_policy.moderate?
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
thredded-0.12.1 app/policies/thredded/topic_policy.rb
thredded-0.12.0 app/policies/thredded/topic_policy.rb
thredded-0.11.1 app/policies/thredded/topic_policy.rb
thredded-0.11.0 app/policies/thredded/topic_policy.rb
thredded-0.10.1 app/policies/thredded/topic_policy.rb
thredded-0.10.0 app/policies/thredded/topic_policy.rb
thredded-0.9.4 app/policies/thredded/topic_policy.rb
thredded-0.9.3 app/policies/thredded/topic_policy.rb
thredded-0.9.2 app/policies/thredded/topic_policy.rb
thredded-0.9.1 app/policies/thredded/topic_policy.rb