Clean up class collisions check and a class method helper.
This commit is contained in:
parent
6bd101188e
commit
2f3681dfc2
|
@ -45,9 +45,8 @@ module Rails
|
|||
|
||||
protected
|
||||
|
||||
# Check whether the given class names are already taken by Ruby or Rails.
|
||||
# In the future, expand to check other namespaces such as the rest of
|
||||
# the user's app.
|
||||
# Check whether the given class names are already taken by user
|
||||
# application or Ruby on Rails.
|
||||
#
|
||||
def class_collisions(*class_names)
|
||||
return unless behavior == :invoke
|
||||
|
|
|
@ -11,15 +11,43 @@ module Rails
|
|||
|
||||
alias :file_name :singular_name
|
||||
|
||||
def initialize(*args)
|
||||
class << self
|
||||
# Add a class collisions name to be checked on class initialization. You
|
||||
# can supply a hash with a :prefix or :suffix to be tested.
|
||||
#
|
||||
# ==== Examples
|
||||
#
|
||||
# check_class_collision :suffix => "Observer"
|
||||
#
|
||||
# If the generator is invoked with class name Admin, it will check for
|
||||
# the presence of "AdminObserver".
|
||||
#
|
||||
def check_class_collision(options={})
|
||||
@class_collisions = options
|
||||
end
|
||||
|
||||
# Returns the class collisions for this class and retreives one from
|
||||
# superclass. The from_superclass method used below is from Thor.
|
||||
#
|
||||
def class_collisions #:nodoc:
|
||||
@class_collisions ||= from_superclass(:class_collisions, nil)
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(*args) #:nodoc:
|
||||
super
|
||||
assign_names!(self.name)
|
||||
parse_attributes! if respond_to?(:attributes)
|
||||
|
||||
if self.class.class_collisions
|
||||
value = add_prefix_and_suffix(class_name, self.class.class_collisions)
|
||||
class_collisions(value)
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def assign_names!(given_name)
|
||||
def assign_names!(given_name) #:nodoc:
|
||||
self.name, @class_path, @file_path, @class_nesting, @class_nesting_depth = extract_modules(given_name)
|
||||
@class_name_without_nesting, @singular_name, @plural_name = inflect_names(self.name)
|
||||
|
||||
|
@ -40,7 +68,7 @@ module Rails
|
|||
|
||||
# Convert attributes hash into an array with GeneratedAttribute objects.
|
||||
#
|
||||
def parse_attributes!
|
||||
def parse_attributes! #:nodoc:
|
||||
attributes.map! do |name, type|
|
||||
Rails::Generator::GeneratedAttribute.new(name, type)
|
||||
end
|
||||
|
@ -49,7 +77,7 @@ module Rails
|
|||
# Extract modules from filesystem-style or ruby-style path. Both
|
||||
# good/fun/stuff and Good::Fun::Stuff produce the same results.
|
||||
#
|
||||
def extract_modules(name)
|
||||
def extract_modules(name) #:nodoc:
|
||||
modules = name.include?('/') ? name.split('/') : name.split('::')
|
||||
name = modules.pop
|
||||
path = modules.map { |m| m.underscore }
|
||||
|
@ -62,13 +90,19 @@ module Rails
|
|||
|
||||
# Receives name and return camelized, underscored and pluralized names.
|
||||
#
|
||||
def inflect_names(name)
|
||||
def inflect_names(name) #:nodoc:
|
||||
camel = name.camelize
|
||||
under = camel.underscore
|
||||
plural = under.pluralize
|
||||
[camel, under, plural]
|
||||
end
|
||||
|
||||
# Receives a name and add suffix and prefix values frrm hash.
|
||||
#
|
||||
def add_prefix_and_suffix(name, hash) #:nodoc:
|
||||
"#{hash[:prefix]}#{name}#{hash[:suffix]}"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
module Rails
|
||||
module Generators
|
||||
class HelperGenerator < NamedBase
|
||||
def check_class_collisions
|
||||
class_collisions "#{class_name}Helper"
|
||||
end
|
||||
check_class_collision :suffix => "Helper"
|
||||
|
||||
def create_helper_files
|
||||
template 'helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb")
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
module Rails
|
||||
module Generators
|
||||
class IntegrationTestGenerator < NamedBase
|
||||
def check_class_collisions
|
||||
class_collisions "#{class_name}Test"
|
||||
end
|
||||
check_class_collisions :suffix => "Test"
|
||||
|
||||
def create_test_files
|
||||
template 'integration_test.rb', File.join('test/integration', class_path, "#{file_name}_test.rb")
|
||||
|
|
|
@ -2,10 +2,7 @@ module Rails
|
|||
module Generators
|
||||
class MailerGenerator < NamedBase
|
||||
argument :actions, :type => :array, :default => [], :banner => "method method"
|
||||
|
||||
def check_class_collision
|
||||
class_collisions class_name
|
||||
end
|
||||
check_class_collision
|
||||
|
||||
def create_mailer_file
|
||||
template "mailer.rb", File.join('app/models', class_path, "#{file_name}.rb")
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
module Rails
|
||||
module Generators
|
||||
class MetalGenerator < NamedBase
|
||||
def check_class_collision
|
||||
class_collisions class_name
|
||||
end
|
||||
check_class_collision
|
||||
|
||||
def create_file
|
||||
template "metal.rb", "app/metal/#{file_name}.rb"
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
module Rails
|
||||
module Generators
|
||||
class ObserverGenerator < NamedBase
|
||||
def check_class_collision
|
||||
class_collisions "#{class_name}Observer"
|
||||
end
|
||||
check_class_collision :suffix => "Observer"
|
||||
|
||||
def create_observer_file
|
||||
template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
module Rails
|
||||
module Generators
|
||||
class PerformanceTestGenerator < NamedBase
|
||||
def check_class_collisions
|
||||
class_collisions "#{class_name}Test"
|
||||
end
|
||||
class PerformanceTestGenerator < NamedBase
|
||||
check_class_collision :suffix => "Test"
|
||||
|
||||
def create_test_files
|
||||
template 'performance_test.rb', File.join('test/performance', class_path, "#{file_name}_test.rb")
|
||||
end
|
||||
end
end
|
||||
def create_test_files
|
||||
template 'performance_test.rb', File.join('test/performance', class_path, "#{file_name}_test.rb")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,9 +7,7 @@ module Rails
|
|||
class_option :with_generator, :type => :boolean, :aliases => "-g", :default => false,
|
||||
:desc => "When supplied creates generator base files."
|
||||
|
||||
def check_class_collision
|
||||
class_collisions class_name
|
||||
end
|
||||
check_class_collision
|
||||
|
||||
def create_root
|
||||
self.root = File.expand_path("vendor/plugins/#{file_name}", root)
|
||||
|
|
|
@ -3,6 +3,7 @@ require 'generators/named_base'
|
|||
module TestUnit
|
||||
module Generators
|
||||
class Base < Rails::Generators::NamedBase
|
||||
check_class_collision :suffix => "Test"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
module TestUnit
|
||||
module Generators
|
||||
class HelperGenerator < Base
|
||||
def check_class_collisions
|
||||
class_collisions "#{class_name}HelperTest"
|
||||
end
|
||||
check_class_collision :suffix => "HelperTest"
|
||||
|
||||
def create_helper_files
|
||||
template 'helper_test.rb', File.join('test/unit/helpers', class_path, "#{file_name}_helper_test.rb")
|
||||
|
|
|
@ -3,10 +3,6 @@ module TestUnit
|
|||
class MailerGenerator < Base
|
||||
argument :actions, :type => :array, :default => [], :banner => "method method"
|
||||
|
||||
def check_class_collisions
|
||||
class_collisions "#{class_name}Test"
|
||||
end
|
||||
|
||||
def create_test_files
|
||||
template "unit_test.rb", File.join('test', 'unit', class_path, "#{file_name}_test.rb")
|
||||
end
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
module TestUnit
|
||||
module Generators
|
||||
class ObserverGenerator < Base
|
||||
def check_class_collisions
|
||||
class_collisions class_name, "#{class_name}Test"
|
||||
end
|
||||
|
||||
def create_test_files
|
||||
template 'unit_test.rb', File.join('test', 'unit', class_path, "#{file_name}_observer_test.rb")
|
||||
end
|
||||
|
|
|
@ -4,6 +4,7 @@ require 'generators/rails/helper/helper_generator'
|
|||
require 'generators/test_unit/helper/helper_generator'
|
||||
|
||||
ObjectHelper = Class.new
|
||||
AnotherObjectHelperTest = Class.new
|
||||
|
||||
class HelperGeneratorTest < GeneratorsTestCase
|
||||
|
||||
|
@ -27,6 +28,11 @@ class HelperGeneratorTest < GeneratorsTestCase
|
|||
assert_match /The name 'ObjectHelper' is either already used in your application or reserved/, content
|
||||
end
|
||||
|
||||
def test_check_class_collision_on_tests
|
||||
content = capture(:stderr){ run_generator ["another_object"] }
|
||||
assert_match /The name 'AnotherObjectHelperTest' is either already used in your application or reserved/, content
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def run_generator(args=["admin"])
|
||||
|
|
Loading…
Reference in New Issue