Skip to content

Commit 4617e77

Browse files
Dishwasharichmolj
authored andcommitted
Allow stats to optionally gain access to resource (#136)
Some stats may need the data to react to conditions passed to the resource so it is useful to allow that without breaking the api for existing measures.
1 parent afbdd8d commit 4617e77

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/jsonapi_compliable/stats/payload.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def generate
3333
stats[name] = {}
3434

3535
each_calculation(name, calculation) do |calc, function|
36-
stats[name][calc] = function.call(@scope, name)
36+
args = function.arity == 3 ? [@scope, name, @resource] : [@scope, name]
37+
stats[name][calc] = function.call(*args)
3738
end
3839
end
3940
end

spec/stats/payload_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@
99
describe '#generate' do
1010
subject { instance.generate }
1111

12-
def stub_stat(attr, calc, result)
13-
allow(dsl).to receive(:stat).with(attr, calc) { ->(_,_) { result } }
12+
def stub_stat(attr, calc, result, with_resource=false)
13+
if with_resource
14+
allow(dsl).to receive(:stat).with(attr, calc) { ->(_,_,_) { result } }
15+
else
16+
allow(dsl).to receive(:stat).with(attr, calc) { ->(_,_) { result } }
17+
end
1418
end
1519

1620
before do
1721
stub_stat(:attr1, :count, 2)
1822
stub_stat(:attr1, :average, 1)
1923
stub_stat(:attr2, :maximum, 3)
24+
stub_stat(:attr3, :maximum, 3, true)
2025
end
2126

2227
it 'generates the correct payload for each requested stat' do

0 commit comments

Comments
 (0)