Как включить модуль с окончанием срока действия кэша в метлах?

У нас есть следующая подметальная машина в рельсовом приложении:

class AgencyEquipmentTypeSweeper < ActionController::Caching::Sweeper 
  observe AgencyEquipmentType

  #include ExpireOptions
  def after_update(agency_equipment_type)
    expire_options(agency_equipment_type)
  end

  def after_delete(agency_equipment_type)
    expire_options(agency_equipment_type)
  end

  def after_create(agency_equipment_type)
    expire_options(agency_equipment_type)
  end

  def expire_options(agency_equipment_type)
    Rails.cache.delete("agency_equipment_type_options/#{agency_equipment_type.agency_id}")
  end
end

Мы хотели бы извлечь обратные вызовы after_update, after_delete и after_create в модуль с именем "ExpireOptions"

Модуль должен выглядеть следующим образом (с методом expire_options, оставшимся в исходном свипере):

module ExpireOptions
  def after_update(record)
    expire_options(record)
  end

  def after_delete(record)
    expire_options(record)
  end

  def after_create(record)
    expire_options(record)
  end
end

class AgencyEquipmentTypeSweeper < ActionController::Caching::Sweeper 
  observe AgencyEquipmentType

  include ExpireOptions

  def expire_options(agency_equipment_type)
    Rails.cache.delete("agency_equipment_type_options/#{agency_equipment_type.agency_id}")
  end
end

НО истечение срока действия кеша работает, только если мы явно определяем методы внутри свипера. Есть ли простой способ извлечь эти методы обратного вызова в модуль, и при этом они все еще работают?

2 ответа

Попробуйте с:

module ExpireOptions
  def self.included(base)
    base.class_eval do
      after_update :custom_after_update
      after_delete :custom_after_delete
      after_create :custom_after_create
    end
  end

  def custom_after_update(record)
    expire_options(record)
  end

  def custom_after_delete(record)
    expire_options(record)
  end

  def custom_after_create(record)
    expire_options(record)
  end
end

Я хотел бы попробовать что-то вроде:

module ExpireOptions
  def after_update(record)
    self.send(:expire_options, record)
  end

  def after_delete(record)
    self.send(:expire_options, record)
  end

  def after_create(record)
    self.send(:expire_options, record)
  end
end

Это должно убедиться, что он не пытается вызвать эти методы в модуле, но на self который, надеюсь, будет вызывающим объектом.

Это помогает?

Другие вопросы по тегам