2011-03-22 3 views
4

우리 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_delete의 after_update를 추출, 그리고라는 모듈 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 

하지만 캐시 expirati ons는 스위퍼 안에서 메서드를 명시 적으로 정의 할 때만 작동합니다. 모듈에 콜백 메소드를 추출하는 쉬운 방법이 있습니까?

+1

매우 이상합니다. 두 가지 예는 모두 나를 위해 일합니다. 캐시 저장소에 무엇을 사용하고 있습니까? –

+1

현재 코드에서 작동해야합니다. 아무런 변경이 필요 없습니다. include 문이 모든 것을 처리합니다. – Anand

+0

모듈 기반 메소드 after_create 등이 전혀 호출되지 않습니까? – moritz

답변

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 
+0

나는 이것을 얻었습니다 :'included ': 정의되지 않은 메소드 AgencyEquipmentTypeSweeper에 대한'after_update': 클래스 – btelles

+0

어떤 Ror 버전을 사용하고 있습니까? –

0

내가 좋아하는 뭔가를 시도 할 것입니다 : 이것은 모듈에 이러한 메소드를 호출하지 않도록해야

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에 희망이 될 것이다 호출 객체.

도움이 되었습니까?

+0

원래 코드가 작동하고있는 것처럼 보입니다. – btelles

관련 문제