Sha256: 13fadc8d1feed134a0ec58f56c9d5110bb638d3baa5c7f8c02f916b00669b167

Contents?: true

Size: 1.15 KB

Versions: 42

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

42 entries across 42 versions & 2 rubygems

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