diff --git a/lib/kete_translatable_content/extensions/controllers/translations_controller.rb b/lib/kete_translatable_content/extensions/controllers/translations_controller.rb index 6eb337f..d2069fa 100644 --- a/lib/kete_translatable_content/extensions/controllers/translations_controller.rb +++ b/lib/kete_translatable_content/extensions/controllers/translations_controller.rb @@ -1,3 +1,32 @@ TranslationsController.class_eval do uses_tiny_mce :options => Kete.translatable_tiny_mce_options, :only => %w{ new edit } + + include WorkerControllerHelpers + + after_filter :expire_locale_cache, :only => [:create, :update] + + private + + # this is the big hammer for translation clearing + # it does the entire locale's cache if a translation changes + # only use on UI element related models + # Kete items (i.e. user generated content) should be done on an item by item basis + def expire_locale_cache + # leave Kete items to be cleared individually + return true if ZOOM_CLASSES.include?(@translatable_class.to_s) + + expire_locale = @translation.locale + method_name = "clear_locale_cache" + options = { :locale_to_clear => expire_locale } + + worker_type = :generic_muted_worker + worker_key = worker_key_for("generic_muted_worker_#{method_name}_#{expire_locale}") + + # only allow a single cache clearing to happen at once + MiddleMan.new_worker( :worker => worker_type, + :worker_key => worker_key ) + + MiddleMan.worker(worker_type, worker_key).async_do_work( :arg => { :method_name => method_name, :options => options } ) + true + end end diff --git a/rails/init.rb b/rails/init.rb index 290913b..dcb18b4 100644 --- a/rails/init.rb +++ b/rails/init.rb @@ -40,6 +40,18 @@ Kete.extensions[:blocks][key] << Proc.new { Kernel.load(ext_path) } end + # extend the Generic Muted Worker to have the expire_locale method + MethodsForGenericMutedWorker.module_eval do + def clear_locale_cache(options = { }) + locale_to_clear = options[:locale_to_clear] + raise unless locale_to_clear + + # WARNING: this relies on the fact that Kete's current caching scheme is file system based + # if an option for Memcache or other caching mechanism is used in the future + # this will need to be updated with logic to figure out correct clearing mechanism + file_path = "#{Rails.root}/tmp/cache/views/#{Kete.site_name}/#{locale_to_clear}" + FileUtils.rm_r(file_path, :force => true) if File.exist?(file_path) && File.directory?(file_path) + end + end end - end