-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more functionality to Foundation
- translation safety - module nesting - also clean up outdated subclass tracking for route class files
- Loading branch information
1 parent
bef4af8
commit adc2c88
Showing
11 changed files
with
116 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ inherit_from: ../.rubocop.yml | |
AllCops: | ||
Exclude: | ||
- "*.gemspec" | ||
- "script/console" |
32 changes: 32 additions & 0 deletions
32
bridgetown-foundation/lib/bridgetown/foundation/core_ext/module.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bridgetown::Foundation | ||
module CoreExt | ||
module Module | ||
module Nested | ||
def nested_within?(other) | ||
other.nested_parents.within?(nested_parents[1..]) | ||
end | ||
|
||
def nested_parents | ||
return [] unless name | ||
|
||
nesting_segments = name.split("::")[...-1] | ||
nesting_segments.map.each_with_index do |_nesting_name, index| | ||
Kernel.const_get(nesting_segments[..-(index + 1)].join("::")) | ||
end | ||
end | ||
|
||
def nested_parent | ||
nested_parents.first | ||
end | ||
|
||
def nested_name | ||
name&.split("::")&.last | ||
end | ||
end | ||
|
||
::Module.include Nested | ||
end | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
bridgetown-foundation/lib/bridgetown/foundation/safe_translations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bridgetown | ||
module Foundation | ||
# NOTE: this is tested by `test/test_ruby_helpers.rb` in bridgetown-core | ||
# | ||
# This is loosely based on the HtmlSafeTranslation module from ActiveSupport, but you can | ||
# actually use it for any kind of safety use case in a translation setting because its | ||
# decoupled from any specific escaping or safety mechanisms. | ||
module SafeTranslations | ||
def self.translate(key, escaper, safety_method = :html_safe, **options) | ||
safe_options = escape_translation_options(options, escaper) | ||
|
||
i18n_error = false | ||
|
||
exception_handler = ->(*args) do | ||
i18n_error = true | ||
I18n.exception_handler.(*args) | ||
end | ||
|
||
I18n.translate(key, **safe_options, exception_handler:).then do |translation| | ||
i18n_error ? translation : safe_translation(translation, safety_method) | ||
end | ||
end | ||
|
||
def self.escape_translation_options(options, escaper) | ||
@reserved_i18n_keys ||= I18n::RESERVED_KEYS.to_set | ||
|
||
options.to_h do |name, value| | ||
unless @reserved_i18n_keys.include?(name) || (name == :count && value.is_a?(Numeric)) | ||
next [name, escaper.(value)] | ||
end | ||
|
||
[name, value] | ||
end | ||
end | ||
|
||
def self.safe_translation(translation, safety_method) | ||
@safe_value ||= -> { _1.respond_to?(safety_method) ? _1.send(safety_method) : _1 } | ||
|
||
return translation.map { @safe_value.(_1) } if translation.respond_to?(:map) | ||
|
||
@safe_value.(translation) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require "bundler" | ||
Bundler.setup | ||
|
||
require "bridgetown-foundation" | ||
|
||
module Bridgetown | ||
module Foundation | ||
binding.irb | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters