From d0efad859b83b7ac78873e012aafcac2d1856481 Mon Sep 17 00:00:00 2001 From: Bilka Date: Tue, 21 May 2024 13:29:44 +0200 Subject: [PATCH] fix: Ruby 3.0 keyword argument error when ICE is enabled (#79) --- .../backend_service.rb | 2 +- .../delegate/i18n_delegate.rb | 4 +- .../backend_service_spec.rb | 49 +++++++++++++++++++ .../delegate/i18n_delegate_spec.rb | 9 ++-- 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/lib/phraseapp-in-context-editor-ruby/backend_service.rb b/lib/phraseapp-in-context-editor-ruby/backend_service.rb index 53e0c38..ead93b2 100644 --- a/lib/phraseapp-in-context-editor-ruby/backend_service.rb +++ b/lib/phraseapp-in-context-editor-ruby/backend_service.rb @@ -55,7 +55,7 @@ def phraseapp_delegate_for(args) key = given_key_from_args(args) return nil unless present?(key) - options = args[1].nil? ? {} : args[1] + options = options_from_args(args) PhraseApp::InContextEditor::Delegate::I18nDelegate.new(key, options, args) end diff --git a/lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb b/lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb index 8d20a4f..fcc4d14 100644 --- a/lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb +++ b/lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb @@ -4,7 +4,7 @@ module PhraseApp module InContextEditor module Delegate class I18nDelegate < Base - attr_accessor :key, :display_key, :options, :api_client, :fallback_keys, :original_args + attr_accessor :key, :display_key def initialize(key, options = {}, original_args = nil) @key = key @@ -24,7 +24,7 @@ def method_missing(*args, &block) data.send(*args, &block) else 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." - original_translation = ::I18n.translate_without_phraseapp(*@original_args) + original_translation = ::I18n.translate_without_phraseapp(*@original_args, **@options) original_translation.send(*args, &block) end end diff --git a/spec/phraseapp-in-context-editor-ruby/backend_service_spec.rb b/spec/phraseapp-in-context-editor-ruby/backend_service_spec.rb index c307d04..8fe351a 100644 --- a/spec/phraseapp-in-context-editor-ruby/backend_service_spec.rb +++ b/spec/phraseapp-in-context-editor-ruby/backend_service_spec.rb @@ -128,4 +128,53 @@ it { is_expected.to eql "my.key" } end end + + describe "delegation of methods on translatable values" do + let(:key) { "foo" } + let(:options) { {} } + let(:delegate) { phraseapp_service.translate(key, **options)} + + before(:each) do + PhraseApp::InContextEditor.enabled = true + end + + context "string method to decorated_key_name" do + subject { delegate.include? "phrase_foo" } + let(:method_return) { true } + + before(:each) do + expect(I18n).not_to receive(:translate_without_phraseapp) + end + + it { is_expected.to eql method_return } + + context "with an option set" do + let(:options) { {locale: :en} } + + it { is_expected.to eql method_return } + end + end + + context "non-string method to original translation" do + subject { delegate.value? 3 } + + let(:i18n_translation) { {baz: 3} } + let(:scoped) { "bar" } + let(:i18n_translation_scoped) { {baz: 5} } + + before(:each) do + I18n.backend.store_translations(:en, {key => i18n_translation}) + I18n.backend.store_translations(:en, {scoped => {key => i18n_translation_scoped}}) + expect(I18n).to receive(:translate_without_phraseapp).and_call_original + end + + it { is_expected.to eql true } + + context "with an option set" do + let(:options) { {scope: scoped} } + + it { is_expected.to eql false } + end + end + end end diff --git a/spec/phraseapp-in-context-editor-ruby/delegate/i18n_delegate_spec.rb b/spec/phraseapp-in-context-editor-ruby/delegate/i18n_delegate_spec.rb index 24ee900..2e3d83d 100644 --- a/spec/phraseapp-in-context-editor-ruby/delegate/i18n_delegate_spec.rb +++ b/spec/phraseapp-in-context-editor-ruby/delegate/i18n_delegate_spec.rb @@ -3,13 +3,8 @@ require "phraseapp-in-context-editor-ruby/delegate/i18n_delegate" describe PhraseApp::InContextEditor::Delegate::I18nDelegate do - let(:key) { "foo.bar" } - let(:options) { {} } - let(:original_args) { double } let(:delegate) { PhraseApp::InContextEditor::Delegate::I18nDelegate.new(key) } - subject { delegate } - describe "#to_s" do let(:key) { "foo.bar" } subject { delegate.to_s } @@ -19,6 +14,7 @@ end describe "#camelize" do + let(:key) { "foo.bar" } subject { delegate.camelize } it { is_expected.to be_a String } @@ -58,6 +54,9 @@ end describe "#decorated_key_name" do + let(:key) { "foo.bar" } + subject { delegate } + it "should include the phrase prefix" do allow(PhraseApp::InContextEditor).to receive(:prefix).and_return("??") expect(subject.send(:decorated_key_name).start_with?("??")).to be_truthy