module RSpec::Rails module Observer def create_observer name, content=nil file = observer_file_name(name) unless File.exist?(file) FileUtils.mkdir_p File.dirname(file) content ||= yield if block_given? File.open(file, 'w') do |f| f.puts observer_content(name, content) end end end def observer_content name, content=nil %Q{class #{name.to_s.camelize}Observer < ActiveRecord::Observer #{content} end} end def remove_observer name file = observer_file_name(name) FileUtils.rm_f(file) if File.exist?(file) end def remove_observers *names names.each{|name| remove_observer name } end def observer_file_name name File.join(::Rails.root, "app/models/#{name}_observer.rb") end end end