impress/app/models/item_observer.rb

31 lines
756 B
Ruby

class ItemObserver < ActionController::Caching::Sweeper
def after_create(item)
Rails.logger.debug "Item #{item.id} was just created"
expire_newest_items
end
def after_update(item)
Rails.logger.debug "Item #{item.id} was just updated"
expire_cache_for(item)
end
def after_destroy(item)
Rails.logger.debug "Item #{item.id} was just destroyed"
expire_cache_for(item)
end
private
def controller
@controller ||= ActionController::Base.new
end
def expire_cache_for(item)
controller.expire_fragment("items/#{item.id}#item_link_partial")
end
def expire_newest_items
controller.expire_fragment('outfits#new newest_items')
controller.expire_fragment('items#index newest_items')
end
end