# Asserts a given file exists. You need to supply an absolute path or a path relative # to the configured destination: # # generator.should have_generated_file "app/controller/products_controller.rb" # module RSpec::FileMatchers class GenerateFile attr_accessor :relative_path def initialize(relative_path, type = nil) @relative_path = relative_rails_file(relative_path, type) end def matches?(generator, &block) file = File.expand_path(relative_path, generator.class.destination_root) file_exists = File.exists?(file) if block && file_exists read = File.read(file) ruby_content = read.extend(RSpec::RubyContent::Helpers) yield ruby_content else file_exists end end def failure_message "Expected file #{relative_path} to have been generated, but it was not" end def negative_failure_message "Did not expected file #{relative_path} to have been generated, but it was" end protected def relative_rails_file path, type = nil path = path.to_s f_name = file_name(path, type) return File.join(::Rails.root, base_dir(type), folder(type), f_name) if type File.join(::Rails.root, path) end def file_name path, type return "#{path}#{postfix(type)}.rb" if !path.include? '.' path end def postfix type "_#{type}" if [:helper, :controller, :observer].include?(type) end def folder type case type when :observer 'models' else type.to_s.pluralize end end def base_dir type case type when :model, :controller, :view, :helper, :observer 'app' when :migration 'db' when :javascript, :stylesheet 'public' when :initializer, :locale 'config' else '' end end end def generate_file(relative, type = nil) GenerateFile.new(relative, type) end def generate_model(relative) GenerateFile.new(relative, :model) end def generate_controller(relative) puts "Generate controller: #{relative}" GenerateFile.new(relative, :controller) end def generate_helper(relative) GenerateFile.new(relative, :helper) end def generate_view(relative, action, ext) GenerateFile.new("#{relative}/#{action}.#{ext}", :view) end def generate_observer(relative) GenerateFile.new(relative, :observer) end end