提交 bac0826b 编写于 作者: P Piotr Sarnacki 提交者: José Valim

Refactor namespaced generators to use module_namespacing block helper

Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 e8363408
......@@ -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"
......
......@@ -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
......
<% 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 -%>
<% module_namespacing do -%>
module <%= class_path.map(&:camelize).join('::') %>
def self.table_name_prefix
'<%= class_path.join('_') %>_'
end
end
<% end -%>
......@@ -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
......
......@@ -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
......
<% module_namespacing do -%>
class <%= class_name %>Controller < ApplicationController
<% for action in actions -%>
def <%= action %>
......@@ -5,3 +6,4 @@ def <%= action %>
<% end -%>
end
<% end -%>
......@@ -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
......
<% module_namespacing do -%>
module <%= class_name %>Helper
end
<% end -%>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册