aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Gemfile.lock6
-rw-r--r--activemodel/CHANGELOG.md24
-rw-r--r--activemodel/lib/active_model/errors.rb23
-rw-r--r--activerecord/test/cases/validations/i18n_generate_message_validation_test.rb20
-rw-r--r--activesupport/CHANGELOG.md14
-rw-r--r--activesupport/activesupport.gemspec2
-rw-r--r--activesupport/lib/active_support/core_ext/string/inflections.rb9
-rw-r--r--activesupport/lib/active_support/inflector/transliterate.rb15
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb6
-rw-r--r--activesupport/test/inflector_test.rb6
-rw-r--r--activesupport/test/transliterate_test.rb6
-rw-r--r--guides/rails_guides/generator.rb2
-rw-r--r--guides/source/association_basics.md11
-rw-r--r--guides/source/configuring.md4
-rw-r--r--railties/CHANGELOG.md6
-rw-r--r--railties/lib/rails/application/dummy_erb_compiler.rb5
-rw-r--r--railties/lib/rails/commands/credentials/USAGE2
-rw-r--r--railties/lib/rails/commands/encrypted/USAGE28
-rw-r--r--railties/lib/rails/commands/encrypted/encrypted_command.rb1
-rw-r--r--railties/test/application/rake/dbs_test.rb1
20 files changed, 160 insertions, 31 deletions
diff --git a/Gemfile.lock b/Gemfile.lock
index c778569e72..cc2bb76150 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -70,7 +70,7 @@ PATH
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
- zeitwerk (~> 1.3, >= 1.3.2)
+ zeitwerk (~> 1.3, >= 1.3.3)
rails (6.0.0.beta2)
actioncable (= 6.0.0.beta2)
actionmailbox (= 6.0.0.beta2)
@@ -277,7 +277,7 @@ GEM
hiredis (0.6.3-java)
http_parser.rb (0.6.0)
httpclient (2.8.3)
- i18n (1.5.3)
+ i18n (1.6.0)
concurrent-ruby (~> 1.0)
image_processing (1.7.1)
mini_magick (~> 4.0)
@@ -517,7 +517,7 @@ GEM
websocket-extensions (0.1.3)
xpath (3.2.0)
nokogiri (~> 1.8)
- zeitwerk (1.3.2)
+ zeitwerk (1.3.3)
PLATFORMS
java
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md
index a644c63992..d7586f60ed 100644
--- a/activemodel/CHANGELOG.md
+++ b/activemodel/CHANGELOG.md
@@ -1,3 +1,27 @@
+
+* Changed how validation error translation strings are fetched: The new behaviour
+ will first try the more specific keys, including doing locale fallback, then try
+ the less specific ones.
+
+ For example this is the order keys will now be tried for a `blank` error on a
+ `product`'s `title` attribute with current locale set to `en-US`:
+
+ en-US.activerecord.errors.models.product.attributes.title.blank
+ en-US.activerecord.errors.models.product.blank
+ en-US.activerecord.errors.messages.blank
+
+ en.activerecord.errors.models.product.attributes.title.blank
+ en.activerecord.errors.models.product.blank
+ en.activerecord.errors.messages.blank
+
+ en-US.errors.attributes.title.blank
+ en-US.errors.messages.blank
+
+ en.errors.attributes.title.blank
+ en.errors.messages.blank
+
+ *Hugo Vacher*
+
## Rails 6.0.0.beta2 (February 25, 2019) ##
* Fix date value when casting a multiparameter date hash to not convert
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 9fd6f2d89c..d7e682d406 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -479,6 +479,14 @@ module ActiveModel
# * <tt>errors.messages.blank</tt>
def generate_message(attribute, type = :invalid, options = {})
type = options.delete(:message) if options[:message].is_a?(Symbol)
+ value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil)
+
+ options = {
+ model: @base.model_name.human,
+ attribute: @base.class.human_attribute_name(attribute),
+ value: value,
+ object: @base
+ }.merge!(options)
if @base.class.respond_to?(:i18n_scope)
i18n_scope = @base.class.i18n_scope.to_s
@@ -487,6 +495,11 @@ module ActiveModel
:"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
end
defaults << :"#{i18n_scope}.errors.messages.#{type}"
+
+ catch(:exception) do
+ translation = I18n.translate(defaults.first, options.merge(default: defaults.drop(1), throw: true))
+ return translation unless translation.nil?
+ end unless options[:message]
else
defaults = []
end
@@ -496,15 +509,7 @@ module ActiveModel
key = defaults.shift
defaults = options.delete(:message) if options[:message]
- value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil)
-
- options = {
- default: defaults,
- model: @base.model_name.human,
- attribute: @base.class.human_attribute_name(attribute),
- value: value,
- object: @base
- }.merge!(options)
+ options[:default] = defaults
I18n.translate(key, options)
end
diff --git a/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb b/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb
index 703c24b340..993c201f03 100644
--- a/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb
+++ b/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb
@@ -4,16 +4,20 @@ require "cases/helper"
require "models/topic"
class I18nGenerateMessageValidationTest < ActiveRecord::TestCase
+ class Backend < I18n::Backend::Simple
+ include I18n::Backend::Fallbacks
+ end
+
def setup
Topic.clear_validators!
@topic = Topic.new
- I18n.backend = I18n::Backend::Simple.new
+ I18n.backend = Backend.new
end
def reset_i18n_load_path
@old_load_path, @old_backend = I18n.load_path.dup, I18n.backend
I18n.load_path.clear
- I18n.backend = I18n::Backend::Simple.new
+ I18n.backend = Backend.new
yield
ensure
I18n.load_path.replace @old_load_path
@@ -83,4 +87,16 @@ class I18nGenerateMessageValidationTest < ActiveRecord::TestCase
assert_equal "Custom taken message", @topic.errors.generate_message(:title, :taken, value: "title")
end
end
+
+ test "activerecord attributes scope falls back to parent locale before it falls back to the :errors namespace" do
+ reset_i18n_load_path do
+ I18n.backend.store_translations "en", activerecord: { errors: { models: { topic: { attributes: { title: { taken: "custom en message" } } } } } }
+ I18n.backend.store_translations "en-US", errors: { messages: { taken: "generic en-US fallback" } }
+
+ I18n.with_locale "en-US" do
+ assert_equal "custom en message", @topic.errors.generate_message(:title, :taken, value: "title")
+ assert_equal "generic en-US fallback", @topic.errors.generate_message(:heading, :taken, value: "heading")
+ end
+ end
+ end
end
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index ea84b54b3f..4da6421ea4 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,13 @@
+* Add support for supplying `locale` to `transliterate` and `parameterize`.
+
+ I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ü" => "ue" } } })
+
+ ActiveSupport::Inflector.transliterate("ü", locale: :de) => "ue"
+ "Fünf autos".parameterize(locale: :de) => "fuenf-autos"
+ ActiveSupport::Inflector.parameterize("Fünf autos", locale: :de) => "fuenf-autos"
+
+ *Kaan Ozkan*, *Sharang Dashputre*
+
* Allow Array#excluding and Enumerable#excluding to deal with a passed array gracefully.
[ 1, 2, 3, 4, 5 ].excluding([4, 5]) => [ 1, 2, 3 ]
@@ -10,12 +20,13 @@
*DHH*
* Added Array#including and Enumerable#including to conveniently enlarge a collection with more members using a method rather than an operator:
-
+
[ 1, 2, 3 ].including(4, 5) => [ 1, 2, 3, 4, 5 ]
post.authors.including(Current.person) => All the authors plus the current person!
*DHH*
+
## Rails 6.0.0.beta2 (February 25, 2019) ##
* New autoloading based on [Zeitwerk](https://github.com/fxn/zeitwerk).
@@ -44,6 +55,7 @@
*Guillermo Iguaran*
+
## Rails 6.0.0.beta1 (January 18, 2019) ##
* Remove deprecated `Module#reachable?` method.
diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec
index 2781d4e6f7..8409a0c420 100644
--- a/activesupport/activesupport.gemspec
+++ b/activesupport/activesupport.gemspec
@@ -34,5 +34,5 @@ Gem::Specification.new do |s|
s.add_dependency "tzinfo", "~> 1.1"
s.add_dependency "minitest", "~> 5.1"
s.add_dependency "concurrent-ruby", "~> 1.0", ">= 1.0.2"
- s.add_dependency "zeitwerk", "~> 1.3", ">= 1.3.2"
+ s.add_dependency "zeitwerk", "~> 1.3", ">= 1.3.3"
end
diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb
index 8af301734a..e63b38b227 100644
--- a/activesupport/lib/active_support/core_ext/string/inflections.rb
+++ b/activesupport/lib/active_support/core_ext/string/inflections.rb
@@ -162,6 +162,11 @@ class String
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
#
+ # If the optional parameter +locale+ is specified,
+ # the word will be parameterized as a word of that language.
+ # By default, this parameter is set to <tt>nil</tt> and it will use
+ # configured I18n.locale
+ #
# class Person
# def to_param
# "#{id}-#{name.parameterize}"
@@ -187,8 +192,8 @@ class String
#
# <%= link_to(@person.name, person_path) %>
# # => <a href="/person/1-Donald-E-Knuth">Donald E. Knuth</a>
- def parameterize(separator: "-", preserve_case: false)
- ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case)
+ def parameterize(separator: "-", preserve_case: false, locale: nil)
+ ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case, locale: locale)
end
# Creates the name of a table like Rails does for models to table names. This method
diff --git a/activesupport/lib/active_support/inflector/transliterate.rb b/activesupport/lib/active_support/inflector/transliterate.rb
index 0d2a17970f..fe778ff0e7 100644
--- a/activesupport/lib/active_support/inflector/transliterate.rb
+++ b/activesupport/lib/active_support/inflector/transliterate.rb
@@ -51,19 +51,18 @@ module ActiveSupport
#
# Now you can have different transliterations for each locale:
#
- # I18n.locale = :en
- # transliterate('Jürgen')
+ # transliterate('Jürgen', locale: :en)
# # => "Jurgen"
#
- # I18n.locale = :de
- # transliterate('Jürgen')
+ # transliterate('Jürgen', locale: :de)
# # => "Juergen"
- def transliterate(string, replacement = "?")
+ def transliterate(string, replacement = "?", locale: nil)
raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String)
I18n.transliterate(
ActiveSupport::Multibyte::Unicode.tidy_bytes(string).unicode_normalize(:nfc),
- replacement: replacement
+ replacement: replacement,
+ locale: locale
)
end
@@ -89,9 +88,9 @@ module ActiveSupport
# parameterize("^très|Jolie-- ", separator: "_") # => "tres_jolie--"
# parameterize("^très_Jolie-- ", separator: ".") # => "tres_jolie--"
#
- def parameterize(string, separator: "-", preserve_case: false)
+ def parameterize(string, separator: "-", preserve_case: false, locale: nil)
# Replace accented chars with their ASCII equivalents.
- parameterized_string = transliterate(string)
+ parameterized_string = transliterate(string, locale: locale)
# Turn unwanted chars into the separator.
parameterized_string.gsub!(/[^a-z0-9\-_]+/i, separator)
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index 2468fe3603..4ffa33aa61 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -206,6 +206,12 @@ class StringInflectionsTest < ActiveSupport::TestCase
end
end
+ def test_parameterize_with_locale
+ word = "Fünf autos"
+ I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ü" => "ue" } } })
+ assert_equal("fuenf-autos", word.parameterize(locale: :de))
+ end
+
def test_humanize
UnderscoreToHuman.each do |underscore, human|
assert_equal(human, underscore.humanize)
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index c3e1faff5d..ddf765a220 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -304,6 +304,12 @@ class InflectorTest < ActiveSupport::TestCase
end
end
+ def test_parameterize_with_locale
+ word = "Fünf autos"
+ I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ü" => "ue" } } })
+ assert_equal("fuenf-autos", ActiveSupport::Inflector.parameterize(word, locale: :de))
+ end
+
def test_classify
ClassNameToTableName.each do |class_name, table_name|
assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))
diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb
index 7d19447598..9e29a93ea0 100644
--- a/activesupport/test/transliterate_test.rb
+++ b/activesupport/test/transliterate_test.rb
@@ -30,6 +30,12 @@ class TransliterateTest < ActiveSupport::TestCase
I18n.locale = default_locale
end
+ def test_transliterate_respects_the_locale_argument
+ char = [117, 776].pack("U*") # "ü" as ASCII "u" plus COMBINING DIAERESIS
+ I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ü" => "ue" } } })
+ assert_equal "ue", ActiveSupport::Inflector.transliterate(char, locale: :de)
+ end
+
def test_transliterate_should_allow_a_custom_replacement_char
assert_equal "a*b", ActiveSupport::Inflector.transliterate("a索b", "*")
end
diff --git a/guides/rails_guides/generator.rb b/guides/rails_guides/generator.rb
index b0ddb0e7e0..c3b77aa7bb 100644
--- a/guides/rails_guides/generator.rb
+++ b/guides/rails_guides/generator.rb
@@ -150,7 +150,7 @@ module RailsGuides
puts "Generating #{guide} as #{output_file}"
layout = @kindle ? "kindle/layout" : "layout"
- view = ActionView::Base.with_view_paths(
+ view = ActionView::Base.with_empty_template_cache.with_view_paths(
[@source_dir],
edge: @edge,
version: @version,
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md
index 672faad6f1..e076f10ece 100644
--- a/guides/source/association_basics.md
+++ b/guides/source/association_basics.md
@@ -2350,6 +2350,17 @@ end
If a `before_add` callback throws an exception, the object does not get added to the collection. Similarly, if a `before_remove` callback throws an exception, the object does not get removed from the collection.
+NOTE: These callbacks are called only when the associated objects are added or removed through the association collection:
+
+```ruby
+# Triggers `before_add` callback
+author.books << book
+author.books = [book, book2]
+
+# Does not trigger the `before_add` callback
+book.update(author_id: 1)
+```
+
### Association Extensions
You're not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example:
diff --git a/guides/source/configuring.md b/guides/source/configuring.md
index bf1c0f8674..a61ba5dc9f 100644
--- a/guides/source/configuring.md
+++ b/guides/source/configuring.md
@@ -135,6 +135,10 @@ defaults to `:debug` for all environments. The available log levels are: `:debug
* `config.reload_classes_only_on_change` enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to `true`. If `config.cache_classes` is `true`, this option is ignored.
+* `config.credentials.content_path` configures lookup path for encrypted credentials.
+
+* `config.credentials.key_path` configures lookup path for encryption key.
+
* `secret_key_base` is used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering. Applications get a random generated key in test and development environments, other environments should set one in `config/credentials.yml.enc`.
* `config.public_file_server.enabled` configures Rails to serve static files from the public directory. This option defaults to `true`, but in the production environment it is set to `false` because the server software (e.g. NGINX or Apache) used to run the application should serve static files instead. If you are running or testing your app in production mode using WEBrick (it is not recommended to use WEBrick in production) set the option to `true.` Otherwise, you won't be able to use page caching and request for files that exist under the public directory.
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md
index 88f9ac06f6..d2377b3b6d 100644
--- a/railties/CHANGELOG.md
+++ b/railties/CHANGELOG.md
@@ -44,7 +44,9 @@
gsub Gemfile
```
- The change command copies a template `config/database.yml` with the target database adapter into your app, and replaces your database gem with the target database gem.
+ The change command copies a template `config/database.yml` with
+ the target database adapter into your app, and replaces your database gem
+ with the target database gem.
*Gannon McGibbon*
@@ -178,7 +180,7 @@
The encryption key can be in `ENV["RAILS_MASTER_KEY"]` or `config/credentials/production.key`.
Environment credentials overrides can be edited with `rails credentials:edit --environment production`.
- If no override is setup for the passed environment, it will be created.
+ If no override is set up for the passed environment, it will be created.
Additionally, the default lookup paths can be overwritten with these configs:
diff --git a/railties/lib/rails/application/dummy_erb_compiler.rb b/railties/lib/rails/application/dummy_erb_compiler.rb
index 4e92526969..985e6f605e 100644
--- a/railties/lib/rails/application/dummy_erb_compiler.rb
+++ b/railties/lib/rails/application/dummy_erb_compiler.rb
@@ -11,6 +11,9 @@ end
class DummyCompiler < ERB::Compiler # :nodoc:
def compile_content(stag, out)
- out.push "_erbout << 'dummy_compiler'"
+ case stag
+ when '<%='
+ out.push "_erbout << 'dummy_compiler'"
+ end
end
end
diff --git a/railties/lib/rails/commands/credentials/USAGE b/railties/lib/rails/commands/credentials/USAGE
index f7268a64d1..c8d3fb9eda 100644
--- a/railties/lib/rails/commands/credentials/USAGE
+++ b/railties/lib/rails/commands/credentials/USAGE
@@ -42,7 +42,7 @@ from leaking.
=== Environment Specific Credentials
The `credentials` command supports passing an `--environment` option to create an
-environment specific override. That override will takes precedence over the
+environment specific override. That override will take precedence over the
global `config/credentials.yml.enc` file when running in that environment. So:
rails credentials:edit --environment development
diff --git a/railties/lib/rails/commands/encrypted/USAGE b/railties/lib/rails/commands/encrypted/USAGE
new file mode 100644
index 0000000000..253eec2378
--- /dev/null
+++ b/railties/lib/rails/commands/encrypted/USAGE
@@ -0,0 +1,28 @@
+=== Storing Encrypted Files in Source Control
+
+The Rails `encrypted` commands provide access to encrypted files or configurations.
+See the `Rails.application.encrypted` documentation for using them in your app.
+
+=== Encryption Keys
+
+By default, Rails looks for the encryption key in `config/master.key` or
+`ENV["RAILS_MASTER_KEY"]`, but that lookup can be overridden with `--key`:
+
+ rails encrypted:edit config/encrypted_file.yml.enc --key config/encrypted_file.key
+
+Don't commit the key! Add it to your source control's ignore file. If you use
+Git, Rails handles this for you.
+
+=== Editing Files
+
+To edit or create an encrypted file use:
+
+ rails encrypted:edit config/encrypted_file.yml.enc
+
+This opens a temporary file in `$EDITOR` with the decrypted contents for editing.
+
+=== Viewing Files
+
+To print the decrypted contents of an encrypted file use:
+
+ rails encrypted:show config/encrypted_file.yml.enc
diff --git a/railties/lib/rails/commands/encrypted/encrypted_command.rb b/railties/lib/rails/commands/encrypted/encrypted_command.rb
index 8d5947652a..f10a07cdf8 100644
--- a/railties/lib/rails/commands/encrypted/encrypted_command.rb
+++ b/railties/lib/rails/commands/encrypted/encrypted_command.rb
@@ -16,6 +16,7 @@ module Rails
def help
say "Usage:\n #{self.class.banner}"
say ""
+ say self.class.desc
end
end
diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb
index 4f689bcb78..ea7117800c 100644
--- a/railties/test/application/rake/dbs_test.rb
+++ b/railties/test/application/rake/dbs_test.rb
@@ -53,6 +53,7 @@ module ApplicationTests
test "db:create and db:drop respect environment setting" do
app_file "config/database.yml", <<-YAML
+ <% 1 %>
development:
database: <%= Rails.application.config.database %>
adapter: sqlite3