diff options
-rw-r--r-- | activerecord/test/cases/column_test.rb | 36 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/app/app_generator.rb | 5 | ||||
-rw-r--r-- | railties/test/generators/app_generator_test.rb | 36 |
3 files changed, 77 insertions, 0 deletions
diff --git a/activerecord/test/cases/column_test.rb b/activerecord/test/cases/column_test.rb index 2a6d8cc2ab..39489c3718 100644 --- a/activerecord/test/cases/column_test.rb +++ b/activerecord/test/cases/column_test.rb @@ -35,6 +35,13 @@ module ActiveRecord assert_equal false, column.type_cast('SOMETHING RANDOM') end + def test_type_cast_string + column = Column.new("field", nil, "varchar") + assert_equal "1", column.type_cast(true) + assert_equal "0", column.type_cast(false) + assert_equal "123", column.type_cast(123) + end + def test_type_cast_integer column = Column.new("field", nil, "integer") assert_equal 1, column.type_cast(1) @@ -72,6 +79,25 @@ module ActiveRecord assert_nil column.type_cast(1.0/0.0) end + def test_type_cast_float + column = Column.new("field", nil, "float") + assert_equal 1.0, column.type_cast("1") + end + + def test_type_cast_decimal + column = Column.new("field", nil, "decimal") + assert_equal BigDecimal.new("0"), column.type_cast(BigDecimal.new("0")) + assert_equal BigDecimal.new("123"), column.type_cast(123.0) + assert_equal BigDecimal.new("1"), column.type_cast(:"1") + end + + def test_type_cast_binary + column = Column.new("field", nil, "binary") + assert_equal nil, column.type_cast(nil) + assert_equal "1", column.type_cast("1") + assert_equal 1, column.type_cast(1) + end + def test_type_cast_time column = Column.new("field", nil, "time") assert_equal nil, column.type_cast(nil) @@ -118,6 +144,16 @@ module ActiveRecord end end end + + if current_adapter?(:SQLite3Adapter) + def test_binary_encoding + column = SQLite3Column.new("field", nil, "binary") + utf8_string = "a string".encode(Encoding::UTF_8) + type_cast = column.type_cast(utf8_string) + + assert_equal Encoding::ASCII_8BIT, type_cast.encoding + end + end end end end diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 1f86a30006..188e62b6c8 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -194,9 +194,14 @@ module Rails build(:bin) end + def create_config_files + build(:config) + end + def update_config_files build(:config_when_updating) end + remove_task :update_config_files def create_boot_file template "config/boot.rb" diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 4beb362ef9..1cbbf62459 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -138,6 +138,42 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "myapp/config/initializers/session_store.rb", /_myapp_session/ end + def test_new_application_use_json_serialzier + run_generator + + assert_file("config/initializers/cookies_serializer.rb", /Rails\.application\.config\.action_dispatch\.cookies_serializer = :json/) + end + + def test_rails_update_keep_the_cookie_serializer_if_it_is_already_configured + app_root = File.join(destination_root, 'myapp') + run_generator [app_root] + + Rails.application.config.root = app_root + Rails.application.class.stubs(:name).returns("Myapp") + Rails.application.stubs(:is_a?).returns(Rails::Application) + + generator = Rails::Generators::AppGenerator.new ["rails"], { with_dispatchers: true }, destination_root: app_root, shell: @shell + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + assert_file("#{app_root}/config/initializers/cookies_serializer.rb", /Rails\.application\.config\.action_dispatch\.cookies_serializer = :json/) + end + + def test_rails_update_set_the_cookie_serializer_to_marchal_if_it_is_not_already_configured + app_root = File.join(destination_root, 'myapp') + run_generator [app_root] + + FileUtils.rm("#{app_root}/config/initializers/cookies_serializer.rb") + + Rails.application.config.root = app_root + Rails.application.class.stubs(:name).returns("Myapp") + Rails.application.stubs(:is_a?).returns(Rails::Application) + + generator = Rails::Generators::AppGenerator.new ["rails"], { with_dispatchers: true }, destination_root: app_root, shell: @shell + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + assert_file("#{app_root}/config/initializers/cookies_serializer.rb", /Rails\.application\.config\.action_dispatch\.cookies_serializer = :marshal/) + end + def test_application_names_are_not_singularized run_generator [File.join(destination_root, "hats")] assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/ |