diff options
author | Mohammed Siddick.E <siddick@gmail.com> | 2010-06-23 12:33:34 +0530 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-06-23 09:30:55 +0200 |
commit | 7008911222826eef07a338bf4cab27b83fe90ce1 (patch) | |
tree | 8e903ca66456b3e6f142256f02cc1b38b2bf284c /railties | |
parent | d132dd33520ba61f7bfa9ba6fdd1b7b2bebd27f3 (diff) | |
download | rails-7008911222826eef07a338bf4cab27b83fe90ce1.tar.gz rails-7008911222826eef07a338bf4cab27b83fe90ce1.tar.bz2 rails-7008911222826eef07a338bf4cab27b83fe90ce1.zip |
Patch for Namespace problem in Scaffold. [#4763 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'railties')
13 files changed, 185 insertions, 60 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 9b83207b3f..d12b2ff0e5 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb @@ -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> 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 415f820206..e58b9fbd08 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb @@ -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 %> 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 d30d306d42..4c46db4d67 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,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 %> 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 ddabc9d349..02ae4d015e 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb @@ -1,4 +1,4 @@ -<h1>New <%= singular_name %></h1> +<h1>New <%= singular_table_name %></h1> <%%= render 'form' %> 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 31b8253b35..c0e5ccff1e 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb @@ -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 %> diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 72ec2856d0..44f831e6f3 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -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. diff --git a/railties/lib/rails/generators/rails/model/USAGE b/railties/lib/rails/generators/rails/model/USAGE index db98a2dd1b..67f76aad01 100644 --- a/railties/lib/rails/generators/rails/model/USAGE +++ b/railties/lib/rails/generators/rails/model/USAGE @@ -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 diff --git a/railties/lib/rails/generators/rails/resource/resource_generator.rb b/railties/lib/rails/generators/rails/resource/resource_generator.rb index 8a46708009..ee302b8aad 100644 --- a/railties/lib/rails/generators/rails/resource/resource_generator.rb +++ b/railties/lib/rails/generators/rails/resource/resource_generator.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 diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb index b5f19b6d15..84cf58d7c4 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -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| diff --git a/railties/lib/rails/generators/resource_helpers.rb b/railties/lib/rails/generators/resource_helpers.rb index 3a98a8f9c1..829f4b200a 100644 --- a/railties/lib/rails/generators/resource_helpers.rb +++ b/railties/lib/rails/generators/resource_helpers.rb @@ -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 diff --git a/railties/lib/rails/generators/test_unit/model/model_generator.rb b/railties/lib/rails/generators/test_unit/model/model_generator.rb index 609b815683..c1dd535dd3 100644 --- a/railties/lib/rails/generators/test_unit/model/model_generator.rb +++ b/railties/lib/rails/generators/test_unit/model/model_generator.rb @@ -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 diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb index d5d3d6d5cd..957ebaa522 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb @@ -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 diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index e8e622fe5c..ea469cb3c8 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -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 |