diff options
Diffstat (limited to 'railties/lib/rails')
3 files changed, 60 insertions, 4 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 diff --git a/railties/lib/rails/generators/rails/controller/controller_generator.rb b/railties/lib/rails/generators/rails/controller/controller_generator.rb index 9788c0d0bc..981ce88474 100644 --- a/railties/lib/rails/generators/rails/controller/controller_generator.rb +++ b/railties/lib/rails/generators/rails/controller/controller_generator.rb @@ -5,7 +5,7 @@ module Rails check_class_collision :suffix => "Controller" def create_controller_files - template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb") + namespaced_template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb") end def add_routes diff --git a/railties/lib/rails/generators/rails/helper/helper_generator.rb b/railties/lib/rails/generators/rails/helper/helper_generator.rb index ad66388591..50d3b46b56 100644 --- a/railties/lib/rails/generators/rails/helper/helper_generator.rb +++ b/railties/lib/rails/generators/rails/helper/helper_generator.rb @@ -4,7 +4,7 @@ module Rails check_class_collision :suffix => "Helper" def create_helper_files - template 'helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb") + namespaced_template 'helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb") end hook_for :test_framework |