From 9a3a4d6aefa7e2ca94340754eb5541bea1783de0 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Fri, 16 Apr 2010 22:16:21 +0200 Subject: Make i18n fallbacks configurable and fallback to the default locale by default in production [#4428 state:resolved] Allows to configure locale fallbacks through config.i18n.fallbacks. The default setting config.i18n.fallbacks = true in production.rb will make I18n.t lookup fallback to the I18n.default_locale if a translation could not be found for the current or given locale. config.fallbacks = true config.fallbacks.map = { :ca => :es } config.fallbacks.defaults = [:'es-ES', :es] config.fallbacks = [:'es-ES', :es] config.fallbacks = { :ca => :es } config.fallbacks = [:'es-ES', :es, { :ca => :es }] Signed-off-by: Pratik Naik --- .../templates/config/environments/production.rb.tt | 4 + railties/test/railties/i18n_railtie_test.rb | 86 ++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 railties/test/railties/i18n_railtie_test.rb (limited to 'railties') diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt index f902120453..b9fb13b640 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt @@ -39,4 +39,8 @@ # Enable threaded mode # config.threadsafe! + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation can not be found) + config.i18n.fallbacks = true end diff --git a/railties/test/railties/i18n_railtie_test.rb b/railties/test/railties/i18n_railtie_test.rb new file mode 100644 index 0000000000..51684aa838 --- /dev/null +++ b/railties/test/railties/i18n_railtie_test.rb @@ -0,0 +1,86 @@ +require "isolation/abstract_unit" + +module RailtiesTest + class I18nRailtieTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + FileUtils.rm_rf("#{app_path}/config/environments") + require "rails/all" + @old_path = I18n.load_path + end + + def teardown + I18n.load_path = @old_path || [] + I18n.backend = nil + end + + def load_app + require "#{app_path}/config/environment" + end + + def assert_fallbacks(fallbacks) + fallbacks.each do |locale, expected| + actual = I18n.fallbacks[locale] + assert_equal expected, actual, "expected fallbacks for #{locale.inspect} to be #{expected.inspect}, but were #{actual.inspect}" + end + end + + def assert_no_fallbacks + assert !I18n.backend.class.included_modules.include?(I18n::Backend::Fallbacks) + end + + test "config.i18n.load_path gets added to I18n.load_path" do + I18n.load_path = ['existing/path/to/locales'] + I18n::Railtie.config.i18n.load_path = ['new/path/to/locales'] + load_app + + assert I18n.load_path.include?('existing/path/to/locales') + assert I18n.load_path.include?('new/path/to/locales') + end + + test "not using config.i18n.fallbacks does not initialize I18n.fallbacks" do + I18n.backend = Class.new { include I18n::Backend::Base }.new # can't uninclude modules, so use a tmp backend class + load_app + assert_no_fallbacks + end + + test "config.i18n.fallbacks = true initializes I18n.fallbacks with default settings" do + I18n::Railtie.config.i18n.fallbacks = true + load_app + assert_fallbacks :de => [:de, :en] + end + + test "config.i18n.fallbacks.defaults = [:'en-US'] initializes fallbacks with en-US as a fallback default" do + I18n::Railtie.config.i18n.fallbacks.defaults = [:'en-US'] + load_app + assert_fallbacks :de => [:de, :'en-US', :en] + end + + test "config.i18n.fallbacks.map = { :ca => :'es-ES' } initializes fallbacks with a mapping ca => es-ES" do + I18n::Railtie.config.i18n.fallbacks.map = { :ca => :'es-ES' } + load_app + assert_fallbacks :ca => [:ca, :"es-ES", :es, :en] + end + + test "[shortcut] config.i18n.fallbacks = [:'en-US'] initializes fallbacks with en-US as a fallback default" do + I18n::Railtie.config.i18n.fallbacks = [:'en-US'] + load_app + assert_fallbacks :de => [:de, :'en-US', :en] + end + + test "[shortcut] config.i18n.fallbacks = [{ :ca => :'es-ES' }] initializes fallbacks with a mapping de-AT => de-DE" do + I18n::Railtie.config.i18n.fallbacks.map = { :ca => :'es-ES' } + load_app + assert_fallbacks :ca => [:ca, :"es-ES", :es, :en] + end + + test "[shortcut] config.i18n.fallbacks = [:'en-US', { :ca => :'es-ES' }] initializes fallbacks with the given arguments" do + I18n::Railtie.config.i18n.fallbacks = [:'en-US', { :ca => :'es-ES' }] + load_app + assert_fallbacks :ca => [:ca, :"es-ES", :es, :'en-US', :en] + end + end +end \ No newline at end of file -- cgit v1.2.3 From 64373937a393518a6e6a63255176ca297d3c009e Mon Sep 17 00:00:00 2001 From: Phil Smith Date: Thu, 15 Apr 2010 22:31:15 -0700 Subject: Make the migration generator handle pre-existing migrations with the same timestamp. In the event a migration already exists with that number, the new migration's timestamp will be incremented by 1. [#4412 state:resolved] Signed-off-by: Michael Koziarski --- railties/test/generators/migration_generator_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'railties') diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 762f84d579..6ea722e239 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -10,6 +10,19 @@ class MigrationGeneratorTest < Rails::Generators::TestCase assert_migration "db/migrate/#{migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration/ end + def test_migrations_generated_simultaneously + migrations = ["change_title_body_from_posts", "change_email_from_comments"] + + first_migration_number, second_migration_number = migrations.collect do |migration| + run_generator [migration] + file_name = migration_file_name "db/migrate/#{migration}.rb" + + File.basename(file_name).split('_').first + end + + assert_not_equal first_migration_number, second_migration_number + end + def test_migration_with_class_name migration = "ChangeTitleBodyFromPosts" run_generator [migration] -- cgit v1.2.3 From c52dbdc888e4bffab076a8443b7573ca2c9c1291 Mon Sep 17 00:00:00 2001 From: eparreno Date: Sat, 17 Apr 2010 23:45:08 +0200 Subject: fix testing guide: fonts and code format --- railties/guides/source/testing.textile | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'railties') diff --git a/railties/guides/source/testing.textile b/railties/guides/source/testing.textile index c4f7ff8e92..206ed6e75c 100644 --- a/railties/guides/source/testing.textile +++ b/railties/guides/source/testing.textile @@ -108,7 +108,7 @@ tag is considered Ruby code. When this fixture is loaded, the +size+ attribute o h5. Fixtures in Action -Rails by default automatically loads all fixtures from the 'test/fixtures' folder for your unit and functional test. Loading involves three steps: +Rails by default automatically loads all fixtures from the +test/fixtures+ folder for your unit and functional test. Loading involves three steps: * Remove any existing data from the table corresponding to the fixture * Load the fixture data into the table @@ -142,7 +142,7 @@ In Rails, unit tests are what you write to test your models. For this guide we will be using Rails _scaffolding_. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practices. I will be using examples from this generated code and would be supplementing it with additional examples where necessary. -NOTE: For more information on Rails _scaffolding_, refer to "Getting Started with Rails":getting_started.html +NOTE: For more information on Rails scaffolding, refer to "Getting Started with Rails":getting_started.html When you use +rails generate scaffold+, for a resource among other things it creates a test stub in the +test/unit+ folder: @@ -221,9 +221,9 @@ $ rake db:migrate $ rake db:test:load -Above +rake db:migrate+ runs any pending migrations on the _development_ environment and updates +db/schema.rb+. +rake db:test:load+ recreates the test database from the current db/schema.rb. On subsequent attempts it is a good to first run +db:test:prepare+ as it first checks for pending migrations and warns you appropriately. +Above +rake db:migrate+ runs any pending migrations on the _development_ environment and updates +db/schema.rb+. +rake db:test:load+ recreates the test database from the current +db/schema.rb+. On subsequent attempts it is a good to first run +db:test:prepare+ as it first checks for pending migrations and warns you appropriately. -NOTE: +db:test:prepare+ will fail with an error if db/schema.rb doesn't exists. +NOTE: +db:test:prepare+ will fail with an error if +db/schema.rb+ doesn't exists. h5. Rake Tasks for Preparing your Application for Testing @@ -256,7 +256,7 @@ This will run all the test methods from the test case. You can also run a particular test method from the test case by using the +-n+ switch with the +test method name+. -
+
 $ ruby unit/post_test.rb -n test_truth
 
 Loaded suite unit/post_test
@@ -265,7 +265,7 @@ Started
 Finished in 0.023513 seconds.
 
 1 tests, 1 assertions, 0 failures, 0 errors
-
+ The +.+ (dot) above indicates a passing test. When a test fails you see an +F+; when a test throws an error you see an +E+ in its place. The last line of the output is the summary. @@ -280,7 +280,7 @@ end Let us run this newly added test. -
+
 $ ruby unit/post_test.rb -n test_should_not_save_post_without_title
 Loaded suite -e
 Started
@@ -292,7 +292,7 @@ test_should_not_save_post_without_title(PostTest) [/test/unit/post_test.rb:6]:
  is not true.
 
 1 tests, 1 assertions, 1 failures, 0 errors
-
+ In the output, +F+ denotes a failure. You can see the corresponding trace shown under +1)+ along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable every assertion provides an optional message parameter, as shown here: @@ -305,12 +305,12 @@ end Running this test shows the friendlier assertion message: -
+
   1) Failure:
 test_should_not_save_post_without_title(PostTest) [/test/unit/post_test.rb:6]:
 Saved the post without a title.
  is not true.
