mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Patch for Namespace problem in Scaffold. [#4763 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
committed by
José Valim
parent
d132dd3352
commit
7008911222
@@ -22,7 +22,7 @@ module ActiveRecord
|
||||
|
||||
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
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
@@ -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' %>
|
||||
|
||||
|
||||
@@ -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 %>
|
||||
|
||||
@@ -51,7 +51,23 @@ module Rails
|
||||
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.
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -18,7 +18,10 @@ module Rails
|
||||
|
||||
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
|
||||
|
||||
@@ -1,53 +1,53 @@
|
||||
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 }
|
||||
@@ -55,14 +55,14 @@ class <%= controller_class_name %>Controller < ApplicationController
|
||||
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" }
|
||||
@@ -71,10 +71,10 @@ class <%= controller_class_name %>Controller < ApplicationController
|
||||
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|
|
||||
|
||||
@@ -72,7 +72,7 @@ module Rails
|
||||
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
|
||||
|
||||
@@ -16,7 +16,7 @@ module TestUnit
|
||||
|
||||
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
|
||||
|
||||
@@ -2,7 +2,7 @@ require 'test_helper'
|
||||
|
||||
class <%= controller_class_name %>ControllerTest < ActionController::TestCase
|
||||
setup do
|
||||
@<%= file_name %> = <%= table_name %>(:one)
|
||||
@<%= singular_table_name %> = <%= table_name %>(:one)
|
||||
end
|
||||
|
||||
<% unless options[:singleton] -%>
|
||||
@@ -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
|
||||
|
||||
@@ -110,4 +110,110 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
|
||||
# Stylesheets (should not be removed)
|
||||
assert_file "public/stylesheets/scaffold.css"
|
||||
end
|
||||
|
||||
def test_scaffold_with_namespace_on_invoke
|
||||
run_generator [ "admin/role", "name:string", "description:string" ]
|
||||
|
||||
# Model
|
||||
assert_file "app/models/admin.rb", /module Admin/
|
||||
assert_file "app/models/admin/role.rb", /class Admin::Role < ActiveRecord::Base/
|
||||
assert_file "test/unit/admin/role_test.rb", /class Admin::RoleTest < ActiveSupport::TestCase/
|
||||
assert_file "test/fixtures/admin/roles.yml"
|
||||
assert_migration "db/migrate/create_admin_roles.rb"
|
||||
|
||||
# Route
|
||||
assert_file "config/routes.rb" do |route|
|
||||
assert_match /namespace :admin do resources :roles end$/, route
|
||||
end
|
||||
|
||||
# Controller
|
||||
assert_file "app/controllers/admin/roles_controller.rb" do |content|
|
||||
assert_match /class Admin::RolesController < ApplicationController/, content
|
||||
|
||||
assert_instance_method :index, content do |m|
|
||||
assert_match /@admin_roles = Admin::Role\.all/, m
|
||||
end
|
||||
|
||||
assert_instance_method :show, content do |m|
|
||||
assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m
|
||||
end
|
||||
|
||||
assert_instance_method :new, content do |m|
|
||||
assert_match /@admin_role = Admin::Role\.new/, m
|
||||
end
|
||||
|
||||
assert_instance_method :edit, content do |m|
|
||||
assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m
|
||||
end
|
||||
|
||||
assert_instance_method :create, content do |m|
|
||||
assert_match /@admin_role = Admin::Role\.new\(params\[:admin_role\]\)/, m
|
||||
assert_match /@admin_role\.save/, m
|
||||
assert_match /@admin_role\.errors/, m
|
||||
end
|
||||
|
||||
assert_instance_method :update, content do |m|
|
||||
assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m
|
||||
assert_match /@admin_role\.update_attributes\(params\[:admin_role\]\)/, m
|
||||
assert_match /@admin_role\.errors/, m
|
||||
end
|
||||
|
||||
assert_instance_method :destroy, content do |m|
|
||||
assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m
|
||||
assert_match /@admin_role\.destroy/, m
|
||||
end
|
||||
end
|
||||
|
||||
assert_file "test/functional/admin/roles_controller_test.rb",
|
||||
/class Admin::RolesControllerTest < ActionController::TestCase/
|
||||
|
||||
# Views
|
||||
%w(
|
||||
index
|
||||
edit
|
||||
new
|
||||
show
|
||||
_form
|
||||
).each { |view| assert_file "app/views/admin/roles/#{view}.html.erb" }
|
||||
assert_no_file "app/views/layouts/admin/roles.html.erb"
|
||||
|
||||
# Helpers
|
||||
assert_file "app/helpers/admin/roles_helper.rb"
|
||||
assert_file "test/unit/helpers/admin/roles_helper_test.rb"
|
||||
|
||||
# Stylesheets
|
||||
assert_file "public/stylesheets/scaffold.css"
|
||||
end
|
||||
|
||||
def test_scaffold_with_namespace_on_revoke
|
||||
run_generator [ "admin/role", "name:string", "description:string" ]
|
||||
run_generator [ "admin/role" ], :behavior => :revoke
|
||||
|
||||
# Model
|
||||
assert_file "app/models/admin.rb" # ( should not be remove )
|
||||
assert_no_file "app/models/admin/role.rb"
|
||||
assert_no_file "test/unit/admin/role_test.rb"
|
||||
assert_no_file "test/fixtures/admin/roles.yml"
|
||||
assert_no_migration "db/migrate/create_admin_roles.rb"
|
||||
|
||||
# Route
|
||||
assert_file "config/routes.rb" do |route|
|
||||
assert_no_match /namespace :admin do resources :roles end$/, route
|
||||
end
|
||||
|
||||
# Controller
|
||||
assert_no_file "app/controllers/admin/roles_controller.rb"
|
||||
assert_no_file "test/functional/admin/roles_controller_test.rb"
|
||||
|
||||
# Views
|
||||
assert_no_file "app/views/admin/roles"
|
||||
assert_no_file "app/views/layouts/admin/roles.html.erb"
|
||||
|
||||
# Helpers
|
||||
assert_no_file "app/helpers/admin/roles_helper.rb"
|
||||
assert_no_file "test/unit/helpers/admin/roles_helper_test.rb"
|
||||
|
||||
# Stylesheets (should not be removed)
|
||||
assert_file "public/stylesheets/scaffold.css"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user