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