File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -349,6 +349,15 @@ like so:
349
349
> Order .find(1 )
350
350
# <Order id=1 total=54.47>
351
351
352
+ > Order .find_by(id: 1 )
353
+ # <Order id=1 total=54.47>
354
+
355
+ > Order .find_by!(id: - 1 , state: ' active' )
356
+ # Frenetic::ResourceNotFound: Couldn't find Order with id=-1, state=active
357
+
358
+ > Order .find_by(id: - 1 )
359
+ # nil
360
+
352
361
> Order .all
353
362
# [<Order id=1 total=54.47>,<Order id=2 total=42.00>]
354
363
```
Original file line number Diff line number Diff line change @@ -18,6 +18,16 @@ def find(params)
18
18
new ( response . body ) if response . success?
19
19
end
20
20
21
+ def find_by! ( params )
22
+ find ( params )
23
+ end
24
+
25
+ def find_by ( params )
26
+ find_by! ( params )
27
+ rescue ClientError
28
+ nil
29
+ end
30
+
21
31
def all
22
32
return [ ] if test_mode?
23
33
response = api . get ( collection_url )
Original file line number Diff line number Diff line change 85
85
end
86
86
end
87
87
88
+ describe '.find_by' do
89
+ before { @stubs . api_description }
90
+
91
+ subject { MyTempResource . find_by ( id :1 ) }
92
+
93
+ it 'delegates to .find' do
94
+ allow ( MyTempResource ) . to receive ( :find ) . and_return ( nil )
95
+ subject
96
+ expect ( MyTempResource ) . to have_received ( :find )
97
+ end
98
+
99
+ context 'for an unknown resource' do
100
+ it 'returns nil' do
101
+ allow ( MyTempResource ) . to receive ( :find ) . and_raise ( Frenetic ::ClientError . new ( { } ) )
102
+ expect { subject } . to_not raise_error
103
+ end
104
+ end
105
+ end
106
+
88
107
describe '.all' do
89
108
before { @stubs . api_description }
90
109
You can’t perform that action at this time.
0 commit comments