Skip to content

Commit 1be160b

Browse files
authored
Fix argument errors of LightService::LocalizationAdapter (#263)
* Refactor Adapters to unify method interfaces * Fix README
1 parent aa27ba1 commit 1be160b

File tree

4 files changed

+76
-39
lines changed

4 files changed

+76
-39
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ If you have `I18n` loaded in your project the default adapter will automatically
882882
But would you want to opt for the built-in localization adapter you can force it with
883883
884884
```ruby
885-
LightService::Configuration.localization_adapter = LightService::LocalizationAdapter
885+
LightService::Configuration.localization_adapter = LightService::LocalizationAdapter.new
886886
```
887887
888888
### I18n localization adapter

lib/light-service/i18n/localization_adapter.rb

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
module LightService
22
module I18n
33
class LocalizationAdapter
4-
def failure(message_or_key, action_class, i18n_options = {})
4+
def failure(message_or_key, action_class, options = {})
55
find_translated_message(message_or_key,
66
action_class,
7-
i18n_options,
8-
:type => :failure)
7+
options.merge(:type => :failure))
98
end
109

11-
def success(message_or_key, action_class, i18n_options = {})
10+
def success(message_or_key, action_class, options = {})
1211
find_translated_message(message_or_key,
1312
action_class,
14-
i18n_options,
15-
:type => :success)
13+
options.merge(:type => :success))
1614
end
1715

1816
private
1917

2018
def find_translated_message(message_or_key,
2119
action_class,
22-
i18n_options,
23-
type)
20+
options)
2421
if message_or_key.is_a?(Symbol)
25-
i18n_options.merge!(type)
26-
translate(message_or_key, action_class, i18n_options)
22+
translate(message_or_key, action_class, options)
2723
else
2824
message_or_key
2925
end

lib/light-service/localization_adapter.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
module LightService
22
class LocalizationAdapter
3-
def failure(message_or_key, action_class)
3+
def failure(message_or_key, action_class, options = {})
44
find_translated_message(message_or_key,
55
action_class.to_s.underscore,
6-
:failures)
6+
options.merge(:type => :failures))
77
end
88

9-
def success(message_or_key, action_class)
9+
def success(message_or_key, action_class, options = {})
1010
find_translated_message(message_or_key,
1111
action_class.to_s.underscore,
12-
:successes)
12+
options.merge(:type => :successes))
1313
end
1414

1515
private
1616

17-
def find_translated_message(message_or_key, action_class, type)
17+
def find_translated_message(message_or_key, action_class, options)
1818
if message_or_key.is_a?(Symbol)
1919
LightService::LocalizationMap.instance.dig(
2020
LightService::Configuration.locale,
2121
action_class.to_sym,
2222
:light_service,
23-
type,
23+
options[:type],
2424
message_or_key
2525
)
2626
else

spec/context_spec.rb

+63-22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
RSpec.describe LightService::Context do
55
let(:context) { LightService::Context.make }
6+
let(:adapter_double) { instance_double("LightService::LocalizationAdapter") }
67

78
describe "can be made" do
89
context "with no arguments" do
@@ -98,34 +99,74 @@
9899
expect(context.error_code).to eq(10_005)
99100
end
100101

101-
it "uses localization adapter to translate failure message" do
102-
action_class = TestDoubles::AnAction
103-
expect(LightService::Configuration.localization_adapter)
104-
.to receive(:failure)
105-
.with(:failure_reason, action_class, {})
106-
.and_return("message")
102+
context "when I18n is defined" do
103+
it "uses localization adapter to translate failure message" do
104+
action_class = TestDoubles::AnAction
105+
expect(LightService::Configuration.localization_adapter)
106+
.to receive(:failure)
107+
.with(:failure_reason, action_class, {})
108+
.and_return("message")
107109

108-
context = LightService::Context.make
109-
context.current_action = action_class
110-
context.fail!(:failure_reason)
110+
context = LightService::Context.make
111+
context.current_action = action_class
112+
context.fail!(:failure_reason)
111113

112-
expect(context).to be_failure
113-
expect(context.message).to eq("message")
114+
expect(context).to be_failure
115+
expect(context.message).to eq("message")
116+
end
117+
118+
it "uses localization adapter to translate success message" do
119+
action_class = TestDoubles::AnAction
120+
expect(LightService::Configuration.localization_adapter)
121+
.to receive(:success)
122+
.with(:action_passed, action_class, {})
123+
.and_return("message")
124+
125+
context = LightService::Context.make
126+
context.current_action = action_class
127+
context.succeed!(:action_passed)
128+
129+
expect(context).to be_success
130+
expect(context.message).to eq("message")
131+
end
114132
end
115133

116-
it "uses localization adapter to translate success message" do
117-
action_class = TestDoubles::AnAction
118-
expect(LightService::Configuration.localization_adapter)
119-
.to receive(:success)
120-
.with(:action_passed, action_class, {})
121-
.and_return("message")
134+
context "when I18n is not defined" do
135+
before do
136+
allow(LightService::Configuration)
137+
.to receive(:localization_adapter)
138+
.and_return(adapter_double)
139+
end
140+
141+
it "uses localization adapter to translate failure message" do
142+
action_class = TestDoubles::AnAction
143+
expect(LightService::Configuration.localization_adapter)
144+
.to receive(:failure)
145+
.with(:failure_reason, TestDoubles::AnAction, {})
146+
.and_return("message")
122147

123-
context = LightService::Context.make
124-
context.current_action = action_class
125-
context.succeed!(:action_passed)
148+
context = LightService::Context.make
149+
context.current_action = action_class
150+
context.fail!(:failure_reason)
126151

127-
expect(context).to be_success
128-
expect(context.message).to eq("message")
152+
expect(context).to be_failure
153+
expect(context.message).to eq("message")
154+
end
155+
156+
it "uses localization adapter to translate success message" do
157+
action_class = TestDoubles::AnAction
158+
expect(LightService::Configuration.localization_adapter)
159+
.to receive(:success)
160+
.with(:action_passed, action_class, {})
161+
.and_return("message")
162+
163+
context = LightService::Context.make
164+
context.current_action = action_class
165+
context.succeed!(:action_passed)
166+
167+
expect(context).to be_success
168+
expect(context.message).to eq("message")
169+
end
129170
end
130171

131172
it "can set a flag to skip all subsequent actions" do

0 commit comments

Comments
 (0)