diff options
author | Prem Sichanugrist <s@sikachu.com> | 2011-04-10 12:27:26 +0800 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2011-04-10 16:47:50 +0800 |
commit | 63cd92f9f346acefca1ad014873c971837843cdb (patch) | |
tree | 23be83a25e191359502ae7b95e83deee97ba93fe /railties | |
parent | c0efc4009adb870ee23e93d46ac374f772d8cb4f (diff) | |
download | rails-63cd92f9f346acefca1ad014873c971837843cdb.tar.gz rails-63cd92f9f346acefca1ad014873c971837843cdb.tar.bz2 rails-63cd92f9f346acefca1ad014873c971837843cdb.zip |
Rails will now generate Ruby 1.9 style hash when running scaffold_controller generator on Ruby 1.9.x
The new hash syntax of Ruby 1.9 looks more superior, so we decide to switch to it in the places that appropriate.
This patch has been requested by DHH.
Diffstat (limited to 'railties')
6 files changed, 50 insertions, 19 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 7f1be02cd2..b5050440bb 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,15 @@ *Rails 3.1.0 (unreleased)* +* Changed scaffold generator to create Ruby 1.9 style hash when running on Ruby 1.9 [Prem Sichanugrist] + + So instead of creating something like: + + redirect_to users_path, :notice => "User has been created" + + it will now be like this: + + redirect_to users_path, notice: "User has been created" + * Changed scaffold_controller generator to create format block for JSON instead of XML [Prem Sichanugrist] * Add using Turn with natural language test case names for test_help.rb when running with minitest (Ruby 1.9.2+) [DHH] 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 4c46db4d67..a7393cfe18 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb @@ -17,7 +17,7 @@ <% end -%> <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> + <td><%%= link_to 'Destroy', <%= singular_table_name %>, <%= key_value :confirm, "'Are you sure?'" %>, <%= key_value :method, ":delete" %> %></td> </tr> <%% end %> </table> diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 2af7f85463..3d19b372ff 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -181,6 +181,16 @@ module Rails class_collisions "#{options[:prefix]}#{name}#{options[:suffix]}" end end + + # Returns Ruby 1.9 style key-value pair if current code is running on + # Ruby 1.9.x. Returns the old-style (with hash rocket) otherwise. + def key_value(key, value) + if RUBY_VERSION < '1.9' + ":#{key} => #{value}" + else + "#{key}: #{value}" + end + end end end end 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 5fb8895654..a8a1c07d1f 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -7,7 +7,7 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| format.html # index.html.erb - format.json { render :json => @<%= plural_table_name %> } + format.json { render <%= key_value :json, "@#{plural_table_name}" %> } end end @@ -18,7 +18,7 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| format.html # show.html.erb - format.json { render :json => @<%= singular_table_name %> } + format.json { render <%= key_value :json, "@#{singular_table_name}" %> } end end @@ -29,7 +29,7 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| format.html # new.html.erb - format.json { render :json => @<%= singular_table_name %> } + format.json { render <%= key_value :json, "@#{singular_table_name}" %> } end end @@ -45,11 +45,11 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| if @<%= orm_instance.save %> - format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully created.') } - format.json { render :json => @<%= singular_table_name %>, :status => :created, :location => @<%= singular_table_name %> } + format.html { redirect_to @<%= singular_table_name %>, <%= key_value :notice, "'#{human_name} was successfully created.'" %> } + format.json { render <%= key_value :json, "@#{singular_table_name}" %>, <%= key_value :status, ':created' %>, <%= key_value :location, "@#{singular_table_name}" %> } else - format.html { render :action => "new" } - format.json { render :json => @<%= orm_instance.errors %>, :status => :unprocessable_entity } + format.html { render <%= key_value :action, '"new"' %> } + format.json { render <%= key_value :json, "@#{orm_instance.errors}" %>, <%= key_value :status, ':unprocessable_entity' %> } end end end @@ -61,11 +61,11 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %> - format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully updated.') } - format.json { render :json => {}, :status => :ok } + format.html { redirect_to @<%= singular_table_name %>, <%= key_value :notice, "'#{human_name} was successfully updated.'" %> } + format.json { render <%= key_value :json, '{}' %>, <%= key_value :status, ':ok' %> } else - format.html { render :action => "edit" } - format.json { render :json => @<%= orm_instance.errors %>, :status => :unprocessable_entity } + format.html { render <%= key_value :action, '"edit"' %> } + format.json { render <%= key_value :json, "@#{orm_instance.errors}" %>, <%= key_value :status, ':unprocessable_entity' %> } end end end @@ -77,8 +77,8 @@ class <%= controller_class_name %>Controller < ApplicationController @<%= orm_instance.destroy %> respond_to do |format| - format.html { redirect_to(<%= index_helper %>_url) } - format.json { render :json => {}, :status => :ok } + format.html { redirect_to <%= index_helper %>_url } + format.json { render <%= key_value :json, '{}' %>, <%= key_value :status, ':ok' %> } 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 964d59d84c..01fe6dda7a 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 @@ -19,30 +19,30 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do - post :create, :<%= singular_table_name %> => @<%= singular_table_name %>.attributes + post :create, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %> end assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end test "should show <%= singular_table_name %>" do - get :show, :id => @<%= singular_table_name %>.to_param + get :show, <%= key_value :id, "@#{singular_table_name}.to_param" %> assert_response :success end test "should get edit" do - get :edit, :id => @<%= singular_table_name %>.to_param + get :edit, <%= key_value :id, "@#{singular_table_name}.to_param" %> assert_response :success end test "should update <%= singular_table_name %>" do - put :update, :id => @<%= singular_table_name %>.to_param, :<%= singular_table_name %> => @<%= singular_table_name %>.attributes + put :update, <%= key_value :id, "@#{singular_table_name}.to_param" %>, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %> assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end test "should destroy <%= singular_table_name %>" do assert_difference('<%= class_name %>.count', -1) do - delete :destroy, :id => @<%= singular_table_name %>.to_param + delete :destroy, <%= key_value :id, "@#{singular_table_name}.to_param" %> end assert_redirected_to <%= index_helper %>_path diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index d55ed22975..ff82cdb744 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -122,4 +122,15 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase ensure Unknown::Generators.send :remove_const, :ActiveModel end + + def test_new_hash_style + run_generator + assert_file "app/controllers/users_controller.rb" do |content| + if RUBY_VERSION < "1.9" + assert_match /\{ render :action => "new" \}/, content + else + assert_match /\{ render action: "new" \}/, content + end + end + end end |