openSUSE/open-build-service

View on GitHub
src/api/app/components/reports_notice_component.rb

Summary

Maintainability
A
0 mins
Test Coverage
C
76%
class ReportsNoticeComponent < ApplicationComponent
  attr_reader :reportable, :reportable_name, :user, :reports

  def initialize(reportable:, user:)
    super

    @reportable = reportable
    @reportable_name = if reportable.instance_of?(::BsRequest)
                         'request'
                       else
                         reportable.class.name.downcase
                       end
    @user = user
    @reports = reportable&.reports&.without_decision
  end

  def by_user?
    Report.exists?(user:, reportable:)
  end

  def report_amount
    pluralize(reports.count, 'report')
  end

  def render?
    Flipper.enabled?(:content_moderation, user) &&
      reports&.any? { |r| Pundit.policy(user, r).show? }
  end
end