Skip to content

Commit

Permalink
Change inside_namespace method to inside_template and use it in all t…
Browse files Browse the repository at this point in the history
…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
drogus committed Nov 16, 2010
1 parent a820d0a commit ced8ebc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 31 deletions.
@@ -1,4 +1,3 @@
<% without_namespacing do -%>
<%%= form_for(@<%= singular_table_name %>) do |f| %>
<%% if @<%= singular_table_name %>.errors.any? %>
<div id="error_explanation">
Expand All @@ -22,4 +21,3 @@
<%%= f.submit %>
</div>
<%% end %>
<% end -%>
@@ -1,8 +1,6 @@
<% without_namespacing do -%>
<h1>Editing <%= singular_table_name %></h1>

<%%= render 'form' %>

<%%= link_to 'Show', @<%= singular_table_name %> %> |
<%%= link_to 'Back', <%= index_helper %>_path %>
<% end -%>
@@ -1,4 +1,3 @@
<% without_namespacing do -%>
<h1>Listing <%= plural_table_name %></h1>

<table>
Expand Down Expand Up @@ -26,4 +25,3 @@
<br />

<%%= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path %>
<% end -%>
@@ -1,7 +1,5 @@
<% without_namespacing do -%>
<h1>New <%= singular_table_name %></h1>

<%%= render 'form' %>

<%%= link_to 'Back', <%= index_helper %>_path %>
<% end -%>
@@ -1,4 +1,3 @@
<% without_namespacing do -%>
<p id="notice"><%%= notice %></p>

<% for attribute in attributes -%>
Expand All @@ -11,4 +10,3 @@

<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> |
<%%= link_to 'Back', <%= index_helper %>_path %>
<% end -%>
41 changes: 20 additions & 21 deletions railties/lib/rails/generators/named_base.rb
Expand Up @@ -16,24 +16,24 @@ 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

# 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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ced8ebc

Please sign in to comment.