Skip to content

Commit 81563b7

Browse files
author
Edwin Cruz
committed
Fixing stack too deep bug when configs had nested hashes
1 parent e33721c commit 81563b7

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

lib/setting.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ def collapse_hashes(v, args)
138138
else
139139
v
140140
end
141-
142-
if out.is_a?(Hash)
143-
collapse_hashes(out, args)
141+
if out.is_a?(Hash) && !args.empty?
142+
collapse_hashes(out, args)
143+
elsif out.is_a?(Hash) && out.has_key?('default')
144+
out['default']
144145
else
145146
out
146147
end

spec/fixtures/shipping.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
shipping_config:
2+
default: Defaulted
3+
domestic:
4+
service: MyService
5+
countries:
6+
- "US"
7+
non_shippable_regions:
8+
- "US-AS"
9+
- "US-GU"
10+
- "US-MP"
11+
- "US-PR"
12+
- "US-UM"
13+
- "US-VI"
14+
- "US-AF"
15+
- "US-AA"
16+
- "US-AC"
17+
- "US-AE"
18+
- "US-AM"
19+
- "US-AP"
20+
- "US-FM"
21+
- "US-PW"
22+
- "US-MH"
23+
international:
24+
service: Foo
25+
countries:
26+
- "GU"
27+
- "VI"
28+
- "AS"
29+
- "MP"
30+
- "UM"
31+
- "FR"
32+
- "GR"
33+
- "RU"
34+
shipping_carrier: Bar

spec/mc_settings_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,21 @@
126126
end
127127

128128
end
129+
context "Complex nested configs" do
130+
before :each do
131+
Setting.reload(
132+
:path => File.join(File.dirname(__FILE__)) + '/fixtures',
133+
:files => ['shipping.yml']
134+
)
135+
end
136+
it "should build correct tree with arrays and default values " do
137+
Setting.shipping_config.should == "Defaulted"
138+
Setting.shipping_config(:domestic, :non_shippable_regions).first.should == "US-AS"
139+
Setting.shipping_config(:international, :service).should == 'Foo'
140+
Setting.shipping_config(:international, :countries).size.should > 0
141+
Setting.shipping_config(:international, :shipping_carrier).should == 'Bar'
142+
#backward compatibility:
143+
Setting.shipping_config(:domestic)['non_shippable_regions'].size.should > 0
144+
end
145+
end
129146
end

0 commit comments

Comments
 (0)