Skip to content

Commit

Permalink
Patch for Namespace problem in Scaffold. [rails#4763 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
siddick authored and josevalim committed Jun 23, 2010
1 parent d132dd3 commit 7008911
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 61 deletions.
Expand Up @@ -22,7 +22,7 @@ def create_model_file

def create_module_file
return if class_path.empty?
template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb")
template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
end

hook_for :test_framework
Expand Down
@@ -1,10 +1,10 @@
<%%= form_for(@<%= singular_name %>) do |f| %>
<%% if @<%= singular_name %>.errors.any? %>
<%%= form_for(@<%= singular_table_name %>) do |f| %>
<%% if @<%= singular_table_name %>.errors.any? %>
<div id="error_explanation">
<h2><%%= pluralize(@<%= singular_name %>.errors.count, "error") %> prohibited this <%= singular_name %> from being saved:</h2>
<h2><%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>

<ul>
<%% @<%= singular_name %>.errors.full_messages.each do |msg| %>
<%% @<%= singular_table_name %>.errors.full_messages.each do |msg| %>
<li><%%= msg %></li>
<%% end %>
</ul>
Expand Down
@@ -1,6 +1,6 @@
<h1>Editing <%= singular_name %></h1>
<h1>Editing <%= singular_table_name %></h1>

<%%= render 'form' %>

<%%= link_to 'Show', @<%= singular_name %> %> |
<%%= link_to 'Show', @<%= singular_table_name %> %> |
<%%= link_to 'Back', <%= index_helper %>_path %>
@@ -1,4 +1,4 @@
<h1>Listing <%= plural_name %></h1>
<h1>Listing <%= plural_table_name %></h1>

<table>
<tr>
Expand All @@ -10,18 +10,18 @@
<th></th>
</tr>

<%% @<%= plural_name %>.each do |<%= singular_name %>| %>
<%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
<tr>
<% for attribute in attributes -%>
<td><%%= <%= singular_name %>.<%= attribute.name %> %></td>
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end -%>
<td><%%= link_to 'Show', <%= singular_name %> %></td>
<td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
<td><%%= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
<td><%%= link_to 'Show', <%= singular_table_name %> %></td>
<td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td>
<td><%%= link_to 'Destroy', <%= singular_table_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<%% end %>
</table>

<br />

<%%= link_to 'New <%= human_name %>', new_<%= singular_name %>_path %>
<%%= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path %>
@@ -1,4 +1,4 @@
<h1>New <%= singular_name %></h1>
<h1>New <%= singular_table_name %></h1>

<%%= render 'form' %>

Expand Down
Expand Up @@ -3,10 +3,10 @@
<% for attribute in attributes -%>
<p>
<b><%= attribute.human_name %>:</b>
<%%= @<%= singular_name %>.<%= attribute.name %> %>
<%%= @<%= singular_table_name %>.<%= attribute.name %> %>
</p>

<% end -%>

<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> |
<%%= link_to 'Back', <%= index_helper %>_path %>
18 changes: 17 additions & 1 deletion railties/lib/rails/generators/named_base.rb
Expand Up @@ -51,7 +51,23 @@ def uncountable?
end

def index_helper
uncountable? ? "#{plural_name}_index" : plural_name
uncountable? ? "#{plural_table_name}_index" : plural_table_name
end

def singular_table_name
@singular_table_name ||= table_name.singularize
end

def plural_table_name
@plural_table_name ||= table_name.pluralize
end

def plural_file_name
@plural_file_name ||= file_name.pluralize
end

def route_url
@route_url ||= class_path.collect{|dname| "/" + dname }.join('') + "/" + plural_file_name
end

# Tries to retrieve the application name or simple return application.
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/generators/rails/model/USAGE
Expand Up @@ -40,6 +40,6 @@ Examples:
Module: app/models/admin.rb
Model: app/models/admin/account.rb
Test: test/unit/admin/account_test.rb
Fixtures: test/fixtures/admin_accounts.yml
Fixtures: test/fixtures/admin/accounts.yml
Migration: db/migrate/XXX_add_admin_accounts.rb

Expand Up @@ -18,7 +18,10 @@ class ResourceGenerator < ModelGenerator #metagenerator

def add_resource_route
return if options[:actions].present?
route "resource#{:s unless options[:singleton]} :#{pluralize?(file_name)}"
route_config = class_path.collect{|namespace| "namespace :#{namespace} do " }.join(" ")
route_config << "resource#{:s unless options[:singleton]} :#{pluralize?(file_name)}"
route_config << " end" * class_path.size
route route_config
end

protected
Expand Down
@@ -1,68 +1,68 @@
class <%= controller_class_name %>Controller < ApplicationController
<% unless options[:singleton] -%>
# GET /<%= table_name %>
# GET /<%= table_name %>.xml
# GET <%= route_url %>
# GET <%= route_url %>.xml
def index
@<%= table_name %> = <%= orm_class.all(class_name) %>
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @<%= table_name %> }
format.xml { render :xml => @<%= plural_table_name %> }
end
end
<% end -%>
# GET /<%= table_name %>/1
# GET /<%= table_name %>/1.xml
# GET <%= route_url %>/1
# GET <%= route_url %>/1.xml
def show
@<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @<%= file_name %> }
format.xml { render :xml => @<%= singular_table_name %> }
end
end

