Skip to content
Browse files

Refactor namespaced generators to use module_namespacing block helper

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent e836340 commit bac0826b1fa5f39274d61ad51304548c725a304f @drogus drogus committed with josevalim
View
2 Gemfile
@@ -8,6 +8,8 @@ end
gem "rails", :path => File.dirname(__FILE__)
+gem "thor", :git => "git://github.com/drogus/thor.git"
+
gem "rake", ">= 0.8.7"
gem "mocha", ">= 0.9.8"
gem "rdoc", ">= 2.5.10"
View
4 activerecord/lib/rails/generators/active_record/model/model_generator.rb
@@ -17,12 +17,12 @@ def create_migration_file
end
def create_model_file
- namespaced_template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
+ template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
end
def create_module_file
return if class_path.empty?
- namespaced_template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
+ template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
end
hook_for :test_framework
View
2 activerecord/lib/rails/generators/active_record/model/templates/model.rb
@@ -1,5 +1,7 @@
+<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
belongs_to :<%= attribute.name %>
<% end -%>
end
+<% end -%>
View
2 activerecord/lib/rails/generators/active_record/model/templates/module.rb
@@ -1,5 +1,7 @@
+<% module_namespacing do -%>
module <%= class_path.map(&:camelize).join('::') %>
def self.table_name_prefix
'<%= class_path.join('_') %>_'
end
end
+<% end -%>
View
23 railties/lib/rails/generators/named_base.rb
@@ -17,6 +17,16 @@ def initialize(args, *options) #:nodoc:
end
protected
+ def module_namespacing(&block)
+ inside_namespace do
+ content = capture(&block)
+ if namespaced?
+ content = indent(content)
+ content = wrap_with_namespace(content)
+ end
+ concat(content)
+ end
+ end
def indent(content, multiplier = 2)
spaces = " " * multiplier
@@ -27,19 +37,6 @@ 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
View
2 railties/lib/rails/generators/rails/controller/controller_generator.rb
@@ -5,7 +5,7 @@ class ControllerGenerator < NamedBase
check_class_collision :suffix => "Controller"
def create_controller_files
- namespaced_template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb")
+ template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb")
end
def add_routes
View
2 railties/lib/rails/generators/rails/controller/templates/controller.rb
@@ -1,3 +1,4 @@
+<% module_namespacing do -%>
class <%= class_name %>Controller < ApplicationController
<% for action in actions -%>
def <%= action %>
@@ -5,3 +6,4 @@ def <%= action %>
<% end -%>
end
+<% end -%>
View
2 railties/lib/rails/generators/rails/helper/helper_generator.rb
@@ -4,7 +4,7 @@ class HelperGenerator < NamedBase
check_class_collision :suffix => "Helper"
def create_helper_files
- namespaced_template 'helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb")
+ template 'helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb")
end
hook_for :test_framework
View
2 railties/lib/rails/generators/rails/helper/templates/helper.rb
@@ -1,2 +1,4 @@
+<% module_namespacing do -%>
module <%= class_name %>Helper
end
+<% end -%>

0 comments on commit bac0826

Please sign in to comment.
Something went wrong with that request. Please try again.