Skip to content
This repository
Browse code

Change inside_namespace method to inside_template and use it in all t…

…emplates 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.
  • Loading branch information...
commit ced8ebcee0fa09939f82cacc59a864f5625e4bd5 1 parent a820d0a
Piotr Sarnacki authored
2  railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb
... ...
@@ -1,4 +1,3 @@
1  
-<% without_namespacing do -%>
2 1
 <%%= form_for(@<%= singular_table_name %>) do |f| %>
3 2
   <%% if @<%= singular_table_name %>.errors.any? %>
4 3
     <div id="error_explanation">
@@ -22,4 +21,3 @@
22 21
     <%%= f.submit %>
23 22
   </div>
24 23
 <%% end %>
25  
-<% end -%>
2  railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb
... ...
@@ -1,8 +1,6 @@
1  
-<% without_namespacing do -%>
2 1
 <h1>Editing <%= singular_table_name %></h1>
3 2
 
4 3
 <%%= render 'form' %>
5 4
 
6 5
 <%%= link_to 'Show', @<%= singular_table_name %> %> |
7 6
 <%%= link_to 'Back', <%= index_helper %>_path %>
8  
-<% end -%>
2  railties/lib/rails/generators/erb/scaffold/templates/index.html.erb
... ...
@@ -1,4 +1,3 @@
1  
-<% without_namespacing do -%>
2 1
 <h1>Listing <%= plural_table_name %></h1>
3 2
 
4 3
 <table>
@@ -26,4 +25,3 @@
26 25
 <br />
27 26
 
28 27
 <%%= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path %>
29  
-<% end -%>
2  railties/lib/rails/generators/erb/scaffold/templates/new.html.erb
... ...
@@ -1,7 +1,5 @@
1  
-<% without_namespacing do -%>
2 1
 <h1>New <%= singular_table_name %></h1>
3 2
 
4 3
 <%%= render 'form' %>
5 4
 
6 5
 <%%= link_to 'Back', <%= index_helper %>_path %>
7  
-<% end -%>
2  railties/lib/rails/generators/erb/scaffold/templates/show.html.erb
... ...
@@ -1,4 +1,3 @@
1  
-<% without_namespacing do -%>
2 1
 <p id="notice"><%%= notice %></p>
3 2
 
4 3
 <% for attribute in attributes -%>
@@ -11,4 +10,3 @@
11 10
 
12 11
 <%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> |
13 12
 <%%= link_to 'Back', <%= index_helper %>_path %>
14  
-<% end -%>
41  railties/lib/rails/generators/named_base.rb
@@ -16,6 +16,14 @@ def initialize(args, *options) #:nodoc:
16 16
         parse_attributes! if respond_to?(:attributes)
17 17
       end
18 18
 
  19
+      no_tasks do
  20
+        def template(source, *args, &block)
  21
+          inside_template do
  22
+            super
  23
+          end
  24
+        end
  25
+      end
  26
+
19 27
       protected
20 28
         attr_reader :file_name
21 29
         alias :singular_name :file_name
@@ -23,17 +31,9 @@ def initialize(args, *options) #:nodoc:
23 31
         # Wrap block with namespace of current application
24 32
         # if namespace exists and is not skipped
25 33
         def module_namespacing(&block)
26  
-          inside_namespace do
27  
-            content = capture(&block)
28  
-            content = wrap_with_namespace(content) if namespaced?
29  
-            concat(content)
30  
-          end
31  
-        end
32  
-
33  
-        def without_namespacing(&block)
34  
-          inside_namespace do
35  
-            concat(capture(&block))
36  
-          end
  34
+          content = capture(&block)
  35
+          content = wrap_with_namespace(content) if namespaced?
  36
+          concat(content)
37 37
         end
38 38
 
39 39
         def indent(content, multiplier = 2)
@@ -46,12 +46,15 @@ def wrap_with_namespace(content)
46 46
           "module #{namespace.name}\n#{content}\nend\n"
47 47
         end
48 48
 
49  
-        def inside_namespace
50  
-          @inside_namespace = true if namespaced?
51  
-          result = yield
52  
-          result
  49
+        def inside_template
  50
+          @inside_template = true
  51
+          yield
53 52
         ensure
54  
-          @inside_namespace = false
  53
+          @inside_template = false
  54
+        end
  55
+
  56
+        def inside_template?
  57
+          @inside_template
55 58
         end
56 59
 
57 60
         def namespace
@@ -64,16 +67,12 @@ def namespaced?
64 67
           !options[:skip_namespace] && namespace
65 68
         end
66 69
 
67  
-        def inside_namespace?
68  
-          @inside_namespace
69  
-        end
70  
-
71 70
         def file_path
72 71
           @file_path ||= (class_path + [file_name]).join('/')
73 72
         end
74 73
 
75 74
         def class_path
76  
-          inside_namespace? || !namespaced? ? regular_class_path : namespaced_class_path
  75
+          inside_template? || !namespaced? ? regular_class_path : namespaced_class_path
77 76
         end
78 77
 
79 78
         def regular_class_path

0 notes on commit ced8ebc

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