Permalink
Browse files

Rails will now generate Ruby 1.9 style hash when running app generato…

…r 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.
  • Loading branch information...
sikachu authored and dhh committed Apr 10, 2011
1 parent 63cd92f commit 74960c3976a69b3c7d9aa6bf28edb42ea82df0ce
View
@@ -1,6 +1,6 @@
*Rails 3.1.0 (unreleased)*
-* Changed scaffold generator to create Ruby 1.9 style hash when running on Ruby 1.9 [Prem Sichanugrist]
+* Changed scaffold and app generator to create Ruby 1.9 style hash when running on Ruby 1.9 [Prem Sichanugrist]
So instead of creating something like:
@@ -174,6 +174,15 @@ def empty_directory_with_gitkeep(destination, config = {})
create_file("#{destination}/.gitkeep") unless options[:skip_git]
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
@@ -1,6 +1,6 @@
# Be sure to restart your server when you modify this file.
-<%= app_const %>.config.session_store :cookie_store, :key => '_<%= app_name %>_session'
+<%= app_const %>.config.session_store :cookie_store, <%= key_value :key, "'_#{app_name}_session'" %>

This comment has been minimized.

Show comment Hide comment
@defeated

defeated May 22, 2011

This change to Hash key format (from :key => val, to key: val) breaks if you:

  1. generate rails new foo on a machine w/ 1.9.2
  2. then deploy to a machine running 1.8.7 (like REE)
/config/initializers/session_store.rb:3: syntax error, unexpected ':', expecting $end (SyntaxError)
@defeated

defeated May 22, 2011

This change to Hash key format (from :key => val, to key: val) breaks if you:

  1. generate rails new foo on a machine w/ 1.9.2
  2. then deploy to a machine running 1.8.7 (like REE)
/config/initializers/session_store.rb:3: syntax error, unexpected ':', expecting $end (SyntaxError)

This comment has been minimized.

Show comment Hide comment
@sikachu

sikachu May 22, 2011

Member

Yep, and I think that's the expected result since 1.8.7 doesn't support newer style hash.

What you could do is passing --old-style-hash into the command, so it would use the hash rocket style hash.

I'd recommend you to use the same ruby version on both production and development environment though, as it would be easier to debug in case something gone wrong. :)

@sikachu

sikachu May 22, 2011

Member

Yep, and I think that's the expected result since 1.8.7 doesn't support newer style hash.

What you could do is passing --old-style-hash into the command, so it would use the hash rocket style hash.

I'd recommend you to use the same ruby version on both production and development environment though, as it would be easier to debug in case something gone wrong. :)

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
@@ -3,5 +3,5 @@
#
# Examples:
#
-# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
-# Mayor.create(:name => 'Daley', :city => cities.first)
+# cities = City.create([{ <%= key_value :name, "'Chicago'" %> }, { <%= key_value :name, "'Copenhagen'" %> }])
+# Mayor.create(<%= key_value :name, "'Daley'" %>, <%= key_value :city, "cities.first" %>)
@@ -216,6 +216,16 @@ def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
end
end
+ def test_new_hash_style
+ run_generator [destination_root]
+ assert_file "config/initializers/session_store.rb" do |file|
+ if RUBY_VERSION < "1.9"
+ assert_match /config.session_store :cookie_store, :key => '_.+_session'/, file
+ else
+ assert_match /config.session_store :cookie_store, key: '_.+_session'/, file
+ end
+ end
+ end
protected
def action(*args, &block)

0 comments on commit 74960c3

Please sign in to comment.