Skip to content

Commit d0efad8

Browse files
authored
fix: Ruby 3.0 keyword argument error when ICE is enabled (#79)
1 parent 9ec6741 commit d0efad8

4 files changed

Lines changed: 56 additions & 8 deletions

File tree

lib/phraseapp-in-context-editor-ruby/backend_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def phraseapp_delegate_for(args)
5555
key = given_key_from_args(args)
5656
return nil unless present?(key)
5757

58-
options = args[1].nil? ? {} : args[1]
58+
options = options_from_args(args)
5959
PhraseApp::InContextEditor::Delegate::I18nDelegate.new(key, options, args)
6060
end
6161

lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module PhraseApp
44
module InContextEditor
55
module Delegate
66
class I18nDelegate < Base
7-
attr_accessor :key, :display_key, :options, :api_client, :fallback_keys, :original_args
7+
attr_accessor :key, :display_key
88

99
def initialize(key, options = {}, original_args = nil)
1010
@key = key
@@ -24,7 +24,7 @@ def method_missing(*args, &block)
2424
data.send(*args, &block)
2525
else
2626
self.class.log "You tried to execute the missing method ##{args.first} on key #{@key} which is not supported. Please make sure you treat your translations as strings only."
27-
original_translation = ::I18n.translate_without_phraseapp(*@original_args)
27+
original_translation = ::I18n.translate_without_phraseapp(*@original_args, **@options)
2828
original_translation.send(*args, &block)
2929
end
3030
end

spec/phraseapp-in-context-editor-ruby/backend_service_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,53 @@
128128
it { is_expected.to eql "my.key" }
129129
end
130130
end
131+
132+
describe "delegation of methods on translatable values" do
133+
let(:key) { "foo" }
134+
let(:options) { {} }
135+
let(:delegate) { phraseapp_service.translate(key, **options)}
136+
137+
before(:each) do
138+
PhraseApp::InContextEditor.enabled = true
139+
end
140+
141+
context "string method to decorated_key_name" do
142+
subject { delegate.include? "phrase_foo" }
143+
let(:method_return) { true }
144+
145+
before(:each) do
146+
expect(I18n).not_to receive(:translate_without_phraseapp)
147+
end
148+
149+
it { is_expected.to eql method_return }
150+
151+
context "with an option set" do
152+
let(:options) { {locale: :en} }
153+
154+
it { is_expected.to eql method_return }
155+
end
156+
end
157+
158+
context "non-string method to original translation" do
159+
subject { delegate.value? 3 }
160+
161+
let(:i18n_translation) { {baz: 3} }
162+
let(:scoped) { "bar" }
163+
let(:i18n_translation_scoped) { {baz: 5} }
164+
165+
before(:each) do
166+
I18n.backend.store_translations(:en, {key => i18n_translation})
167+
I18n.backend.store_translations(:en, {scoped => {key => i18n_translation_scoped}})
168+
expect(I18n).to receive(:translate_without_phraseapp).and_call_original
169+
end
170+
171+
it { is_expected.to eql true }
172+
173+
context "with an option set" do
174+
let(:options) { {scope: scoped} }
175+
176+
it { is_expected.to eql false }
177+
end
178+
end
179+
end
131180
end

spec/phraseapp-in-context-editor-ruby/delegate/i18n_delegate_spec.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
require "phraseapp-in-context-editor-ruby/delegate/i18n_delegate"
44

55
describe PhraseApp::InContextEditor::Delegate::I18nDelegate do
6-
let(:key) { "foo.bar" }
7-
let(:options) { {} }
8-
let(:original_args) { double }
96
let(:delegate) { PhraseApp::InContextEditor::Delegate::I18nDelegate.new(key) }
107

11-
subject { delegate }
12-
138
describe "#to_s" do
149
let(:key) { "foo.bar" }
1510
subject { delegate.to_s }
@@ -19,6 +14,7 @@
1914
end
2015

2116
describe "#camelize" do
17+
let(:key) { "foo.bar" }
2218
subject { delegate.camelize }
2319

2420
it { is_expected.to be_a String }
@@ -58,6 +54,9 @@
5854
end
5955

6056
describe "#decorated_key_name" do
57+
let(:key) { "foo.bar" }
58+
subject { delegate }
59+
6160
it "should include the phrase prefix" do
6261
allow(PhraseApp::InContextEditor).to receive(:prefix).and_return("??")
6362
expect(subject.send(:decorated_key_name).start_with?("??")).to be_truthy

0 commit comments

Comments
 (0)