aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPrem Sichanugrist <s@sikachu.com>2011-04-10 12:38:59 +0800
committerDavid Heinemeier Hansson <david@loudthinking.com>2011-04-10 16:47:52 +0800
commit22a3416298523d5e8ecb432a263e4bee9441e6c5 (patch)
treeecbe853fa81be36f3452b5b4cb5221086c0fd23b
parent74960c3976a69b3c7d9aa6bf28edb42ea82df0ce (diff)
downloadrails-22a3416298523d5e8ecb432a263e4bee9441e6c5.tar.gz
rails-22a3416298523d5e8ecb432a263e4bee9441e6c5.tar.bz2
rails-22a3416298523d5e8ecb432a263e4bee9441e6c5.zip
Add --old-style-hash option to force creating old style hash on Ruby 1.9
That means if you don't like the new syntax, you can pass --old-style-hash to force Rails to generate code with hash rockets.
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/rails/generators/app_base.rb5
-rw-r--r--railties/lib/rails/generators/named_base.rb5
-rw-r--r--railties/test/generators/app_generator_test.rb8
-rw-r--r--railties/test/generators/scaffold_controller_generator_test.rb7
5 files changed, 25 insertions, 2 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index e81d20c3ac..90faad6131 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -10,6 +10,8 @@
redirect_to users_path, notice: "User has been created"
+ You can also passing `--old-style-hash` to make Rails generate old style hash even you're on Ruby 1.9
+
* 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/app_base.rb b/railties/lib/rails/generators/app_base.rb
index 47dc6ce103..93db5106ce 100644
--- a/railties/lib/rails/generators/app_base.rb
+++ b/railties/lib/rails/generators/app_base.rb
@@ -53,6 +53,9 @@ module Rails
class_option :help, :type => :boolean, :aliases => "-h", :group => :rails,
:desc => "Show this help message and quit"
+
+ class_option :old_style_hash, :type => :boolean, :default => false,
+ :desc => "Force using old style hash (:foo => 'bar') on Ruby >= 1.9"
end
def initialize(*args)
@@ -177,7 +180,7 @@ module Rails
# 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'
+ if options[:old_style_hash] || RUBY_VERSION < '1.9'
":#{key} => #{value}"
else
"#{key}: #{value}"
diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb
index 3d19b372ff..36bc9e055c 100644
--- a/railties/lib/rails/generators/named_base.rb
+++ b/railties/lib/rails/generators/named_base.rb
@@ -8,6 +8,9 @@ module Rails
class_option :skip_namespace, :type => :boolean, :default => false,
:desc => "Skip namespace (affects only isolated applications)"
+ class_option :old_style_hash, :type => :boolean, :default => false,
+ :desc => "Force using old style hash (:foo => 'bar') on Ruby >= 1.9"
+
def initialize(args, *options) #:nodoc:
# Unfreeze name in case it's given as a frozen string
args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
@@ -185,7 +188,7 @@ module Rails
# 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'
+ if options[:old_style_hash] || RUBY_VERSION < '1.9'
":#{key} => #{value}"
else
"#{key}: #{value}"
diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb
index e440885adf..43f2fbd71c 100644
--- a/railties/test/generators/app_generator_test.rb
+++ b/railties/test/generators/app_generator_test.rb
@@ -226,6 +226,14 @@ class AppGeneratorTest < Rails::Generators::TestCase
end
end
end
+
+ def test_force_old_style_hash
+ run_generator [destination_root, "--old-style-hash"]
+ assert_file "config/initializers/session_store.rb" do |file|
+ assert_match /config.session_store :cookie_store, :key => '_.+_session'/, file
+ end
+ end
+
protected
def action(*args, &block)
diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb
index ff82cdb744..c7f45a807d 100644
--- a/railties/test/generators/scaffold_controller_generator_test.rb
+++ b/railties/test/generators/scaffold_controller_generator_test.rb
@@ -133,4 +133,11 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
end
end
end
+
+ def test_force_old_style_hash
+ run_generator ["User", "--old-style-hash"]
+ assert_file "app/controllers/users_controller.rb" do |content|
+ assert_match /\{ render :action => "new" \}/, content
+ end
+ end
end