New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make context safe for interpolation #15
Conversation
load_translations(users: { show: 'User' }) | ||
allow(helper).to receive_message_chain(:controller, :view_assigns).and_return('scope' => 'Foo') | ||
|
||
expect(helper.title).to eq('User') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
spec/helpers/title_helper_spec.rb
Outdated
stub_rails | ||
stub_controller_and_action(:users, :show) | ||
load_translations(users: { show: 'User' }) | ||
allow(helper).to receive_message_chain(:controller, :view_assigns).and_return('scope' => 'Foo') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [99/80]
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
spec/helpers/title_helper_spec.rb
Outdated
it 'makes context safe to be used as interpolation options' do | ||
stub_rails | ||
stub_controller_and_action(:users, :show) | ||
load_translations(users: { show: 'User' }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
spec/helpers/title_helper_spec.rb
Outdated
@@ -51,6 +51,15 @@ | |||
expect(helper.title(greeting: 'Hello')).to eq('Hello Caleb') | |||
end | |||
|
|||
it 'makes context safe to be used as interpolation options' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
app/helpers/title/title_helper.rb
Outdated
@@ -59,6 +59,10 @@ def adjusted_action_name(action_name) | |||
action_name | |||
end | |||
end | |||
|
|||
def safe_context | |||
context.except *I18n::RESERVED_KEYS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ambiguous splat operator. Parenthesize the method arguments if it's surely a splat operator, or add a whitespace to the right of the * if it should be a multiplication.
211f831
to
be13d7c
Compare
be13d7c
to
5b1f9f9
Compare
@calebthompson Hi, just a mention to see if this can be merged in (or not). Thanks! |
5b1f9f9
to
a5f0c5e
Compare
load_translations(users: { show: 'User' }) | ||
allow(helper).to receive_message_chain(:controller, :view_assigns).and_return('scope' => 'Foo') | ||
|
||
expect(helper.title).to eq('User') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
stub_rails | ||
stub_controller_and_action(:users, :show) | ||
load_translations(users: { show: 'User' }) | ||
allow(helper).to receive_message_chain(:controller, :view_assigns).and_return('scope' => 'Foo') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [99/80]
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
it 'makes context safe to be passed as interpolation options' do | ||
stub_rails | ||
stub_controller_and_action(:users, :show) | ||
load_translations(users: { show: 'User' }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
@@ -51,6 +51,15 @@ | |||
expect(helper.title(greeting: 'Hello')).to eq('Hello Caleb') | |||
end | |||
|
|||
it 'makes context safe to be passed as interpolation options' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Fixes an issue related to colliding context and i18n reserved keys, which results in application name being rendered on translation lookup failure. The chance of encountering this problem is real as some of these names are relatively common terms.
cascade
deep_interpolation
fallback
fallback_in_progress
format
object
raise
resolve
scope
separator
throw
Please see: https://github.com/svenfuchs/i18n/blob/405b672ed121d48d2e41140da29f63b350b9899e/lib/i18n.rb#L15.
This change filters out reserved keys from context before translation.