2011-05-07 07:35:04 -04:00
|
|
|
require 'active_support/core_ext/module/introspection'
|
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
|
2012-10-14 06:03:39 -04:00
|
|
|
argument :name, type: :string
|
|
|
|
class_option :skip_namespace, type: :boolean, default: false,
|
|
|
|
desc: "Skip namespace (affects only isolated applications)"
|
2009-06-23 10:50:21 -04:00
|
|
|
|
2009-11-03 21:14:44 -05:00
|
|
|
def initialize(args, *options) #:nodoc:
|
2011-05-18 07:48:54 -04:00
|
|
|
@inside_template = nil
|
2009-11-03 21:14:44 -05:00
|
|
|
# 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
|
2012-04-19 10:29:40 -04:00
|
|
|
content = content.each_line.map {|line| line.blank? ? 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
|
2011-06-07 05:21:38 -04:00
|
|
|
Rails::Generators.namespace
|
2010-09-23 13:13:07 -04:00
|
|
|
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
|
|
|
|
|
Fix generators to help with ambiguous `ApplicationController` issue
In development mode, dependencies are loaded dynamically at runtime,
using `const_missing`. Because of that, when one of the constants is
already loaded and `const_missing` is not triggered, user can end up
with unexpected results.
Given such file in an Engine:
```ruby
module Blog
class PostsController < ApplicationController
end
end
```
If you load it first, before loading any application files, it will
correctly load `Blog::ApplicationController`, because second line will
hit `const_missing`. However if you load `ApplicationController` first,
the constant will be loaded already, `const_missing` hook will not be
fired and in result `PostsController` will inherit from
`ApplicationController` instead of `Blog::ApplicationController`.
Since it can't be fixed in `AS::Dependencies`, the easiest fix is to
just explicitly load application controller.
closes #6413
2012-05-20 19:19:11 -04:00
|
|
|
def namespaced_file_path
|
|
|
|
@namespaced_file_path ||= namespaced_class_path.join("/")
|
|
|
|
end
|
|
|
|
|
2010-09-23 13:13:07 -04:00
|
|
|
def namespaced_class_path
|
2012-06-06 05:27:55 -04:00
|
|
|
@namespaced_class_path ||= [namespaced_path] + @class_path
|
2012-06-06 05:02:11 -04:00
|
|
|
end
|
|
|
|
|
2012-06-06 05:27:55 -04:00
|
|
|
def namespaced_path
|
|
|
|
@namespaced_path ||= namespace.name.split("::").map {|m| m.underscore }[0]
|
2010-09-23 13:13:07 -04:00
|
|
|
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
|
2012-04-03 09:16:09 -04:00
|
|
|
@i18n_scope ||= file_path.tr('/', '.')
|
2010-01-25 07:13:29 -05:00
|
|
|
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
|
2012-06-04 18:10:57 -04:00
|
|
|
@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:
|
2011-08-17 05:16:04 -04:00
|
|
|
self.attributes = (attributes || []).map do |attr|
|
2011-12-24 04:38:19 -05:00
|
|
|
Rails::Generators::GeneratedAttribute.parse(attr)
|
2009-06-23 12:12:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-09 13:53:23 -05:00
|
|
|
def attributes_names
|
|
|
|
@attributes_names ||= attributes.each_with_object([]) do |a, names|
|
2012-12-09 15:01:03 -05:00
|
|
|
names << a.column_name
|
2012-12-09 13:53:23 -05:00
|
|
|
names << "#{a.name}_type" if a.polymorphic?
|
|
|
|
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
|
|
|
|
#
|
2012-10-14 19:00:57 -04:00
|
|
|
# check_class_collision suffix: "Decorator"
|
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
|
2012-10-14 19:00:57 -04:00
|
|
|
# the presence of "AdminDecorator".
|
2009-06-30 07:25:27 -04:00
|
|
|
#
|
|
|
|
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
|
2009-06-30 07:25:27 -04:00
|
|
|
end
|
2009-06-23 10:19:23 -04:00
|
|
|
end
|
|
|
|
end
|