From 11b9c27d846de1063900726e82e1204cf04354e3 Mon Sep 17 00:00:00 2001 From: Francesc Esplugas Date: Wed, 14 Apr 2010 13:31:17 +0200 Subject: [PATCH] Fixed scaffolding for namespaced models. --- .../erb/scaffold/templates/_form.html.erb | 20 ++++---- .../erb/scaffold/templates/edit.html.erb | 4 +- .../erb/scaffold/templates/index.html.erb | 12 ++-- .../generators/erb/scaffold/templates/new.html.erb | 2 +- .../erb/scaffold/templates/show.html.erb | 6 +- .../scaffold_controller/templates/controller.rb | 52 ++++++++++---------- railties/lib/rails/generators/resource_helpers.rb | 2 +- 7 files changed, 49 insertions(+), 49 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 9b83207..cb93b4b 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb @@ -1,14 +1,14 @@ -<%%= form_for(@<%= singular_name %>) do |f| %> - <%% if @<%= singular_name %>.errors.any? %> -
-

<%%= pluralize(@<%= singular_name %>.errors.count, "error") %> prohibited this <%= singular_name %> from being saved:

+<%%= form_for(@<%= table_name.singularize %>) do |f| %> + <%% if @<%= table_name.singularize %>.errors.any? %> +
+

<%%= pluralize(@<%= table_name.singularize %>.errors.count, "error") %> prohibited this <%= singular_name %> from being saved:

-
    - <%% @<%= singular_name %>.errors.full_messages.each do |msg| %> -
  • <%%= msg %>
  • - <%% end %> -
-
+ +
<%% end %> <% for attribute in attributes -%> 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 5bc507f..a45dd24 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb @@ -2,5 +2,5 @@ <%%= render 'form' %> -<%%= link_to 'Show', @<%= singular_name %> %> | -<%%= link_to 'Back', <%= plural_name %>_path %> +<%%= link_to 'Show', @<%= table_name.singularize %> %> | +<%%= link_to 'Back', <%= table_name %>_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 d30d306..0392e02 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb @@ -10,18 +10,18 @@ -<%% @<%= plural_name %>.each do |<%= singular_name %>| %> +<%% @<%= table_name %>.each do |<%= table_name.singularize %>| %> <% for attribute in attributes -%> - <%%= <%= singular_name %>.<%= attribute.name %> %> + <%%= <%= table_name.singularize %>.<%= attribute.name %> %> <% end -%> - <%%= link_to 'Show', <%= singular_name %> %> - <%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %> - <%%= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %> + <%%= link_to 'Show', <%= table_name.singularize %> %> + <%%= link_to 'Edit', edit_<%= table_name.singularize %>_path(<%= table_name.singularize %>) %> + <%%= link_to 'Destroy', <%= table_name.singularize %>, :confirm => 'Are you sure?', :method => :delete %> <%% end %>
-<%%= link_to 'New <%= human_name %>', new_<%= singular_name %>_path %> +<%%= link_to 'New <%= human_name %>', new_<%= table_name.singularize %>_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 9a1c489..73b3b15 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb @@ -2,4 +2,4 @@ <%%= render 'form' %> -<%%= link_to 'Back', <%= plural_name %>_path %> +<%%= link_to 'Back', <%= table_name %>_path %> 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 4dd2e6b..ebae9f0 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 -%>

<%= attribute.human_name %>: - <%%= @<%= singular_name %>.<%= attribute.name %> %> + <%%= @<%= table_name.singularize %>.<%= attribute.name %> %>

<% end -%> -<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> | -<%%= link_to 'Back', <%= plural_name %>_path %> +<%%= link_to 'Edit', edit_<%= table_name.singularize %>_path(@<%= table_name.singularize %>) %> | +<%%= link_to 'Back', <%= table_name %>_path %> 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 bbdce66..afc31fc 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -1,7 +1,7 @@ class <%= controller_class_name %>Controller < ApplicationController <% unless options[:singleton] -%> - # GET /<%= table_name %> - # GET /<%= table_name %>.xml + # GET /<%= class_name.underscore.pluralize %> + # GET /<%= class_name.underscore.pluralize %>.xml def index @<%= table_name %> = <%= orm_class.all(class_name) %> @@ -12,42 +12,42 @@ class <%= controller_class_name %>Controller < ApplicationController end <% end -%> - # GET /<%= table_name %>/1 - # GET /<%= table_name %>/1.xml + # GET /<%= class_name.underscore.pluralize %>/1 + # GET /<%= class_name.underscore.pluralize %>/1.xml def show - @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> + @<%= table_name.singularize %> = <%= 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 => @<%= table_name.singularize %> } end end - # GET /<%= table_name %>/new - # GET /<%= table_name %>/new.xml + # GET /<%= class_name.underscore.pluralize %>/new + # GET /<%= class_name.underscore.pluralize %>/new.xml def new - @<%= file_name %> = <%= orm_class.build(class_name) %> + @<%= table_name.singularize %> = <%= orm_class.build(class_name) %> respond_to do |format| format.html # new.html.erb - format.xml { render :xml => @<%= file_name %> } + format.xml { render :xml => @<%= table_name.singularize %> } end end - # GET /<%= table_name %>/1/edit + # GET /<%= class_name.underscore.pluralize %>/1/edit def edit - @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> + @<%= table_name.singularize %> = <%= orm_class.find(class_name, "params[:id]") %> end - # POST /<%= table_name %> - # POST /<%= table_name %>.xml + # POST /<%= class_name.underscore.pluralize %> + # POST /<%= class_name.underscore.pluralize %>.xml def create - @<%= file_name %> = <%= orm_class.build(class_name, "params[:#{file_name}]") %> + @<%= table_name.singularize %> = <%= orm_class.build(class_name, "params[:#{table_name.singularize}]") %> 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(@<%= table_name.singularize %>, :notice => '<%= human_name %> was successfully created.') } + format.xml { render :xml => @<%= table_name.singularize %>, :status => :created, :location => @<%= table_name.singularize %> } 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 /<%= class_name.underscore.pluralize %>/1 + # PUT /<%= class_name.underscore.pluralize %>/1.xml def update - @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> + @<%= table_name.singularize %> = <%= 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[:#{table_name.singularize}]") %> + format.html { redirect_to(@<%= table_name.singularize %>, :notice => '<%= human_name %> was successfully updated.') } format.xml { head :ok } else format.html { render :action => "edit" } @@ -71,11 +71,11 @@ class <%= controller_class_name %>Controller < ApplicationController end end - # DELETE /<%= table_name %>/1 - # DELETE /<%= table_name %>/1.xml + # DELETE /<%= class_name.underscore.pluralize %>/1 + # DELETE /<%= class_name.underscore.pluralize %>/1.xml def destroy - @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> - @<%= orm_instance.destroy %> + @<%= table_name.singularize %> = <%= orm_class.find(class_name, "params[:id]") %> + @<%= orm_instance(table_name.singularize).destroy %> respond_to do |format| format.html { redirect_to(<%= table_name %>_url) } diff --git a/railties/lib/rails/generators/resource_helpers.rb b/railties/lib/rails/generators/resource_helpers.rb index 3a98a8f..1e6dbd1 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=table_name.singularize) @orm_instance ||= @orm_class.new(name) end end -- 1.7.0.5