From 1be160bb9724b254579f54f8b4dae0bffa443be5 Mon Sep 17 00:00:00 2001 From: Waqas Ali Date: Tue, 14 Jan 2025 08:59:14 +0500 Subject: [PATCH] Fix argument errors of LightService::LocalizationAdapter (#263) * Refactor Adapters to unify method interfaces * Fix README --- README.md | 2 +- .../i18n/localization_adapter.rb | 16 ++-- lib/light-service/localization_adapter.rb | 12 +-- spec/context_spec.rb | 85 ++++++++++++++----- 4 files changed, 76 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index eff4f269..c0c30e15 100644 --- a/README.md +++ b/README.md @@ -882,7 +882,7 @@ If you have `I18n` loaded in your project the default adapter will automatically But would you want to opt for the built-in localization adapter you can force it with ```ruby -LightService::Configuration.localization_adapter = LightService::LocalizationAdapter +LightService::Configuration.localization_adapter = LightService::LocalizationAdapter.new ``` ### I18n localization adapter diff --git a/lib/light-service/i18n/localization_adapter.rb b/lib/light-service/i18n/localization_adapter.rb index d66f088e..21a6cb82 100644 --- a/lib/light-service/i18n/localization_adapter.rb +++ b/lib/light-service/i18n/localization_adapter.rb @@ -1,29 +1,25 @@ module LightService module I18n class LocalizationAdapter - def failure(message_or_key, action_class, i18n_options = {}) + def failure(message_or_key, action_class, options = {}) find_translated_message(message_or_key, action_class, - i18n_options, - :type => :failure) + options.merge(:type => :failure)) end - def success(message_or_key, action_class, i18n_options = {}) + def success(message_or_key, action_class, options = {}) find_translated_message(message_or_key, action_class, - i18n_options, - :type => :success) + options.merge(:type => :success)) end private def find_translated_message(message_or_key, action_class, - i18n_options, - type) + options) if message_or_key.is_a?(Symbol) - i18n_options.merge!(type) - translate(message_or_key, action_class, i18n_options) + translate(message_or_key, action_class, options) else message_or_key end diff --git a/lib/light-service/localization_adapter.rb b/lib/light-service/localization_adapter.rb index 0c95c665..d2428e97 100644 --- a/lib/light-service/localization_adapter.rb +++ b/lib/light-service/localization_adapter.rb @@ -1,26 +1,26 @@ module LightService class LocalizationAdapter - def failure(message_or_key, action_class) + def failure(message_or_key, action_class, options = {}) find_translated_message(message_or_key, action_class.to_s.underscore, - :failures) + options.merge(:type => :failures)) end - def success(message_or_key, action_class) + def success(message_or_key, action_class, options = {}) find_translated_message(message_or_key, action_class.to_s.underscore, - :successes) + options.merge(:type => :successes)) end private - def find_translated_message(message_or_key, action_class, type) + def find_translated_message(message_or_key, action_class, options) if message_or_key.is_a?(Symbol) LightService::LocalizationMap.instance.dig( LightService::Configuration.locale, action_class.to_sym, :light_service, - type, + options[:type], message_or_key ) else diff --git a/spec/context_spec.rb b/spec/context_spec.rb index a9d9b2dd..7afd009b 100644 --- a/spec/context_spec.rb +++ b/spec/context_spec.rb @@ -3,6 +3,7 @@ RSpec.describe LightService::Context do let(:context) { LightService::Context.make } + let(:adapter_double) { instance_double("LightService::LocalizationAdapter") } describe "can be made" do context "with no arguments" do @@ -98,34 +99,74 @@ expect(context.error_code).to eq(10_005) end - it "uses localization adapter to translate failure message" do - action_class = TestDoubles::AnAction - expect(LightService::Configuration.localization_adapter) - .to receive(:failure) - .with(:failure_reason, action_class, {}) - .and_return("message") + context "when I18n is defined" do + it "uses localization adapter to translate failure message" do + action_class = TestDoubles::AnAction + expect(LightService::Configuration.localization_adapter) + .to receive(:failure) + .with(:failure_reason, action_class, {}) + .and_return("message") - context = LightService::Context.make - context.current_action = action_class - context.fail!(:failure_reason) + context = LightService::Context.make + context.current_action = action_class + context.fail!(:failure_reason) - expect(context).to be_failure - expect(context.message).to eq("message") + expect(context).to be_failure + expect(context.message).to eq("message") + end + + it "uses localization adapter to translate success message" do + action_class = TestDoubles::AnAction + expect(LightService::Configuration.localization_adapter) + .to receive(:success) + .with(:action_passed, action_class, {}) + .and_return("message") + + context = LightService::Context.make + context.current_action = action_class + context.succeed!(:action_passed) + + expect(context).to be_success + expect(context.message).to eq("message") + end end - it "uses localization adapter to translate success message" do - action_class = TestDoubles::AnAction - expect(LightService::Configuration.localization_adapter) - .to receive(:success) - .with(:action_passed, action_class, {}) - .and_return("message") + context "when I18n is not defined" do + before do + allow(LightService::Configuration) + .to receive(:localization_adapter) + .and_return(adapter_double) + end + + it "uses localization adapter to translate failure message" do + action_class = TestDoubles::AnAction + expect(LightService::Configuration.localization_adapter) + .to receive(:failure) + .with(:failure_reason, TestDoubles::AnAction, {}) + .and_return("message") - context = LightService::Context.make - context.current_action = action_class - context.succeed!(:action_passed) + context = LightService::Context.make + context.current_action = action_class + context.fail!(:failure_reason) - expect(context).to be_success - expect(context.message).to eq("message") + expect(context).to be_failure + expect(context.message).to eq("message") + end + + it "uses localization adapter to translate success message" do + action_class = TestDoubles::AnAction + expect(LightService::Configuration.localization_adapter) + .to receive(:success) + .with(:action_passed, action_class, {}) + .and_return("message") + + context = LightService::Context.make + context.current_action = action_class + context.succeed!(:action_passed) + + expect(context).to be_success + expect(context.message).to eq("message") + end end it "can set a flag to skip all subsequent actions" do