<commit_before>###
# CacheObserver is an almost-sweeper for handling cache invalidation.
#
# When using touches and updating multiple models simultaneously, this might
# cause multiple unnecessary touches on parent models where one might suffice.
# CacheObserver prevents this by registering which objects were updated and
# doing the touching after the request is processed.
###
class CacheObserver < ActionController::Caching::Sweeper
  observe :activity,
    :evaluation,
    :meeting,
    :participant,
    :result,
    :table_state,
    :student,
    :group

  attr_accessor :changed_models

  # Controller callbacks
  def before(controller)
    self.changed_models = []
    return true
  end
  def after(controller)
    handle_changes()
  end

  # ActiveRecord callback
  def after_save(model)
    if self.changed_models.nil?
      $stderr.puts "\033[31mCacheObserver is running outside a request."
      $stderr.puts "Remember to call CacheObserver.instance.handle_changes if affecting something that needs the cache to be invalidated\033[0m"
      self.changed_models = []
    end

    self.changed_models << model
  end


  def handle_changes
    single_models = {}
    evaluations   = []
    students      = []
    groups        = []

    # Register models to be touched
    self.changed_models.each do |model|
      register_touches(model, single_models, evaluations, students, groups)
    end

    # Touch models
    touch_single(single_models)
    touch_for_evaluations(evaluations)
    touch_for_students(students)
    touch_for_groups(groups)

    # Reset changed models
    self.changed_models = nil
  end


  private

  def register_touches(model, single_models, evaluations, students, groups)
    case model
    when Activity, Meeting, Participant
      single_models[Suite] ||= []<commit_msg>Invalidate cache on destroys as well, #159 #165<commit_after>###
# CacheObserver is an almost-sweeper for handling cache invalidation.
#
# When using touches and updating multiple models simultaneously, this might
# cause multiple unnecessary touches on parent models where one might suffice.
# CacheObserver prevents this by registering which objects were updated and
# doing the touching after the request is processed.
###
class CacheObserver < ActionController::Caching::Sweeper
  observe :activity,
    :evaluation,
    :meeting,
    :participant,
    :result,
    :table_state,
    :student,
    :group

  attr_accessor :changed_models

  # Controller callbacks
  def before(controller)
    self.changed_models = []
    return true
  end
  def after(controller)
    handle_changes()
  end

  # ActiveRecord callback
  def after_save(model)
    if self.changed_models.nil?
      $stderr.puts "\033[31mCacheObserver is running outside a request."
      $stderr.puts "Remember to call CacheObserver.instance.handle_changes if affecting something that needs the cache to be invalidated\033[0m"
      self.changed_models = []
    end

    self.changed_models << model
  end

  alias_method :after_destroy, :after_save


  def handle_changes
    single_models = {}
    evaluations   = []
    students      = []
    groups        = []

    # Register models to be touched
    self.changed_models.each do |model|
      register_touches(model, single_models, evaluations, students, groups)
    end

    # Touch models
    touch_single(single_models)
    touch_for_evaluations(evaluations)
    touch_for_students(students)
    touch_for_groups(groups)

    # Reset changed models
    self.changed_models = nil
  end


  private

  def register_touches(model, single_models, evaluations, students, groups)
    case model
    when Activity, Meeting, Participant
      single_models[Suite] ||= []
{'fraction_non_alphanumeric': 0.0718918918918919, 'fraction_numerical': 0.006486486486486486, 'mean_word_length': 3.5466830466830466, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '903433', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1011, 'n_words': 316}