<commit_before># frozen_string_literal: true

class Activity < ApplicationRecord
  paginates_per 50

  belongs_to :subject, polymorphic: true
  belongs_to :author, polymorphic: true
  belongs_to :recipient, polymorphic: true
  belongs_to :site

  validates :action, presence: true
  validates :subject_ip, presence: true
  validates :admin_activity, inclusion: { in: [true, false] }

  scope :sorted, -> { order(id: :desc) }
  scope :admin, -> { where(admin_activity: true) }
  # TODO: Open activities to GobiertoParticipation module
  scope :no_admin, -> { where(admin_activity: false, recipient_type: "GobiertoParticipation::Process") }
  scope :global, -> { where(site_id: nil) }
  scope :in_site, ->(site_id) { where(site_id: site_id) }
  scope :for_recipient, ->(recipient) { where(recipient: recipient) }

  def self.global_admin_activities
    global.admin.sorted.includes(:subject, :author, :recipient)
  end

  def self.admin_activities(admin)
    where(author_type: admin.class.name, author_id: admin.id).sorted.includes(:subject, :author, :recipient)
  end

  def self.in_participation
    Activity.select { |a| GobiertoCommon::CollectionItem.where("container_type IN ('GobiertoParticipation')").any? }.pluck(:id)
    where(id: ids)
  end

  def self.in_container(container)
    Activity.select { |a| GobiertoCommon::CollectionItem.where(container: container, item: a.subject).any? }.pluck(:id)
    where(id: ids)
  end
end<commit_msg>Fix filter by issue in participation<commit_after># frozen_string_literal: true

class Activity < ApplicationRecord
  paginates_per 50

  belongs_to :subject, polymorphic: true
  belongs_to :author, polymorphic: true
  belongs_to :recipient, polymorphic: true
  belongs_to :site

  validates :action, presence: true
  validates :subject_ip, presence: true
  validates :admin_activity, inclusion: { in: [true, false] }

  scope :sorted, -> { order(id: :desc) }
  scope :admin, -> { where(admin_activity: true) }
  # TODO: Open activities to GobiertoParticipation module
  scope :no_admin, -> { where(admin_activity: false, recipient_type: "GobiertoParticipation::Process") }
  scope :global, -> { where(site_id: nil) }
  scope :in_site, ->(site_id) { where(site_id: site_id) }
  scope :for_recipient, ->(recipient) { where(recipient: recipient) }

  def self.global_admin_activities
    global.admin.sorted.includes(:subject, :author, :recipient)
  end

  def self.admin_activities(admin)
    where(author_type: admin.class.name, author_id: admin.id).sorted.includes(:subject, :author, :recipient)
  end

  def self.in_participation
    Activity.no_admin.joins("LEFT JOIN collection_items ON collection_items.item_type = activities.subject_type AND
                             collection_items.item_id = activities.subject_id AND collection_items.container_type = 'GobiertoParticipation'")
  end

  def self.in_container(container)
    Activity.no_admin.joins("LEFT JOIN collection_items ON collection_items.item_type = activities.subject_type AND
                            collection_items.item_id = activities.subject_id")
            .where("collection_items.container_type = ?
                    AND collection_items.container_id = ?", container.class.to_s, container.id)
  end
end
{'fraction_non_alphanumeric': 0.11069882498453927, 'fraction_numerical': 0.0012368583797155227, 'mean_word_length': 4.548885077186964, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3356390', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1002, 'n_words': 231}