<commit_before># frozen_string_literal: true

require_dependency "gobierto_calendars"

module GobiertoCalendars
  class Event < ApplicationRecord
    acts_as_paranoid column: :archived_at

    paginates_per 8
    ADMIN_PAGE_SIZE = 40

    attr_accessor :admin_id

    include ActsAsParanoidAliases
    include User::Subscribable
    include GobiertoCommon::UrlBuildable
    include GobiertoCommon::Searchable
    include GobiertoCommon::Sluggable
    include GobiertoCommon::Collectionable
    include GobiertoCommon::Metadatable
    include GobiertoAttachments::Attachable

    validates :site, :collection, presence: true
    validates :slug, uniqueness: { scope: :site_id }


    translates :title, :description

    metadata_attributes :type

    algoliasearch_gobierto do
      attribute :site_id, :title_en, :title_es, :title_ca, :searchable_description, :updated_at
      searchableAttributes ['title_en', 'title_es', 'title_ca', 'searchable_description']
      attributesForFaceting [:site_id]
      add_attribute :resource_path, :class_name
    end

    belongs_to :site
    belongs_to :department, class_name: "GobiertoPeople::Department"
    belongs_to :interest_group, class_name: "GobiertoPeople::InterestGroup"
    has_many :locations, class_name: "EventLocation", dependent: :destroy
    has_many :attendees, class_name: "EventAttendee", dependent: :destroy

    after_create :add_item_to_collection
    after_restore :set_slug

    scope :past,     -> { published.where("starts_at <= ?", Time.zone.now) }
    scope :upcoming, -> { published.where("starts_at > ?", Time.zone.now) }
    scope :sorted,   -> { order(starts_at: :asc) }
    scope :sorted_backwards, -> { order(starts_at: :desc) }
    scope :within_range, ->(date_range) { published.where(starts_at: date_range) }
    scope :synchronized, -> { where("external_id IS NOT NULL") }
    scope :by_date, ->(date) { where("starts_at::date = ?", date) }
    scope :sort_by_updated_at, -> { order(updated_at: :desc) }
    scope :inverse_sorted_by_id, -> { order(id: :asc) }
    scope :sorted_by_id, -> { order(id: :desc) }
    scope :with_interest_group, -> { where.not(interest_group_id: nil) }
    scope :with_department, -> { where.not(department_id: nil) }

    scope :by_collection, ->(collection) { where(collection_id: collection.id) }
    scope :by_site, ->(site) { where(site_id: site.id) }

    scope :by_person_category, ->(category) do
      collection_ids = ::GobiertoPeople::Person.where(category: category).map { |p| p.events_collection.id }
      where(collection_id: collection_ids)
    end

    scope :by_person_party, ->(party) do
      collection_ids = ::GobiertoPeople::Person.where(party: party).map { |p| p.events_collection.id }
      where(collection_id: collection_ids)
    end

    scope :person_events, -> do
      joins("INNER JOIN collection_items ON
          collection_items.container_type = 'GobiertoPeople::Person' AND
          collection_items.item_type = 'GobiertoCalendars::Event' AND
          collection_items.item_id = #{table_name}.id")
    end<commit_msg>Replace queries by JOINS<commit_after># frozen_string_literal: true

require_dependency "gobierto_calendars"

module GobiertoCalendars
  class Event < ApplicationRecord
    acts_as_paranoid column: :archived_at

    paginates_per 8
    ADMIN_PAGE_SIZE = 40

    attr_accessor :admin_id

    include ActsAsParanoidAliases
    include User::Subscribable
    include GobiertoCommon::UrlBuildable
    include GobiertoCommon::Searchable
    include GobiertoCommon::Sluggable
    include GobiertoCommon::Collectionable
    include GobiertoCommon::Metadatable
    include GobiertoAttachments::Attachable

    validates :site, :collection, presence: true
    validates :slug, uniqueness: { scope: :site_id }


    translates :title, :description

    metadata_attributes :type

    algoliasearch_gobierto do
      attribute :site_id, :title_en, :title_es, :title_ca, :searchable_description, :updated_at
      searchableAttributes ['title_en', 'title_es', 'title_ca', 'searchable_description']
      attributesForFaceting [:site_id]
      add_attribute :resource_path, :class_name
    end

    belongs_to :site
    belongs_to :department, class_name: "GobiertoPeople::Department"
    belongs_to :interest_group, class_name: "GobiertoPeople::InterestGroup"
    has_many :locations, class_name: "EventLocation", dependent: :destroy
    has_many :attendees, class_name: "EventAttendee", dependent: :destroy

    after_create :add_item_to_collection
    after_restore :set_slug

    scope :past,     -> { published.where("starts_at <= ?", Time.zone.now) }
    scope :upcoming, -> { published.where("starts_at > ?", Time.zone.now) }
    scope :sorted,   -> { order(starts_at: :asc) }
    scope :sorted_backwards, -> { order(starts_at: :desc) }
    scope :within_range, ->(date_range) { published.where(starts_at: date_range) }
    scope :synchronized, -> { where("external_id IS NOT NULL") }
    scope :by_date, ->(date) { where("starts_at::date = ?", date) }
    scope :sort_by_updated_at, -> { order(updated_at: :desc) }
    scope :inverse_sorted_by_id, -> { order(id: :asc) }
    scope :sorted_by_id, -> { order(id: :desc) }
    scope :with_interest_group, -> { where.not(interest_group_id: nil) }
    scope :with_department, -> { where.not(department_id: nil) }

    scope :by_collection, ->(collection) { where(collection_id: collection.id) }
    scope :by_site, ->(site) { where(site_id: site.id) }

    scope :by_person_category, ->(category) do
      joins("INNER JOIN #{GobiertoPeople::Person.table_name} ON
        collection_items.container_id = #{GobiertoPeople::Person.table_name}.id AND
        #{GobiertoPeople::Person.table_name}.category = #{category}")
    end

    scope :by_person_party, ->(party) do
      joins("INNER JOIN #{GobiertoPeople::Person.table_name} ON
        collection_items.container_id = #{GobiertoPeople::Person.table_name}.id AND
        #{GobiertoPeople::Person.table_name}.party = #{party}")
    end

    scope :person_events, -> do
      joins("INNER JOIN collection_items ON
          collection_items.container_type = 'GobiertoPeople::Person' AND
          collection_items.item_type = 'GobiertoCalendars::Event' AND
          collection_items.item_id = #{table_name}.id")
    end
{'fraction_non_alphanumeric': 0.11403089663959229, 'fraction_numerical': 0.0009555661729574773, 'mean_word_length': 4.242070116861436, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, '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': '7305017', 'n_tokens_mistral': 2071, 'n_tokens_neox': 2018, 'n_words': 402}