Module: ActionController::Caching::Pages::ClassMethods

Defined in:
lib/cache_trasher.rb

Instance Method Summary collapse

Instance Method Details

#trash_cache(path, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cache_trasher.rb', line 5

def trash_cache(path, options)
  puts "[CacheTrasher] ActionController::Base.perform_caching is disabled, exiting." and return unless perform_caching

  # check if the action or controller has been saved as a cached file
  # as opposed to just a directory.
  paths = [path.chomp('/')]
  # if options includes format(s), then use those, otherwise use the standard page_cache_extension to pick stuff up
  if options[:format]
    options[:format].to_a.each do |format|
      paths << "#{paths[0]}.#{format}" if File.exist?("#{page_cache_directory}/#{paths[0]}.#{format}")
    end
  else
    paths << "#{paths[0]}#{page_cache_extension}" if File.exist?("#{page_cache_directory}/#{paths[0]}#{page_cache_extension}")
  end
  paths.each do |p|
    p = File.join(page_cache_directory, p)
    unless [p, p.chomp, p.chomp('/'), p.chomp('/*')].include? Rails.public_path 
      puts "[CacheTrasher] Removing cache file or directory: #{p}"
      begin
        FileUtils.rm_r(p) 
        puts "[CacheTrasher] Expired #{p}"
      rescue Errno::ENOENT
        puts "[CacheTrasher] Failed to expire #{p}"
      end
    else
      # drop a long message into the log if they try to delete RAILS_ROOT/public
      puts "[CacheTrasher] Something is misconfigured.  Check your environment log for more info."
      File.open("#{RAILS_ROOT}/vendor/plugins/cache_trasher/warning.txt", 'r') { |f| logger.info f.read }
    end
  end
end