From 4a49ad2fe86e3114cb541a1e6ac3ad4aae6f4df0 Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 27 Dec 2013 21:48:38 -0500 Subject: [PATCH] oh poo, didn't commit these properly with the closet hanger caching :( --- app/models/closet_hanger_observer.rb | 20 ++++++++++++++++++++ app/models/closet_list_observer.rb | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 app/models/closet_hanger_observer.rb create mode 100644 app/models/closet_list_observer.rb diff --git a/app/models/closet_hanger_observer.rb b/app/models/closet_hanger_observer.rb new file mode 100644 index 00000000..b00938e0 --- /dev/null +++ b/app/models/closet_hanger_observer.rb @@ -0,0 +1,20 @@ +class ClosetHangerObserver < ActionController::Caching::Sweeper + extend FragmentExpiration + + def after_create(hanger) + self.class.expire_item_trade_hangers(hanger) if hanger.trading? + end + + def after_update(hanger) + self.class.expire_item_trade_hangers(hanger) if hanger.list_id_changed? + end + + def after_destroy(hanger) + self.class.expire_item_trade_hangers(hanger) if hanger.trading? + end + + def self.expire_item_trade_hangers(hanger) + expire_fragment_in_all_locales("items/#{hanger.item_id} trade_hangers") + expire_fragment_in_all_locales("items/#{hanger.item_id} trade_hangers owned=#{hanger.owned}") + end +end diff --git a/app/models/closet_list_observer.rb b/app/models/closet_list_observer.rb new file mode 100644 index 00000000..a1be4910 --- /dev/null +++ b/app/models/closet_list_observer.rb @@ -0,0 +1,18 @@ +class ClosetListObserver < ActionController::Caching::Sweeper + include FragmentExpiration + + def after_update(list) + expire_all_items_trade_hangers(list) if list.trading_changed? + end + + def before_destroy(list) + # Nullify all the child records explicitly, which will in turn trigger + # their update callbacks and expire their items' caches. This occurs in the + # same transaction as the list's destruction. + list.hangers.each { |h| h.list_id = nil; h.save! } + end + + def expire_all_items_trade_hangers(list) + list.hangers.each { |h| ClosetHangerObserver.expire_item_trade_hangers(h) } + end +end \ No newline at end of file