aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/generators/base.rb38
-rw-r--r--railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt4
-rw-r--r--railties/railties.gemspec2
-rw-r--r--railties/test/generators/actions_test.rb2
4 files changed, 25 insertions, 21 deletions
diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb
index 0da85ea4a4..766644bbc2 100644
--- a/railties/lib/rails/generators/base.rb
+++ b/railties/lib/rails/generators/base.rb
@@ -20,24 +20,19 @@ module Rails
add_runtime_options!
- # Automatically sets the source root based on the class name.
- #
- def self.source_root
- @_rails_source_root ||= begin
- if base_name && generator_name
- File.expand_path(File.join(base_name, generator_name, 'templates'), File.dirname(__FILE__))
- end
- end
+ # Returns the source root for this generator using default_source_root as default.
+ def self.source_root(path=nil)
+ @_source_root = path if path
+ @_source_root ||= default_source_root
end
# Tries to get the description from a USAGE file one folder above the source
# root otherwise uses a default description.
- #
def self.desc(description=nil)
return super if description
- usage = File.expand_path(File.join(source_root, "..", "USAGE"))
+ usage = source_root && File.expand_path("../USAGE", source_root)
- @desc ||= if File.exist?(usage)
+ @desc ||= if usage && File.exist?(usage)
File.read(usage)
else
"Description:\n Create #{base_name.humanize.downcase} files for #{generator_name} generator."
@@ -47,7 +42,6 @@ module Rails
# Convenience method to get the namespace from the class name. It's the
# same as Thor default except that the Generator at the end of the class
# is removed.
- #
def self.namespace(name=nil)
return super if name
@namespace ||= super.sub(/_generator$/, '').sub(/:generators:/, ':')
@@ -200,7 +194,6 @@ module Rails
end
# Make class option aware of Rails::Generators.options and Rails::Generators.aliases.
- #
def self.class_option(name, options={}) #:nodoc:
options[:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc)
options[:aliases] = default_aliases_for_option(name, options)
@@ -208,14 +201,27 @@ module Rails
super(name, options)
end
+ # Returns the default source root for a given generator. This is used internally
+ # by rails to set its generators source root. If you want to customize your source
+ # 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)
+ path if File.exists?(path)
+ end
+
+ # Returns the base root for a common set of generators. This is used to dynamically
+ # guess the default source root.
+ def self.base_root
+ File.dirname(__FILE__)
+ end
+
# Cache source root and add lib/generators/base/generator/templates to
# source paths.
- #
def self.inherited(base) #:nodoc:
super
- # Cache source root, we need to do this, since __FILE__ is a relative value
- # and can point to wrong directions when inside an specified directory.
+ # Invoke source_root so the default_source_root is set.
base.source_root
if base.name && base.name !~ /Base$/
diff --git a/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt b/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt
index d8757460e4..d0575772bc 100644
--- a/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt
+++ b/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt
@@ -1,5 +1,3 @@
class <%= class_name %>Generator < Rails::Generators::NamedBase
- def self.source_root
- @source_root ||= File.expand_path('../templates', __FILE__)
- end
+ source_root File.expand_path('../templates', __FILE__)
end
diff --git a/railties/railties.gemspec b/railties/railties.gemspec
index 2e54f20e9c..99537d6d24 100644
--- a/railties/railties.gemspec
+++ b/railties/railties.gemspec
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.has_rdoc = false
s.add_dependency('rake', '>= 0.8.3')
- s.add_dependency('thor', '~> 0.13.5')
+ s.add_dependency('thor', '~> 0.13.6')
s.add_dependency('activesupport', version)
s.add_dependency('actionpack', version)
end
diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb
index 44e0640552..e6fab93a87 100644
--- a/railties/test/generators/actions_test.rb
+++ b/railties/test/generators/actions_test.rb
@@ -209,7 +209,7 @@ class ActionsTest < Rails::Generators::TestCase
def test_readme
run_generator
- Rails::Generators::AppGenerator.expects(:source_root).returns(destination_root)
+ Rails::Generators::AppGenerator.expects(:source_root).times(2).returns(destination_root)
assert_match(/Welcome to Rails/, action(:readme, "README"))
end