Ensure generators can be invoked by their shortcut and remove attr_reader tasks.

This commit is contained in:
José Valim 2010-01-18 10:43:10 +01:00
parent 4c00c65c58
commit a2e2e73c5e
6 changed files with 34 additions and 10 deletions

@ -168,6 +168,8 @@ def self.load_paths
# Rails looks for is the first and last parts of the namespace.
#
def self.find_by_namespace(name, base=nil, context=nil) #:nodoc:
base = "rails" unless base || context || name.to_s.include?(?:)
# Mount regexps to lookup
regexps = []
regexps << /^#{base}:[\w:]*#{name}$/ if base
@ -203,8 +205,7 @@ def self.find_by_regexps(regexps, klasses)
# commands.
def self.invoke(namespace, args=ARGV, config={})
names = namespace.to_s.split(':')
if klass = find_by_namespace(names.pop, names.shift || "rails")
if klass = find_by_namespace(names.pop, names.shift)
args << "--help" if klass.arguments.any? { |a| a.required? } && args.empty?
klass.start(args, config)
else

@ -324,9 +324,13 @@ def self.hooks #:nodoc:
# added hook is being used.
#
def self.prepare_for_invocation(name, value) #:nodoc:
return super unless value.is_a?(String) || value.is_a?(Symbol)
if value && constants = self.hooks[name]
value = name if TrueClass === value
Rails::Generators.find_by_namespace(value, *constants)
elsif klass = Rails::Generators.find_by_namespace(value)
klass
else
super
end

@ -6,10 +6,12 @@ module Generators
class NamedBase < Base
argument :name, :type => :string
attr_reader :class_name, :singular_name, :plural_name, :table_name,
:class_path, :file_path, :class_nesting_depth
no_tasks {
attr_reader :class_name, :singular_name, :plural_name, :table_name,
:class_path, :file_path, :class_nesting_depth
alias :file_name :singular_name
alias :file_name :singular_name
}
def initialize(args, *options) #:nodoc:
# Unfreeze name in case it's given as a frozen string

@ -9,10 +9,14 @@ module ResourceHelpers
mattr_accessor :skip_warn
def self.included(base) #:nodoc:
base.send :attr_reader, :controller_name, :controller_class_name, :controller_file_name,
:controller_class_path, :controller_file_path
base.class_eval do
class_option :force_plural, :type => :boolean, :desc => "Forces the use of a plural ModelName"
base.send :class_option, :force_plural, :type => :boolean, :desc => "Forces the use of a plural ModelName"
no_tasks {
attr_reader :controller_name, :controller_class_name, :controller_file_name,
:controller_class_path, :controller_file_path
}
end
end
# Set controller variables on initialization.

@ -1,6 +1,9 @@
require 'generators/generators_test_helper'
require 'rails/generators/rails/app/app_generator'
# TODO This line shouldn't be required
require 'rails/generators/rails/model/model_generator'
class ActionsTest < GeneratorsTestCase
tests Rails::Generators::AppGenerator
arguments [destination_root]
@ -11,6 +14,16 @@ def setup
@svn_plugin_uri = 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
end
def test_invoke_other_generator_with_shortcut
action :invoke, 'model', ['my_model']
assert_file 'app/models/my_model.rb', /MyModel/
end
def test_invoke_other_generator_with_full_namespace
action :invoke, 'rails:generators:model', ['my_model']
assert_file 'app/models/my_model.rb', /MyModel/
end
def test_create_file_should_write_data_to_file_path
action :create_file, 'lib/test_file.rb', 'heres test data'
assert_file 'lib/test_file.rb', 'heres test data'

@ -34,8 +34,8 @@ def test_invoke_with_config_values
Rails::Generators.invoke :model, ["Account"], :behavior => :skip
end
def test_find_by_namespace_without_base_or_context
assert_nil Rails::Generators.find_by_namespace(:model)
def test_find_by_namespace_without_base_or_context_looks_into_rails_namespace
assert Rails::Generators.find_by_namespace(:model)
end
def test_find_by_namespace_with_base