<commit_before>class Assignment < ApplicationRecord
  include ImportRelation

  after_update :delete_reminder, if: :saved_change_to_confirmation?

  belongs_to :client
  accepts_nested_attributes_for :client

  belongs_to :volunteer
  accepts_nested_attributes_for :volunteer

  belongs_to :creator, -> { with_deleted }, class_name: 'User'
  has_many :hours, as: :hourable, dependent: :destroy

  has_many :feedbacks, as: :feedbackable, dependent: :destroy
  has_many :reminders, dependent: :destroy

  STATES = [:suggested, :active, :finished, :archived].freeze
  KINDS = [:accompaniment, :family, :workshop, :german_class, :transit_center].freeze

  has_one :import, as: :importable, dependent: :destroy
  accepts_nested_attributes_for :import, allow_destroy: true

  validates :client_id, uniqueness: { scope: :volunteer_id, message: I18n.t('assignment_exists') }

  scope :default_order, (-> { order(created_at: :desc) })

  scope :created_between, lambda { |start_date, end_date|
    created_before(end_date).crated_after(start_date)
  }
  scope :created_before, ->(max_time) { where('assignments.created_at < ?', max_time) }
  scope :created_after, ->(min_time) { where('assignments.created_at > ?', min_time) }

  scope :no_end, (-> { where(period_end: nil) })
  scope :has_end, (-> { where.not(period_end: nil) })

  scope :ended, (-> { where('assignments.period_end < ?', Time.zone.today) })
  scope :end_before, ->(date) { where('period_end < ?', date) }
  scope :end_after, ->(date) { where('period_end > ?', date) }
  scope :end_within, ->(date_range) { where(period_end: date_range) }

  scope :end_in_future, (-> { where('period_end > ?', Time.zone.today) })
  scope :not_ended, (-> { no_end.or(end_in_future) })

  scope :started, (-> { where('period_start < ?', Time.zone.today) })
  scope :will_start, (-> { where('period_start > ?', Time.zone.today) })
  scope :start_before, ->(date) { where('period_start < ?', date) }
  scope :start_after, ->(date) { where('period_start > ?', date) }
  scope :start_within, ->(date_range) { where(period_start: date_range) }

  scope :started_six_months_ago, (-> { where('period_start < ?', 6.months.ago.to_date) })

  def started_six_months_ago?
    period_start < 6.months.ago.to_date
  end

  scope :active, (-> { not_ended.started })
  scope :inactive, (-> { ended })

  scope :active_between, lambda { |start_date, end_date|
    start_within(start_date..end_date)
      .or(end_within(start_date..end_date))
  }

  scope :zurich, (-> { joins(:client).merge(Client.zurich) })

  scope :with_hours, (-> { joins(:hours) })

  def ended?
    period_end && period_end < Time.zone.today
  end

  def ongoing?
    !ended?
  end

  def inactive?
    ended? || period_start > Time.zone.today
  end

  def active?
    ongoing? && period_start < Time.zone.today
  end
<commit_msg>Fix active between, zurich and external scopes<commit_after>class Assignment < ApplicationRecord
  include ImportRelation

  after_update :delete_reminder, if: :saved_change_to_confirmation?

  belongs_to :client
  accepts_nested_attributes_for :client

  belongs_to :volunteer
  accepts_nested_attributes_for :volunteer

  belongs_to :creator, -> { with_deleted }, class_name: 'User'
  has_many :hours, as: :hourable, dependent: :destroy

  has_many :feedbacks, as: :feedbackable, dependent: :destroy
  has_many :reminders, dependent: :destroy

  STATES = [:suggested, :active, :finished, :archived].freeze
  KINDS = [:accompaniment, :family, :workshop, :german_class, :transit_center].freeze

  has_one :import, as: :importable, dependent: :destroy
  accepts_nested_attributes_for :import, allow_destroy: true

  validates :client_id, uniqueness: { scope: :volunteer_id, message: I18n.t('assignment_exists') }

  scope :default_order, (-> { order(created_at: :desc) })

  scope :created_between, lambda { |start_date, end_date|
    created_before(end_date).crated_after(start_date)
  }
  scope :created_before, ->(max_time) { where('assignments.created_at < ?', max_time) }
  scope :created_after, ->(min_time) { where('assignments.created_at > ?', min_time) }

  scope :no_end, (-> { where(period_end: nil) })
  scope :has_end, (-> { where.not(period_end: nil) })

  scope :ended, (-> { where('assignments.period_end < ?', Time.zone.today) })
  scope :end_before, ->(date) { where('period_end < ?', date) }
  scope :end_after, ->(date) { where('period_end > ?', date) }
  scope :end_within, ->(date_range) { where(period_end: date_range) }

  scope :end_in_future, (-> { where('period_end > ?', Time.zone.today) })
  scope :not_ended, (-> { no_end.or(end_in_future) })

  scope :started, (-> { where('period_start < ?', Time.zone.today) })
  scope :will_start, (-> { where('period_start > ?', Time.zone.today) })
  scope :start_before, ->(date) { where('period_start < ?', date) }
  scope :start_after, ->(date) { where('period_start > ?', date) }
  scope :start_within, ->(date_range) { where(period_start: date_range) }

  scope :started_six_months_ago, (-> { where('period_start < ?', 6.months.ago.to_date) })

  def started_six_months_ago?
    period_start < 6.months.ago.to_date
  end

  scope :active, (-> { not_ended.started })
  scope :inactive, (-> { ended })

  scope :active_between, lambda { |start_date, end_date|
    no_end.start_before(end_date)
          .or(
            start_before(end_date).end_after(start_date)
          )
  }

  scope :zurich, (-> { joins(:client).merge(Client.zurich) })
  scope :not_zurich, (-> { joins(:client).merge(Client.not_zurich) })

  scope :internal, (-> { joins(:volunteer).merge(Volunteer.internal) })
  scope :external, (-> { joins(:volunteer).merge(Volunteer.external) })

  scope :with_hours, (-> { joins(:hours) })

  def ended?
    period_end && period_end < Time.zone.today
  end

  def ongoing?
    !ended?
  end

  def inactive?
    ended? || period_start > Time.zone.today
  end

  def active?
    ongoing? && period_start < Time.zone.today
  end

{'fraction_non_alphanumeric': 0.154856084834203, 'fraction_numerical': 0.0013465746507322, 'mean_word_length': 4.506950880444856, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 62, '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': '627669', 'n_tokens_mistral': 2181, 'n_tokens_neox': 2088, 'n_words': 402}