-
+ Now to get this test to pass we can add a model level validation for the _title_ field. @@ -322,7 +322,7 @@ end Now the test should pass. Let us verify by running the test again: -
+
 $ ruby unit/post_test.rb -n test_should_not_save_post_without_title
 Loaded suite unit/post_test
 Started
@@ -330,7 +330,7 @@ Started
 Finished in 0.193608 seconds.
 
 1 tests, 1 assertions, 0 failures, 0 errors
-
+ Now if you noticed we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as _Test-Driven Development_ (TDD). @@ -348,7 +348,7 @@ end Now you can see even more output in the console from running the tests: -
+
 $ ruby unit/post_test.rb -n test_should_report_error
 Loaded suite -e
 Started
@@ -361,7 +361,7 @@ NameError: undefined local variable or method `some_undefined_variable' for #
+
 
 Notice the 'E' in the output. It denotes a test with error.
 
@@ -446,7 +446,7 @@ test "should get index" do
 end
 
 
-In the +test_should_get_index+ test, Rails simulates a request on the action called index, making sure the request was successful and also ensuring that it assigns a valid +posts+ instance variable.
+In the +test_should_get_index+ test, Rails simulates a request on the action called +index+, making sure the request was successful and also ensuring that it assigns a valid +posts+ instance variable.
 
 The +get+ method kicks off the web request and populates the results into the response. It accepts 4 arguments:
 
-- 
cgit v1.2.3


From 9428473f9de014e993152fe08277047db0f21848 Mon Sep 17 00:00:00 2001
From: Xavier Noria 
Date: Sat, 17 Apr 2010 16:43:31 +0200
Subject: AS guide: AS no longer extends Pathname

---
 railties/guides/source/active_support_core_extensions.textile | 4 ----
 1 file changed, 4 deletions(-)

(limited to 'railties')

diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile
index 398d2b2392..32738fe070 100644
--- a/railties/guides/source/active_support_core_extensions.textile
+++ b/railties/guides/source/active_support_core_extensions.textile
@@ -2662,10 +2662,6 @@ h3. Extensions to +Process+
 
 ...
 
-h3. Extensions to +Pathname+
-
-...
-
 h3. Extensions to +File+
 
 h4. +atomic_write+
-- 
cgit v1.2.3


From 15efaa701aae036031df604877b985eb2e59327e Mon Sep 17 00:00:00 2001
From: Rohit Arondekar 
Date: Mon, 19 Apr 2010 05:04:19 -0700
Subject: Fixes to the getting started guide

---
 railties/guides/source/getting_started.textile | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

(limited to 'railties')

diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile
index cbace177f9..09190f5800 100644
--- a/railties/guides/source/getting_started.textile
+++ b/railties/guides/source/getting_started.textile
@@ -87,7 +87,7 @@ Action View manages the views of your Rails application. It can create both HTML
 
 h5. Action Dispatch
 
-Action Dispatch handles routing of web requests and dispatches them as you want, either to your application, any other Rack application.
+Action Dispatch handles routing of web requests and dispatches them as you want, either to your application or any other Rack application.
 
 h5. Action Mailer
 
@@ -356,19 +356,19 @@ The scaffold generator will build 15 files in your application, along with some
 |_.File                                       |_.Purpose|
 |db/migrate/20100207214725_create_posts.rb.rb    |Migration to create the posts table in your database (your name will include a different timestamp)|
 |app/models/post.rb                           |The Post model|
-|test/unit/post_test.rb                       |Unit testing harness for the posts model|
 |test/fixtures/posts.yml                      |Dummy posts for use in testing|
 |app/controllers/posts_controller.rb          |The Posts controller|
 |app/views/posts/index.html.erb               |A view to display an index of all posts |
 |app/views/posts/edit.html.erb                |A view to edit an existing post|
 |app/views/posts/show.html.erb                |A view to display a single post|
 |app/views/posts/new.html.erb                 |A view to create a new post|
-|app/views/posts/_form.html.erb               |A view to control the overall look and feel of the other posts views|
-|app/views/layouts/posts.html.erb             |A view to control the overall look and feel of the other posts views|
+|app/views/posts/_form.html.erb               |A partial to control the overall look and feel of the form used in edit and new views|
+|app/views/layouts/posts.html.erb             |A view to control the overall look and feel of the other post views|
+|app/helpers/posts_helper.rb                  |Helper functions to be used from the post views|
+|test/unit/post_test.rb                       |Unit testing harness for the posts model|
 |test/functional/posts_controller_test.rb     |Functional testing harness for the posts controller|
-|app/helpers/posts_helper.rb                  |Helper functions to be used from the posts views|
-|config/routes.rb                             |Edited to include routing information for posts|
 |test/unit/helpers/posts_helper_test.rb       |Unit testing harness for the posts helper|
+|config/routes.rb                             |Edited to include routing information for posts|
 |public/stylesheets/scaffold.css              |Cascading style sheet to make the scaffolded views look better|
 
 h4. Running a Migration
-- 
cgit v1.2.3


From ad4615e97896428ca140411081442f1b5e7d6089 Mon Sep 17 00:00:00 2001
From: Cheah Chu Yeow 
Date: Mon, 19 Apr 2010 18:29:18 +0800
Subject: Rails on Rack Rails guide: indicate that Metal pieces now require an
 "X-Cascade" header with a value of "pass" to continue the Metal chain
 execution instead of a HTTP 404 response. Also removed reference to old code.

---
 railties/guides/source/rails_on_rack.textile | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)

(limited to 'railties')

diff --git a/railties/guides/source/rails_on_rack.textile b/railties/guides/source/rails_on_rack.textile
index d0d86e99f2..512be43668 100644
--- a/railties/guides/source/rails_on_rack.textile
+++ b/railties/guides/source/rails_on_rack.textile
@@ -247,7 +247,7 @@ class Poller
     if env["PATH_INFO"] =~ /^\/poller/
       [200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
     else
-      [404, {"Content-Type" => "text/html"}, ["Not Found"]]
+      [404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
     end
   end
 end
@@ -257,23 +257,13 @@ Metal applications within +app/metal+ folders in plugins will also be discovered
 
 Metal applications are an optimization. You should make sure to "understand the related performance implications":http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal before using it.
 
-h4. Execution Order
-
-All Metal Applications are executed by +Rails::Rack::Metal+ middleware, which is a part of the +ActionController::MiddlewareStack+ chain.
+WARNING: To continue the Metal chain execution, return an +X-Cascade+ HTTP header with a value of +pass+.
 
-Here's the primary method responsible for running the Metal applications:
+h4. Execution Order
 
-
-def call(env)
-  @metals.keys.each do |app|
-    result = app.call(env)
-    return result unless result[0].to_i == 404
-  end
-  @app.call(env)
-end
-
+All Metal Applications are executed in alphabetical order of their filenames, so +aaa.rb+ will come before +bbb.rb+ in the metal chain.
 
-In the code above, +@metals+ is an ordered hash of metal applications. Due to the default alphabetical ordering, +aaa.rb+ will come before +bbb.rb+ in the metal chain.
+You can override the default ordering in your environment. Simply add a line like the following to +config/application.rb+
 
 It is, however, possible to override the default ordering in your environment. Simply add a line like the following to +config/environment.rb+
 
@@ -283,8 +273,6 @@ config.metals = ["Bbb", "Aaa"]
 
 Each string in the array should be the name of your metal class. If you do this then be warned that any metal applications not listed will not be loaded.
 
-WARNING: Metal applications cannot return the HTTP Status +404+ to a client, as it is used for continuing the Metal chain execution. Please use normal Rails controllers or a custom middleware if returning +404+ is a requirement.
-
 h3. Resources
 
 h4. Learning Rack
-- 
cgit v1.2.3