From ced8ebcee0fa09939f82cacc59a864f5625e4bd5 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 15 Nov 2010 22:39:28 -0600 Subject: [PATCH] Change inside_namespace method to inside_template and use it in all templates by default The initial implementation of namespacing was based on wrong assumptions. Namespacing path and class names in templates should be based on current namespace and skip_namespace attribute, but it should be not necessary to wrap content on all the templates into additional block methods. --- .../erb/scaffold/templates/_form.html.erb | 2 - .../erb/scaffold/templates/edit.html.erb | 2 - .../erb/scaffold/templates/index.html.erb | 2 - .../erb/scaffold/templates/new.html.erb | 2 - .../erb/scaffold/templates/show.html.erb | 2 - railties/lib/rails/generators/named_base.rb | 41 +++++++++---------- 6 files changed, 20 insertions(+), 31 deletions(-) diff --git a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb index c8ee939ad7920..d12b2ff0e5a6d 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb @@ -1,4 +1,3 @@ -<% without_namespacing do -%> <%%= form_for(@<%= singular_table_name %>) do |f| %> <%% if @<%= singular_table_name %>.errors.any? %>
@@ -22,4 +21,3 @@ <%%= f.submit %>
<%% end %> -<% end -%> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb index d1bfcbc429966..e58b9fbd08906 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb @@ -1,8 +1,6 @@ -<% without_namespacing do -%>

Editing <%= singular_table_name %>

<%%= render 'form' %> <%%= link_to 'Show', @<%= singular_table_name %> %> | <%%= link_to 'Back', <%= index_helper %>_path %> -<% end -%> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb index 435d126ee4d3f..4c46db4d67449 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb @@ -1,4 +1,3 @@ -<% without_namespacing do -%>

Listing <%= plural_table_name %>

@@ -26,4 +25,3 @@
<%%= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path %> -<% end -%> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb index fe4d0971c4b1d..02ae4d015eced 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb @@ -1,7 +1,5 @@ -<% without_namespacing do -%>

New <%= singular_table_name %>

<%%= render 'form' %> <%%= link_to 'Back', <%= index_helper %>_path %> -<% end -%> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb index bc3a87b99fd38..c0e5ccff1e2a6 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb @@ -1,4 +1,3 @@ -<% without_namespacing do -%>

<%%= notice %>

<% for attribute in attributes -%> @@ -11,4 +10,3 @@ <%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> | <%%= link_to 'Back', <%= index_helper %>_path %> -<% end -%> diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 8fddf1ab72b6e..e0dde4360f220 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -16,6 +16,14 @@ def initialize(args, *options) #:nodoc: parse_attributes! if respond_to?(:attributes) end + no_tasks do + def template(source, *args, &block) + inside_template do + super + end + end + end + protected attr_reader :file_name alias :singular_name :file_name @@ -23,17 +31,9 @@ def initialize(args, *options) #:nodoc: # Wrap block with namespace of current application # if namespace exists and is not skipped def module_namespacing(&block) - inside_namespace do - content = capture(&block) - content = wrap_with_namespace(content) if namespaced? - concat(content) - end - end - - def without_namespacing(&block) - inside_namespace do - concat(capture(&block)) - end + content = capture(&block) + content = wrap_with_namespace(content) if namespaced? + concat(content) end def indent(content, multiplier = 2) @@ -46,12 +46,15 @@ def wrap_with_namespace(content) "module #{namespace.name}\n#{content}\nend\n" end - def inside_namespace - @inside_namespace = true if namespaced? - result = yield - result + def inside_template + @inside_template = true + yield ensure - @inside_namespace = false + @inside_template = false + end + + def inside_template? + @inside_template end def namespace @@ -64,16 +67,12 @@ def namespaced? !options[:skip_namespace] && namespace end - def inside_namespace? - @inside_namespace - end - def file_path @file_path ||= (class_path + [file_name]).join('/') end def class_path - inside_namespace? || !namespaced? ? regular_class_path : namespaced_class_path + inside_template? || !namespaced? ? regular_class_path : namespaced_class_path end def regular_class_path