Skip to content Skip to sidebar Skip to footer

Rails Detect Changes To Files Programatically

I would like to write a method that programatically detects whether any of the files in my rails app have been changed. Is it possible do do something like an MD5 of the whole app

Solution 1:

Have you considered using Guard.

You can programatically do anything whenever a file in your project changes.

There is a nice railscast about it

Solution 2:

There is a simple ruby gem called filewatcher. This is the most advanced example:

require 'filewatcher'

FileWatcher.new(["README.rdoc"]).watch() do |filename, event|
  if(event == :changed)
    puts "File updated: " + filename
  endif(event == :delete)
    puts "File deleted: " + filename
  endif(event == :new)
    puts "New file: " + filename
  endend

Solution 3:

File.ctime is the key. Iterate through all files and create a unique id based on the sum of all their ctimes:

cache_id = 0
Dir.glob('./**/*') do |this_file|
   ignore_files = ['.', '..', "log"]
   ignore_files.each do |ig|
       nextif this_file == ig
   end
   cache_id += File.ctime(this_file).to_i if File.directory?(this_file)
end

Works like a charm, page only re-caches when it needs to, even in development.

Post a Comment for "Rails Detect Changes To Files Programatically"