我们在 rails 应用程序中有以下清扫器:
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
但是缓存过期只有在我们在清扫器中明确定义方法时才有效。有没有一种简单的方法可以将这些回调方法提取到模块中,并且仍然可以工作?