From bac0826b1fa5f39274d61ad51304548c725a304f Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 24 Sep 2010 15:46:29 +0200 Subject: Refactor namespaced generators to use module_namespacing block helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- Gemfile | 2 ++ .../active_record/model/model_generator.rb | 4 ++-- .../active_record/model/templates/model.rb | 2 ++ .../active_record/model/templates/module.rb | 2 ++ railties/lib/rails/generators/named_base.rb | 23 ++++++++++------------ .../rails/controller/controller_generator.rb | 2 +- .../rails/controller/templates/controller.rb | 2 ++ .../generators/rails/helper/helper_generator.rb | 2 +- .../generators/rails/helper/templates/helper.rb | 2 ++ 9 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Gemfile b/Gemfile index d9f089f7ab..40b317b996 100644 --- a/Gemfile +++ b/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" diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index bb53c489be..960c29c49c 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -17,12 +17,12 @@ module ActiveRecord 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 diff --git a/activerecord/lib/rails/generators/active_record/model/templates/model.rb b/activerecord/lib/rails/generators/active_record/model/templates/model.rb index 21ae29e9f2..5c47f8b241 100644 --- a/activerecord/lib/rails/generators/active_record/model/templates/model.rb +++ b/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 -%> diff --git a/activerecord/lib/rails/generators/active_record/model/templates/module.rb b/activerecord/lib/rails/generators/active_record/model/templates/module.rb index bb4220f038..fca2908080 100644 --- a/activerecord/lib/rails/generators/active_record/model/templates/module.rb +++ b/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 -%> diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 7fae505883..6db294290a 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -17,6 +17,16 @@ module Rails 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 @@ module Rails "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 diff --git a/railties/lib/rails/generators/rails/controller/controller_generator.rb b/railties/lib/rails/generators/rails/controller/controller_generator.rb index 981ce88474..9788c0d0bc 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 - 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 diff --git a/railties/lib/rails/generators/rails/controller/templates/controller.rb b/railties/lib/rails/generators/rails/controller/templates/controller.rb index cda2659e69..c61ea4b510 100644 --- a/railties/lib/rails/generators/rails/controller/templates/controller.rb +++ b/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 @@ class <%= class_name %>Controller < ApplicationController <% end -%> end +<% end -%> diff --git a/railties/lib/rails/generators/rails/helper/helper_generator.rb b/railties/lib/rails/generators/rails/helper/helper_generator.rb index 50d3b46b56..ad66388591 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 - 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 diff --git a/railties/lib/rails/generators/rails/helper/templates/helper.rb b/railties/lib/rails/generators/rails/helper/templates/helper.rb index 3fe2ecdc74..b4173151b4 100644 --- a/railties/lib/rails/generators/rails/helper/templates/helper.rb +++ b/railties/lib/rails/generators/rails/helper/templates/helper.rb @@ -1,2 +1,4 @@ +<% module_namespacing do -%> module <%= class_name %>Helper end +<% end -%> -- cgit v1.2.3