diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2010-09-23 19:13:07 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-09-24 21:02:24 +0200 |
commit | e83634081a9222446df608106c8df5a6831d64db (patch) | |
tree | f40a69c6b4e863b93d9e0ed1bb9af4cd97cde8a5 /railties/lib/rails/generators/named_base.rb | |
parent | 55b6fa9370dbdac23b0df2bbcb70b2e2d604b410 (diff) | |
download | rails-e83634081a9222446df608106c8df5a6831d64db.tar.gz rails-e83634081a9222446df608106c8df5a6831d64db.tar.bz2 rails-e83634081a9222446df608106c8df5a6831d64db.zip |
Generators are not aware of namespace of isolated engines and applications
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'railties/lib/rails/generators/named_base.rb')
-rw-r--r-- | railties/lib/rails/generators/named_base.rb | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 44f831e6f3..7fae505883 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -5,6 +5,8 @@ module Rails module Generators class NamedBase < Base argument :name, :type => :string + class_option :skip_namespace, :type => :boolean, :default => false, + :desc => "Skip namespace (affects only isolated applications)" def initialize(args, *options) #:nodoc: # Unfreeze name in case it's given as a frozen string @@ -16,15 +18,69 @@ module Rails protected - attr_reader :class_path, :file_name + def indent(content, multiplier = 2) + spaces = " " * multiplier + content.each_line.map {|line| "#{spaces}#{line}" }.join("\n") + end + + def wrap_with_namespace(content) + "module #{namespace.name}\n#{content}\nend\n" + end + + def namespaced_template(source, *args, &block) + inside_namespace do + template(source, *args) do |content| + content = block.call(content) if block_given? + if namespace + content = indent(content) + content = wrap_with_namespace(content) + end + content + end + end + end + + def inside_namespace + @inside_namespace = true if namespaced? + result = yield + @inside_namespace = false + result + end + + def namespace + @namespace ||= if defined?(Rails) && Rails.application + Rails.application.parents.detect { |n| n.respond_to?(:_railtie) } + end + end + + def namespaced? + !options[:skip_namespace] && !!namespace + end + + def inside_namespace? + @inside_namespace + end + + attr_reader :file_name alias :singular_name :file_name def file_path @file_path ||= (class_path + [file_name]).join('/') end + def class_path + inside_namespace? || !namespaced? ? @class_path : namespaced_class_path + end + + def namespaced_class_path + @namespaced_class_path ||= begin + namespace_path = namespace.name.split("::").map {|m| m.underscore } + namespace_path + @class_path + end + end + def class_name - @class_name ||= (class_path + [file_name]).map!{ |m| m.camelize }.join('::') + (class_path + [file_name]).map!{ |m| m.camelize }.join('::') end def human_name |