From dc87d5bb3173c34cab12aac1eae5b8f1954051d7 Mon Sep 17 00:00:00 2001 From: javier Date: Sun, 7 Feb 2010 22:17:29 +0100 Subject: [PATCH] scaffold generator uses responder and i18n --- .../erb/scaffold/templates/edit.html.erb | 6 +- .../erb/scaffold/templates/index.html.erb | 10 ++-- .../generators/erb/scaffold/templates/new.html.erb | 4 +- .../erb/scaffold/templates/show.html.erb | 4 +- .../scaffold_controller/templates/controller.rb | 48 +++++--------------- railties/lib/locale/en.yml | 15 ++++++ railties/lib/locale/es.yml | 15 ++++++ railties/lib/rails/all.rb | 2 + .../scaffold_controller_generator_test.rb | 4 +- .../test/generators/scaffold_generator_test.rb | 4 +- 10 files changed, 60 insertions(+), 52 deletions(-) create mode 100644 railties/lib/locale/en.yml create mode 100644 railties/lib/locale/es.yml diff --git a/railties/lib/generators/erb/scaffold/templates/edit.html.erb b/railties/lib/generators/erb/scaffold/templates/edit.html.erb index 5bc507f..a12edcc 100644 --- a/railties/lib/generators/erb/scaffold/templates/edit.html.erb +++ b/railties/lib/generators/erb/scaffold/templates/edit.html.erb @@ -1,6 +1,6 @@ -

Editing <%= singular_name %>

+

<%%=t(:title_for_edit, :scope => [:railties, :scaffold], :model=>'<%=singular_name%>') %>

<%%= render 'form' %> -<%%= link_to 'Show', @<%= singular_name %> %> | -<%%= link_to 'Back', <%= plural_name %>_path %> +<%%= link_to t(:link_to_show, :scope => [:railties, :scaffold]), @<%= singular_name %> %> | +<%%= link_to t(:link_to_back, :scope => [:railties, :scaffold]), <%= plural_name %>_path %> diff --git a/railties/lib/generators/erb/scaffold/templates/index.html.erb b/railties/lib/generators/erb/scaffold/templates/index.html.erb index b5c7fd1..7f92e9b 100644 --- a/railties/lib/generators/erb/scaffold/templates/index.html.erb +++ b/railties/lib/generators/erb/scaffold/templates/index.html.erb @@ -1,4 +1,4 @@ -

Listing <%= plural_name %>

+

<%%=t(:title_for_index, :scope => [:railties, :scaffold], :plural_name=>'<%=plural_name%>') %>

@@ -15,13 +15,13 @@ <% for attribute in attributes -%> <% end -%> - - - + + + <%% end %>
<%%= <%= singular_name %>.<%= attribute.name %> %><%%= 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 t(:link_to_show, :scope => [:railties, :scaffold]), <%= singular_name %> %><%%= link_to t(:link_to_edit, :scope => [:railties, :scaffold]), edit_<%= singular_name %>_path(<%= singular_name %>) %><%%= link_to t(:link_to_destroy, :scope => [:railties, :scaffold]), <%= singular_name %>, :confirm => t(:confirm_destroy, :scope => [:railties, :scaffold]), :method => :delete %>

-<%%= link_to 'New <%= singular_name %>', new_<%= singular_name %>_path %> +<%%= link_to t(:link_to_new, :scope => [:railties, :scaffold], :model=>'<%=singular_name%>'), new_<%= singular_name %>_path %> diff --git a/railties/lib/generators/erb/scaffold/templates/new.html.erb b/railties/lib/generators/erb/scaffold/templates/new.html.erb index 9a1c489..5af2ac9 100644 --- a/railties/lib/generators/erb/scaffold/templates/new.html.erb +++ b/railties/lib/generators/erb/scaffold/templates/new.html.erb @@ -1,5 +1,5 @@ -

New <%= singular_name %>

+

<%%=t(:title_for_new, :scope => [:railties, :scaffold], :model=>'<%=singular_name%>') %>