# GET /<%= table_name %>/new
# GET /<%= table_name %>/new.xml
# GET <%= route_url %>/new
# GET <%= route_url %>/new.xml
def new
@<%= file_name %> = <%= orm_class.build(class_name) %>
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @<%= file_name %> }
format.xml { render :xml => @<%= singular_table_name %> }
end
end

# GET /<%= table_name %>/1/edit
# GET <%= route_url %>/1/edit
def edit
@<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
end

# POST /<%= table_name %>
# POST /<%= table_name %>.xml
# POST <%= route_url %>
# POST <%= route_url %>.xml
def create
@<%= file_name %> = <%= orm_class.build(class_name, "params[:#{file_name}]") %>
@<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>

respond_to do |format|
if @<%= orm_instance.save %>
format.html { redirect_to(@<%= file_name %>, :notice => '<%= human_name %> was successfully created.') }
format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully created.') }
format.xml { render :xml => @<%= singular_table_name %>, :status => :created, :location => @<%= singular_table_name %> }
else
format.html { render :action => "new" }
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
end
end
end

# PUT /<%= table_name %>/1
# PUT /<%= table_name %>/1.xml
# PUT <%= route_url %>/1
# PUT <%= route_url %>/1.xml
def update
@<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>

respond_to do |format|
if @<%= orm_instance.update_attributes("params[:#{file_name}]") %>
format.html { redirect_to(@<%= file_name %>, :notice => '<%= human_name %> was successfully updated.') }
if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
Expand All @@ -71,10 +71,10 @@ def update
end
end

# DELETE /<%= table_name %>/1
# DELETE /<%= table_name %>/1.xml
# DELETE <%= route_url %>/1
# DELETE <%= route_url %>/1.xml
def destroy
@<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@<%= orm_instance.destroy %>
respond_to do |format|
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/generators/resource_helpers.rb
Expand Up @@ -72,7 +72,7 @@ def orm_class
end

# Initialize ORM::Generators::ActiveModel to access instance methods.
def orm_instance(name=file_name)
def orm_instance(name=singular_table_name)
@orm_instance ||= @orm_class.new(name)
end
end
Expand Down
Expand Up @@ -16,7 +16,7 @@ def create_test_file

def create_fixture_file
if options[:fixture] && options[:fixture_replacement].nil?
template 'fixtures.yml', File.join('test/fixtures', "#{table_name}.yml")
template 'fixtures.yml', File.join('test/fixtures', class_path, "#{plural_file_name}.yml")
end
end
end
Expand Down
Expand Up @@ -2,7 +2,7 @@

class <%= controller_class_name %>ControllerTest < ActionController::TestCase
setup do
@<%= file_name %> = <%= table_name %>(:one)
@<%= singular_table_name %> = <%= table_name %>(:one)
end
<% unless options[:singleton] -%>
Expand All @@ -18,32 +18,32 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
assert_response :success
end
test "should create <%= file_name %>" do
test "should create <%= singular_table_name %>" do
assert_difference('<%= class_name %>.count') do
post :create, :<%= file_name %> => @<%= file_name %>.attributes
post :create, :<%= singular_table_name %> => @<%= singular_table_name %>.attributes
end
assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
end
test "should show <%= file_name %>" do
get :show, :id => @<%= file_name %>.to_param
test "should show <%= singular_table_name %>" do
get :show, :id => @<%= singular_table_name %>.to_param
assert_response :success
end
test "should get edit" do
get :edit, :id => @<%= file_name %>.to_param
get :edit, :id => @<%= singular_table_name %>.to_param
assert_response :success
end
test "should update <%= file_name %>" do
put :update, :id => @<%= file_name %>.to_param, :<%= file_name %> => @<%= file_name %>.attributes
assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
test "should update <%= singular_table_name %>" do
put :update, :id => @<%= singular_table_name %>.to_param, :<%= singular_table_name %> => @<%= singular_table_name %>.attributes
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
end
test "should destroy <%= file_name %>" do
test "should destroy <%= singular_table_name %>" do
assert_difference('<%= class_name %>.count', -1) do
delete :destroy, :id => @<%= file_name %>.to_param
delete :destroy, :id => @<%= singular_table_name %>.to_param
end

assert_redirected_to <%= index_helper %>_path
Expand Down

0 comments on commit 7008911

Please sign in to comment.