aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionmailer/lib/rails/generators/mailer/mailer_generator.rb6
-rw-r--r--activerecord/lib/rails/generators/active_record.rb10
-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
6 files changed, 30 insertions, 32 deletions
diff --git a/actionmailer/lib/rails/generators/mailer/mailer_generator.rb b/actionmailer/lib/rails/generators/mailer/mailer_generator.rb
index 8f47539f42..dd7fa640c9 100644
--- a/actionmailer/lib/rails/generators/mailer/mailer_generator.rb
+++ b/actionmailer/lib/rails/generators/mailer/mailer_generator.rb
@@ -1,13 +1,11 @@
module Rails
module Generators
class MailerGenerator < NamedBase
+ source_root File.expand_path("../templates", __FILE__)
+
argument :actions, :type => :array, :default => [], :banner => "method method"
check_class_collision
- def self.source_root
- File.expand_path("../templates", __FILE__)
- end
-
def create_mailer_file
template "mailer.rb", File.join('app/mailers', class_path, "#{file_name}.rb")
end
diff --git a/activerecord/lib/rails/generators/active_record.rb b/activerecord/lib/rails/generators/active_record.rb
index d2b1e86857..5d8a8e81bc 100644
--- a/activerecord/lib/rails/generators/active_record.rb
+++ b/activerecord/lib/rails/generators/active_record.rb
@@ -8,16 +8,12 @@ module ActiveRecord
class Base < Rails::Generators::NamedBase #:nodoc:
include Rails::Generators::Migration
- def self.source_root
- @_ar_source_root ||= begin
- if base_name && generator_name
- File.expand_path(File.join(base_name, generator_name, 'templates'), File.dirname(__FILE__))
- end
- end
+ # Set the current directory as base for the inherited generators.
+ def self.base_root
+ File.dirname(__FILE__)
end
# Implement the required interface for Rails::Generators::Migration.
- #
def self.next_migration_number(dirname) #:nodoc:
next_migration_number = current_migration_number(dirname) + 1
if ActiveRecord::Base.timestamped_migrations
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