Skip to content

Commit

Permalink
Usage file in generators shouldn't be fetched only based on source_root
Browse files Browse the repository at this point in the history
In case `source_roout` is not set, `default_source_root` is used,
which includes also `templates` directory. If there is no `templates`
directory, `default_source_root` is not available and USAGE will not
be displayed. USAGE should be also checked based on default
directory excluding `templates`.
  • Loading branch information
drogus committed Apr 1, 2012
1 parent 27fc6ec commit 515e1d3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
21 changes: 17 additions & 4 deletions railties/lib/rails/generators/base.rb
Expand Up @@ -31,10 +31,9 @@ def self.source_root(path=nil)
# root otherwise uses a default description.
def self.desc(description=nil)
return super if description
usage = source_root && File.expand_path("../USAGE", source_root)

@desc ||= if usage && File.exist?(usage)
ERB.new(File.read(usage)).result(binding)
@desc ||= if usage_path
ERB.new(File.read(usage_path)).result(binding)
else
"Description:\n Create #{base_name.humanize.downcase} files for #{generator_name} generator."
end
Expand Down Expand Up @@ -207,7 +206,8 @@ def self.class_option(name, options={}) #:nodoc:
# root, you should use source_root.
def self.default_source_root
return unless base_name && generator_name
path = File.expand_path(File.join(base_name, generator_name, 'templates'), base_root)
return unless default_generator_root
path = File.join(default_generator_root, 'templates')
path if File.exists?(path)
end

Expand Down Expand Up @@ -371,6 +371,19 @@ def self.add_shebang_option!
}
end

def self.usage_path
paths = [
source_root && File.expand_path("../USAGE", source_root),
default_generator_root && File.join(default_generator_root, "USAGE")
]
paths.compact.detect { |path| File.exists? path }
end

def self.default_generator_root
path = File.expand_path(File.join(base_name, generator_name), base_root)
path if File.exists?(path)
end

end
end
end
2 changes: 1 addition & 1 deletion railties/test/generators/app_generator_test.rb
Expand Up @@ -316,7 +316,7 @@ def test_usage_read_from_file
end

def test_default_usage
File.expects(:exist?).returns(false)
Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
end

Expand Down
4 changes: 4 additions & 0 deletions railties/test/generators/migration_generator_test.rb
Expand Up @@ -157,4 +157,8 @@ def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove
end
end
end

def test_properly_identifies_usage_file
assert generator_class.send(:usage_path)
end
end

0 comments on commit 515e1d3

Please sign in to comment.