Skip to content

Commit

Permalink
Update generators to use thor 0.13.6 with simpler source_root handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Apr 30, 2010
1 parent 7b98d2a commit cde168e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 32 deletions.
6 changes: 2 additions & 4 deletions 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
Expand Down
10 changes: 3 additions & 7 deletions activerecord/lib/rails/generators/active_record.rb
Expand Up @@ -8,16 +8,12 @@ module Generators
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
Expand Down
38 changes: 22 additions & 16 deletions railties/lib/rails/generators/base.rb
Expand Up @@ -20,24 +20,19 @@ class Base < Thor::Group

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."
Expand All @@ -47,7 +42,6 @@ def self.desc(description=nil)
# 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:/, ':')
Expand Down Expand Up @@ -200,22 +194,34 @@ def self.remove_hook_for(*names)
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)
options[:default] = default_value_for_option(name, options)
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$/
Expand Down
@@ -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
2 changes: 1 addition & 1 deletion railties/railties.gemspec
Expand Up @@ -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
2 changes: 1 addition & 1 deletion railties/test/generators/actions_test.rb
Expand Up @@ -209,7 +209,7 @@ def test_route_should_add_data_to_the_routes_block_in_config_routes

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

Expand Down

0 comments on commit cde168e

Please sign in to comment.