aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorLisa Ugray <lisa.ugray@shopify.com>2017-07-19 10:28:18 -0400
committerLisa Ugray <lisa.ugray@shopify.com>2017-07-19 13:25:03 -0400
commitfee24f2189b53c19082770b5b8a62d4a5a0b830c (patch)
treeeff09c7f1f828afbdc5046ca99719ef7093c587f /railties
parent1f7f872ac6c8b57af6e0117bde5f6c38d0bae923 (diff)
downloadrails-fee24f2189b53c19082770b5b8a62d4a5a0b830c.tar.gz
rails-fee24f2189b53c19082770b5b8a62d4a5a0b830c.tar.bz2
rails-fee24f2189b53c19082770b5b8a62d4a5a0b830c.zip
Move generator namespacing to Base
Generators need to be aware of namespace to work properly in engines. Methods to help with this lived in Rails::Generators::NamedBase. Moving them to Rails::Generators::Base (which NamedBase inherits from) allows a follow-up in which a namespace aware unnamed generator can be created.
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/generators/base.rb37
-rw-r--r--railties/lib/rails/generators/named_base.rb36
2 files changed, 37 insertions, 36 deletions
diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb
index e7f51dba99..9705af8b40 100644
--- a/railties/lib/rails/generators/base.rb
+++ b/railties/lib/rails/generators/base.rb
@@ -16,6 +16,9 @@ module Rails
include Thor::Actions
include Rails::Generators::Actions
+ class_option :skip_namespace, type: :boolean, default: false,
+ desc: "Skip namespace (affects only isolated applications)"
+
add_runtime_options!
strict_args_position!
@@ -271,6 +274,40 @@ module Rails
end
end
+ # Wrap block with namespace of current application
+ # if namespace exists and is not skipped
+ def module_namespacing(&block) # :doc:
+ content = capture(&block)
+ content = wrap_with_namespace(content) if namespaced?
+ concat(content)
+ end
+
+ def indent(content, multiplier = 2) # :doc:
+ spaces = " " * multiplier
+ content.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join
+ end
+
+ def wrap_with_namespace(content) # :doc:
+ content = indent(content).chomp
+ "module #{namespace.name}\n#{content}\nend\n"
+ end
+
+ def namespace # :doc:
+ Rails::Generators.namespace
+ end
+
+ def namespaced? # :doc:
+ !options[:skip_namespace] && namespace
+ end
+
+ def namespace_dirs
+ @namespace_dirs ||= namespace.name.split("::").map(&:underscore)
+ end
+
+ def namespaced_path # :doc:
+ @namespaced_path ||= namespace_dirs.join("/")
+ end
+
# Use Rails default banner.
def self.banner # :doc:
"rails generate #{namespace.sub(/^rails:/, '')} #{arguments.map(&:usage).join(' ')} [options]".gsub(/\s+/, " ")
diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb
index d63a5b0c30..dc1d6cab9d 100644
--- a/railties/lib/rails/generators/named_base.rb
+++ b/railties/lib/rails/generators/named_base.rb
@@ -6,8 +6,6 @@ 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:
@inside_template = nil
@@ -45,24 +43,6 @@ module Rails
file_name
end
- # Wrap block with namespace of current application
- # if namespace exists and is not skipped
- def module_namespacing(&block) # :doc:
- content = capture(&block)
- content = wrap_with_namespace(content) if namespaced?
- concat(content)
- end
-
- def indent(content, multiplier = 2) # :doc:
- spaces = " " * multiplier
- content.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join
- end
-
- def wrap_with_namespace(content) # :doc:
- content = indent(content).chomp
- "module #{namespace.name}\n#{content}\nend\n"
- end
-
def inside_template # :doc:
@inside_template = true
yield
@@ -74,18 +54,6 @@ module Rails
@inside_template
end
- def namespace # :doc:
- Rails::Generators.namespace
- end
-
- def namespaced? # :doc:
- !options[:skip_namespace] && namespace
- end
-
- def namespace_dirs
- @namespace_dirs ||= namespace.name.split("::").map(&:underscore)
- end
-
def file_path # :doc:
@file_path ||= (class_path + [file_name]).join("/")
end
@@ -102,10 +70,6 @@ module Rails
@namespaced_class_path ||= namespace_dirs + @class_path
end
- def namespaced_path # :doc:
- @namespaced_path ||= namespace_dirs.join("/")
- end
-
def class_name # :doc:
(class_path + [file_name]).map!(&:camelize).join("::")
end