<%%= render 'form' %> -<%%= link_to 'Back', <%= plural_name %>_path %> +<%%= link_to t(:link_to_back, :scope => [:railties, :scaffold]), <%= plural_name %>_path %> diff --git a/railties/lib/generators/erb/scaffold/templates/show.html.erb b/railties/lib/generators/erb/scaffold/templates/show.html.erb index 24f13fc..c098aa6 100644 --- a/railties/lib/generators/erb/scaffold/templates/show.html.erb +++ b/railties/lib/generators/erb/scaffold/templates/show.html.erb @@ -6,5 +6,5 @@ <% end -%> -<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> | -<%%= link_to 'Back', <%= plural_name %>_path %> +<%%= link_to t(:link_to_edit, :scope => [:railties, :scaffold]), edit_<%= singular_name %>_path(@<%= singular_name %>) %> | +<%%= link_to t(:link_to_back, :scope => [:railties, :scaffold]), <%= plural_name %>_path %> diff --git a/railties/lib/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/generators/rails/scaffold_controller/templates/controller.rb index 874e96a..f9f0cd2 100644 --- a/railties/lib/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/generators/rails/scaffold_controller/templates/controller.rb @@ -1,14 +1,13 @@ class <%= controller_class_name %>Controller < ApplicationController + respond_to :html, :xml + <% unless options[:singleton] -%> # GET /<%= table_name %> # GET /<%= table_name %>.xml def index @<%= table_name %> = <%= orm_class.all(class_name) %> - respond_to do |format| - format.html # index.html.erb - format.xml { render :xml => @<%= table_name %> } - end + respond_with @<%= table_name %> end <% end -%> @@ -17,10 +16,7 @@ class <%= controller_class_name %>Controller < ApplicationController def show @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> - respond_to do |format| - format.html # show.html.erb - format.xml { render :xml => @<%= file_name %> } - end + respond_with @<%= file_name %> end # GET /<%= table_name %>/new @@ -28,10 +24,7 @@ class <%= controller_class_name %>Controller < ApplicationController def new @<%= file_name %> = <%= orm_class.build(class_name) %> - respond_to do |format| - format.html # new.html.erb - format.xml { render :xml => @<%= file_name %> } - end + respond_with @<%= file_name %> end # GET /<%= table_name %>/1/edit @@ -43,32 +36,18 @@ class <%= controller_class_name %>Controller < ApplicationController # POST /<%= table_name %>.xml def create @<%= file_name %> = <%= orm_class.build(class_name, "params[:#{file_name}]") %> - - respond_to do |format| - if @<%= orm_instance.save %> - format.html { redirect_to(@<%= file_name %>, :notice => '<%= class_name %> was successfully created.') } - format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> } - else - format.html { render :action => "new" } - format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity } - end - end + + flash[:notice] = I18n.t(:flash_created_ok, :scope => [:railties, :scaffold], :model=><%=class_name%>) if @<%= orm_instance.save %> + respond_with @<%= file_name %> end # PUT /<%= table_name %>/1 # PUT /<%= table_name %>/1.xml def update @<%= file_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 => '<%= class_name %> was successfully updated.') } - format.xml { head :ok } - else - format.html { render :action => "edit" } - format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity } - end - end + + flash[:notice] = I18n.t(:flash_updated_ok, :scope => [:railties, :scaffold], :model=><%=class_name%>) if @<%= orm_instance.update_attributes("params[:#{file_name}]") %> + respond_with @<%= file_name %> end # DELETE /<%= table_name %>/1 @@ -77,9 +56,6 @@ class <%= controller_class_name %>Controller < ApplicationController @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> @<%= orm_instance.destroy %> - respond_to do |format| - format.html { redirect_to(<%= table_name %>_url) } - format.xml { head :ok } - end + respond_with @<%= file_name %> end end diff --git a/railties/lib/locale/en.yml b/railties/lib/locale/en.yml new file mode 100644 index 0000000..2402ce9 --- /dev/null +++ b/railties/lib/locale/en.yml @@ -0,0 +1,15 @@ +en: + railties: + scaffold: + confirm_destroy: 'Are you sure?' + flash_created_ok: '{{model}} was successfully created.' + flash_updated_ok: '{{model}} was successfully updated.' + link_to_back: Back + link_to_destroy: Destroy + link_to_edit: Edit + link_to_new: New {{model}} + link_to_show: Show + title_for_edit: Editing {{model}} + title_for_index: Listing {{plural_name}} + title_for_new: New {{model}} + \ No newline at end of file diff --git a/railties/lib/locale/es.yml b/railties/lib/locale/es.yml new file mode 100644 index 0000000..31c0d90 --- /dev/null +++ b/railties/lib/locale/es.yml @@ -0,0 +1,15 @@ +es: + railties: + scaffold: + confirm_destroy: '¿Está seguro?' + flash_created_ok: '{{model}} se ha creado correctamente.' + flash_updated_ok: '{{model}} se ha actualizado correctamente.' + link_to_back: Volver + link_to_destroy: Borrar + link_to_edit: Editar + link_to_new: Nuevo {{model}} + link_to_show: Ver + title_for_edit: Editando {{model}} + title_for_index: Listado de {{plural_name}} + title_for_new: Nuevo {{model}} + \ No newline at end of file diff --git a/railties/lib/rails/all.rb b/railties/lib/rails/all.rb index 82775b7..e8e247c 100644 --- a/railties/lib/rails/all.rb +++ b/railties/lib/rails/all.rb @@ -12,3 +12,5 @@ require "rails" rescue LoadError end end + +I18n.load_path << Dir[File.join(File.dirname(__FILE__),'..', 'locale','*.{rb,yml}')] \ No newline at end of file diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index f971598..c7f014a 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -35,13 +35,13 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_instance_method :create, content do |m| assert_match /@user = User\.new\(params\[:user\]\)/, m assert_match /@user\.save/, m - assert_match /@user\.errors/, m + assert_match /flash\[:notice\]/, m end assert_instance_method :update, content do |m| assert_match /@user = User\.find\(params\[:id\]\)/, m assert_match /@user\.update_attributes\(params\[:user\]\)/, m - assert_match /@user\.errors/, m + assert_match /flash\[:notice\]/, m end assert_instance_method :destroy, content do |m| diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index a7e9c8a..83b580c 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -52,13 +52,13 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_instance_method :create, content do |m| assert_match /@product_line = ProductLine\.new\(params\[:product_line\]\)/, m assert_match /@product_line\.save/, m - assert_match /@product_line\.errors/, m + assert_match /flash\[:notice\]/, m end assert_instance_method :update, content do |m| assert_match /@product_line = ProductLine\.find\(params\[:id\]\)/, m assert_match /@product_line\.update_attributes\(params\[:product_line\]\)/, m - assert_match /@product_line\.errors/, m + assert_match /flash\[:notice\]/, m end assert_instance_method :destroy, content do |m| -- 1.6.3.3