Skip to content

Commit

Permalink
Fix argument errors of LightService::LocalizationAdapter (#263)
Browse files Browse the repository at this point in the history
* Refactor Adapters to unify method interfaces

* Fix README
  • Loading branch information
wqsaali authored Jan 14, 2025
1 parent aa27ba1 commit 1be160b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions lib/light-service/i18n/localization_adapter.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/light-service/localization_adapter.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
85 changes: 63 additions & 22 deletions spec/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1be160b

Please sign in to comment.