Sha256: f27fc9a59aaff0c49d24fff7a6a5950a94acb767f7c34ef06dbd8c37b2ae6844

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # A concern with the features needed when you want a model to be reportable
  module Reportable
    extend ActiveSupport::Concern

    included do
      has_one :moderation, as: :reportable, foreign_key: "decidim_reportable_id", foreign_type: "decidim_reportable_type", class_name: "Decidim::Moderation"
      has_many :reports, through: :moderation

      scope :reported, -> { left_outer_joins(:moderation).where(Decidim::Moderation.arel_table[:report_count].gt(0)) }
      scope :hidden, -> { left_outer_joins(:moderation).where.not(Decidim::Moderation.arel_table[:hidden_at].eq nil) }
      scope :not_hidden, -> { left_outer_joins(:moderation).where(Decidim::Moderation.arel_table[:hidden_at].eq nil) }

      # Public: Check if the user has reported the proposal.
      #
      # Returns Boolean.
      def reported_by?(user)
        reports.where(user: user).any?
      end

      # Public: Checks if the proposal is hidden or not.
      #
      # Returns Boolean.
      def hidden?
        moderation&.hidden_at&.present?
      end

      # Public: Checks if the proposal has been reported or not.
      #
      # Returns Boolean.
      def reported?
        moderation&.report_count&.positive?
      end

      # Public: The reported content
      #
      # Returns html content
      def reported_content
        raise NotImplementedError
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-core-0.2.0 lib/decidim/reportable.rb