1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/lib/generators/named_base.rb

151 lines
5 KiB
Ruby
Raw Normal View History

2009-06-23 13:10:42 -04:00
require 'generators/base'
require 'generators/generated_attribute'
module Rails
module Generators
class NamedBase < Base
2009-06-23 10:50:21 -04:00
argument :name, :type => :string
attr_reader :class_name, :singular_name, :plural_name, :table_name,
2009-06-30 07:25:27 -04:00
:class_path, :file_path, :class_nesting_depth
2009-06-23 12:12:37 -04:00
2009-06-23 10:50:21 -04:00
alias :file_name :singular_name
def initialize(*args) #:nodoc:
2009-06-23 10:50:21 -04:00
super
2009-06-26 03:53:53 -04:00
assign_names!(self.name)
2009-06-23 12:12:37 -04:00
parse_attributes! if respond_to?(:attributes)
2009-06-23 10:50:21 -04:00
end
protected
def assign_names!(given_name) #:nodoc:
2009-06-30 07:25:27 -04:00
base_name, @class_path, @file_path, class_nesting, @class_nesting_depth = extract_modules(given_name)
class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name)
2009-06-23 10:50:21 -04:00
@table_name = if pluralize_table_names?
2009-06-23 10:50:21 -04:00
plural_name
else
singular_name
end
@table_name.gsub! '/', '_'
2009-06-30 07:25:27 -04:00
if class_nesting.empty?
@class_name = class_name_without_nesting
2009-06-23 10:50:21 -04:00
else
2009-06-30 07:25:27 -04:00
@table_name = class_nesting.underscore << "_" << @table_name
@class_name = "#{class_nesting}::#{class_name_without_nesting}"
2009-06-23 10:50:21 -04:00
end
end
2009-06-23 12:12:37 -04:00
# Convert attributes hash into an array with GeneratedAttribute objects.
#
def parse_attributes! #:nodoc:
2009-06-27 07:03:07 -04:00
self.attributes = (attributes || {}).map do |name, type|
Rails::Generators::GeneratedAttribute.new(name, type)
2009-06-23 12:12:37 -04:00
end
end
2009-06-23 10:50:21 -04:00
# 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) #:nodoc:
2009-06-23 10:50:21 -04:00
modules = name.include?('/') ? name.split('/') : name.split('::')
name = modules.pop
path = modules.map { |m| m.underscore }
file_path = (path + [name.underscore]).join('/')
nesting = modules.map { |m| m.camelize }.join('::')
[name, path, file_path, nesting, modules.size]
end
# Receives name and return camelized, underscored and pluralized names.
#
def inflect_names(name) #:nodoc:
2009-06-23 10:50:21 -04:00
camel = name.camelize
under = camel.underscore
plural = under.pluralize
[camel, under, plural]
end
def pluralize_table_names?
!defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names
end
2009-06-30 07:25:27 -04:00
# 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"
#
2009-06-30 07:25:27 -04:00
# If the generator is invoked with class name Admin, it will check for
# the presence of "AdminObserver".
#
def self.check_class_collision(options={})
define_method :check_class_collision do
name = if self.respond_to?(:controller_class_name) # for ScaffoldBase
2009-06-30 07:25:27 -04:00
controller_class_name
else
class_name
end
class_collisions "#{options[:prefix]}#{name}#{options[:suffix]}"
end
end
2009-06-30 07:25:27 -04:00
end
# Deal with controller names on scaffold. Also provide helpers to deal with
# ActionORM.
2009-06-30 07:25:27 -04:00
#
module ScaffoldBase
2009-06-30 07:25:27 -04:00
def self.included(base) #:nodoc:
base.send :attr_reader, :controller_name, :controller_class_name, :controller_file_name,
:controller_class_path, :controller_file_path
2009-06-30 07:25:27 -04:00
end
# Set controller variables on initialization.
#
def initialize(*args) #:nodoc:
2009-06-30 07:25:27 -04:00
super
@controller_name = name.pluralize
base_name, @controller_class_path, @controller_file_path, class_nesting, class_nesting_depth = extract_modules(@controller_name)
2009-06-30 07:25:27 -04:00
class_name_without_nesting, @controller_file_name, controller_plural_name = inflect_names(base_name)
@controller_class_name = if class_nesting.empty?
class_name_without_nesting
else
"#{class_nesting}::#{class_name_without_nesting}"
end
end
protected
# Loads the ORM::Generators::ActionORM class. This class is responsable
# to tell scaffold entities how to generate an specific method for the
# ORM. Check Rails::Generators::ActionORM for more information.
#
def orm_class
@orm_class ||= begin
unless self.class.class_options[:orm]
raise "You need to have :orm as class option to invoke orm_class and orm_instance"
end
action_orm = "#{options[:orm].to_s.classify}::Generators::ActionORM"
action_orm.constantize
rescue NameError => e
raise Error, "Could not load #{action_orm}, skipping controller. Error: #{e.message}."
end
end
# Initialize ORM::Generators::ActionORM to access instance methods.
#
def orm_instance(name=file_name)
@orm_instance ||= @orm_class.new(name)
end
end
end
end