Add `CustomFilter.unexpired` scope (#29896)

pull/29950/head
Matt Jankowski 2024-04-16 05:29:34 -04:00 committed by GitHub
parent 66ee0d4a1f
commit 6b33d3f81b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 2 deletions

View File

@ -42,6 +42,7 @@ class CustomFilter < ApplicationRecord
validate :context_must_be_valid
normalizes :context, with: ->(context) { context.map(&:strip).filter_map(&:presence) }
scope :unexpired, -> { where(expires_at: nil).or where.not(expires_at: ..Time.zone.now) }
before_save :prepare_cache_invalidation!
before_destroy :prepare_cache_invalidation!
@ -66,14 +67,16 @@ class CustomFilter < ApplicationRecord
active_filters = Rails.cache.fetch("filters:v3:#{account_id}") do
filters_hash = {}
scope = CustomFilterKeyword.includes(:custom_filter).where(custom_filter: { account_id: account_id }).where(Arel.sql('expires_at IS NULL OR expires_at > NOW()'))
scope = CustomFilterKeyword.left_outer_joins(:custom_filter).merge(unexpired.where(account_id: account_id))
scope.to_a.group_by(&:custom_filter).each do |filter, keywords|
keywords.map!(&:to_regex)
filters_hash[filter.id] = { keywords: Regexp.union(keywords), filter: filter }
end.to_h
scope = CustomFilterStatus.includes(:custom_filter).where(custom_filter: { account_id: account_id }).where(Arel.sql('expires_at IS NULL OR expires_at > NOW()'))
scope = CustomFilterStatus.left_outer_joins(:custom_filter).merge(unexpired.where(account_id: account_id))
scope.to_a.group_by(&:custom_filter).each do |filter, statuses|
filters_hash[filter.id] ||= { filter: filter }
filters_hash[filter.id].merge!(status_ids: statuses.map(&:status_id))