2009-09-24 17:01:31 -04:00
|
|
|
require 'rails/generators/base'
|
|
|
|
require 'rails/generators/generated_attribute'
|
2009-06-23 10:19:23 -04:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Generators
|
|
|
|
class NamedBase < Base
|
2009-06-23 10:50:21 -04:00
|
|
|
argument :name, :type => :string
|
2010-09-23 13:13:07 -04:00
|
|
|
class_option :skip_namespace, :type => :boolean, :default => false,
|
|
|
|
:desc => "Skip namespace (affects only isolated applications)"
|
2009-06-23 10:50:21 -04:00
|
|
|
|
2011-04-10 00:38:59 -04:00
|
|
|
class_option :old_style_hash, :type => :boolean, :default => false,
|
|
|
|
:desc => "Force using old style hash (:foo => 'bar') on Ruby >= 1.9"
|
|
|
|
|
2009-11-03 21:14:44 -05:00
|
|
|
def initialize(args, *options) #:nodoc:
|
|
|
|
# Unfreeze name in case it's given as a frozen string
|
|
|
|
args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
|
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
|
|
|
|
|
2010-11-15 23:39:28 -05:00
|
|
|
no_tasks do
|
|
|
|
def template(source, *args, &block)
|
|
|
|
inside_template do
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-23 10:50:21 -04:00
|
|
|
protected
|
2010-09-25 07:41:42 -04:00
|
|
|
attr_reader :file_name
|
|
|
|
alias :singular_name :file_name
|
|
|
|
|
|
|
|
# Wrap block with namespace of current application
|
|
|
|
# if namespace exists and is not skipped
|
2010-09-24 09:46:29 -04:00
|
|
|
def module_namespacing(&block)
|
2010-11-15 23:39:28 -05:00
|
|
|
content = capture(&block)
|
|
|
|
content = wrap_with_namespace(content) if namespaced?
|
|
|
|
concat(content)
|
2010-11-03 15:12:08 -04:00
|
|
|
end
|
|
|
|
|
2010-09-23 13:13:07 -04:00
|
|
|
def indent(content, multiplier = 2)
|
|
|
|
spaces = " " * multiplier
|
2010-11-03 15:12:08 -04:00
|
|
|
content = content.each_line.map {|line| "#{spaces}#{line}" }.join
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def wrap_with_namespace(content)
|
2010-12-12 19:28:27 -05:00
|
|
|
content = indent(content).chomp
|
2010-09-23 13:13:07 -04:00
|
|
|
"module #{namespace.name}\n#{content}\nend\n"
|
|
|
|
end
|
|
|
|
|
2010-11-15 23:39:28 -05:00
|
|
|
def inside_template
|
|
|
|
@inside_template = true
|
|
|
|
yield
|
2010-09-25 07:41:42 -04:00
|
|
|
ensure
|
2010-11-15 23:39:28 -05:00
|
|
|
@inside_template = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def inside_template?
|
|
|
|
@inside_template
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def namespace
|
|
|
|
@namespace ||= if defined?(Rails) && Rails.application
|
2010-09-24 17:39:15 -04:00
|
|
|
Rails.application.class.parents.detect { |n| n.respond_to?(:_railtie) }
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespaced?
|
2010-11-14 20:33:25 -05:00
|
|
|
!options[:skip_namespace] && namespace
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
|
2010-01-25 07:13:29 -05:00
|
|
|
def file_path
|
|
|
|
@file_path ||= (class_path + [file_name]).join('/')
|
|
|
|
end
|
|
|
|
|
2010-09-23 13:13:07 -04:00
|
|
|
def class_path
|
2010-11-15 23:39:28 -05:00
|
|
|
inside_template? || !namespaced? ? regular_class_path : namespaced_class_path
|
2010-09-24 15:54:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def regular_class_path
|
|
|
|
@class_path
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def namespaced_class_path
|
|
|
|
@namespaced_class_path ||= begin
|
|
|
|
namespace_path = namespace.name.split("::").map {|m| m.underscore }
|
|
|
|
namespace_path + @class_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-25 07:13:29 -05:00
|
|
|
def class_name
|
2010-09-23 13:13:07 -04:00
|
|
|
(class_path + [file_name]).map!{ |m| m.camelize }.join('::')
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
2010-03-16 19:08:59 -04:00
|
|
|
def human_name
|
|
|
|
@human_name ||= singular_name.humanize
|
|
|
|
end
|
|
|
|
|
2010-01-25 07:13:29 -05:00
|
|
|
def plural_name
|
|
|
|
@plural_name ||= singular_name.pluralize
|
|
|
|
end
|
|
|
|
|
|
|
|
def i18n_scope
|
|
|
|
@i18n_scope ||= file_path.gsub('/', '.')
|
|
|
|
end
|
|
|
|
|
|
|
|
def table_name
|
|
|
|
@table_name ||= begin
|
|
|
|
base = pluralize_table_names? ? plural_name : singular_name
|
|
|
|
(class_path + [base]).join('_')
|
2009-06-23 10:50:21 -04:00
|
|
|
end
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
2009-06-23 10:50:21 -04:00
|
|
|
|
2010-06-19 17:35:54 -04:00
|
|
|
def uncountable?
|
|
|
|
singular_name == plural_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def index_helper
|
2010-06-23 03:03:34 -04:00
|
|
|
uncountable? ? "#{plural_table_name}_index" : plural_table_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def singular_table_name
|
2011-02-07 20:26:42 -05:00
|
|
|
@singular_table_name ||= (pluralize_table_names? ? table_name.singularize : table_name)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def plural_table_name
|
2011-02-07 20:26:42 -05:00
|
|
|
@plural_table_name ||= (pluralize_table_names? ? table_name : table_name.pluralize)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def plural_file_name
|
|
|
|
@plural_file_name ||= file_name.pluralize
|
|
|
|
end
|
|
|
|
|
|
|
|
def route_url
|
|
|
|
@route_url ||= class_path.collect{|dname| "/" + dname }.join('') + "/" + plural_file_name
|
2010-06-19 17:35:54 -04:00
|
|
|
end
|
|
|
|
|
2010-01-25 07:13:29 -05:00
|
|
|
# Tries to retrieve the application name or simple return application.
|
|
|
|
def application_name
|
|
|
|
if defined?(Rails) && Rails.application
|
|
|
|
Rails.application.class.name.split('::').first.underscore
|
2009-06-23 10:50:21 -04:00
|
|
|
else
|
2010-01-25 07:13:29 -05:00
|
|
|
"application"
|
2009-06-23 10:50:21 -04:00
|
|
|
end
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
2009-08-09 06:24:40 -04:00
|
|
|
|
2010-01-25 07:13:29 -05:00
|
|
|
def assign_names!(name) #:nodoc:
|
|
|
|
@class_path = name.include?('/') ? name.split('/') : name.split('::')
|
|
|
|
@class_path.map! { |m| m.underscore }
|
|
|
|
@file_name = @class_path.pop
|
2009-06-23 10:50:21 -04:00
|
|
|
end
|
|
|
|
|
2010-01-25 07:13:29 -05:00
|
|
|
# Convert attributes array into GeneratedAttribute objects.
|
2009-06-26 04:40:02 -04:00
|
|
|
def parse_attributes! #:nodoc:
|
2009-07-21 06:00:02 -04:00
|
|
|
self.attributes = (attributes || []).map do |key_value|
|
|
|
|
name, type = key_value.split(':')
|
2009-06-27 07:03:07 -04:00
|
|
|
Rails::Generators::GeneratedAttribute.new(name, type)
|
2009-06-23 12:12:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-01 14:30:47 -04:00
|
|
|
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-26 04:40:02 -04:00
|
|
|
#
|
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
|
2009-07-01 14:30:47 -04:00
|
|
|
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
|
2009-06-26 04:40:02 -04:00
|
|
|
end
|
2011-04-10 00:27:26 -04:00
|
|
|
|
|
|
|
# Returns Ruby 1.9 style key-value pair if current code is running on
|
|
|
|
# Ruby 1.9.x. Returns the old-style (with hash rocket) otherwise.
|
|
|
|
def key_value(key, value)
|
2011-04-10 00:38:59 -04:00
|
|
|
if options[:old_style_hash] || RUBY_VERSION < '1.9'
|
2011-04-10 00:27:26 -04:00
|
|
|
":#{key} => #{value}"
|
|
|
|
else
|
|
|
|
"#{key}: #{value}"
|
|
|
|
end
|
|
|
|
end
|
2009-06-30 07:25:27 -04:00
|
|
|
end
|
2009-06-23 10:19:23 -04:00
|
|
|
end
|
|
|
|
end
|