Skip to content

Commit 28537aa

Browse files
authored
Merge pull request #119 from ryanhertz/MWEB-1217-handle-nil-default
Mweb 1217 handle nil default
2 parents ed08d73 + 95243a7 commit 28537aa

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v1.4.3
2+
- Change Finders and Defaultable to handle an api response of an empty array when finding a single resource.
3+
14
v1.4.2
25
- Added Defaultable module
36

Guardfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
guard :rspec, cmd: 'rspec' do
3+
watch(%r{^spec/.+_spec\.rb$})
4+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
5+
watch('spec/spec_helper.rb') { "spec" }
6+
end

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.2
1+
1.4.3

lib/spark_api/models/defaultable.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ class << base
1919
module ClassMethods
2020

2121
def default(options = {})
22-
find(DEFAULT_ID, options)
22+
response = connection.get("/#{element_name}/default", options).first
23+
unless response.nil?
24+
response[:Id] = DEFAULT_ID if response[:Id].nil?
25+
new(response)
26+
end
2327
end
2428

2529
def find(*arguments)
26-
result = original_find(*arguments)
2730
if arguments.first == DEFAULT_ID
28-
result.Id = DEFAULT_ID if result.Id.nil?
31+
default
32+
else
33+
original_find(*arguments)
2934
end
30-
result
3135
end
3236

3337
end

spark_api.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ Gem::Specification.new do |s|
4848
s.add_development_dependency 'rb-fsevent'
4949
s.add_development_dependency 'simplecov'
5050
s.add_development_dependency 'simplecov-rcov'
51+
s.add_development_dependency 'listen', '~> 3.0.8' # for guard-rspec with ruby 1.9.3
52+
s.add_development_dependency 'guard-rspec'
5153
end
5254

spec/unit/spark_api/models/defaultable_spec.rb

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,33 @@ class TestClass < Base
1414

1515
describe 'default' do
1616

17-
it 'calls find' do
18-
expect(TestClass).to receive(:find).with('default', {})
19-
TestClass.default
17+
it 'returns an instance of the class' do
18+
allow(TestClass).to receive(:connection).and_return(double(get: [{Name: 'foo'}]))
19+
expect(TestClass.default).to be_a TestClass
20+
end
21+
22+
it 'returns nil when there are no results' do
23+
allow(TestClass).to receive(:connection).and_return(double(get: []))
24+
expect(TestClass.default).to be nil
2025
end
21-
22-
end
23-
24-
describe 'find' do
2526

26-
it "adds the id 'default'" do
27-
allow(TestClass).to receive(:connection).and_return(double(get: [{Id: nil, Name: 'foo'}]))
28-
default = TestClass.find(TestClass::DEFAULT_ID)
29-
expect(default.Id).to eq 'default'
27+
it "assigns the default id to the instance if it doesn't have an id" do
28+
allow(TestClass).to receive(:connection).and_return(double(get: [{Name: 'foo'}]))
29+
expect(TestClass.default.Id).to eq TestClass::DEFAULT_ID
3030
end
3131

3232
it "doesn't override the id if one is present" do
3333
allow(TestClass).to receive(:connection).and_return(double(get: [{Id: '5', Name: 'foo'}]))
34-
expect(TestClass.find(TestClass::DEFAULT_ID).Id).to eq '5'
34+
expect(TestClass.default.Id).to eq '5'
35+
end
36+
37+
end
38+
39+
describe 'find' do
40+
41+
it "calls 'default' when given the default id" do
42+
expect(TestClass).to receive(:default)
43+
TestClass.find(TestClass::DEFAULT_ID)
3544
end
3645

3746
it "calls Finders.find when given a normal id" do

spec/unit/spark_api/models/finders_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ class MyResource < Base
4949
end
5050

5151
end
52+
5253
end

0 commit comments

Comments
 (0)