Skip to content

Commit 35ac7c8

Browse files
committed
restrict InertiRails.always to only acept blocks
1 parent 13ab57f commit 35ac7c8

File tree

7 files changed

+58
-55
lines changed

7 files changed

+58
-55
lines changed

docs/guide/partial-reloads.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,7 @@ On the inverse, you can use the `InertiaRails.always` method to specify that a p
201201
class UsersController < ApplicationController
202202
def index
203203
render inertia: 'Users/Index', props: {
204-
users: InertiaRails.always(User.all),
205-
206-
# Also works with block:
207-
# users: InertiaRails.always { User.all },
208-
209-
# Also works with a lambda:
210-
# users: InertiaRails.always(-> { User.all }),
204+
users: InertiaRails.always { User.all },
211205
}
212206
end
213207
end

lib/inertia_rails/base_prop.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@
33
module InertiaRails
44
# Base class for all props.
55
class BaseProp
6-
def initialize(value = nil, &block)
7-
raise ArgumentError, 'You must provide either a value or a block, not both' if value && block
8-
9-
@value = value
6+
def initialize(&block)
107
@block = block
118
end
129

1310
def call(controller)
14-
value.respond_to?(:call) ? controller.instance_exec(&value) : value
15-
end
16-
17-
def value
18-
@value.nil? ? @block : @value
11+
controller.instance_exec(&@block)
1912
end
2013
end
2114
end

lib/inertia_rails/inertia_rails.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def lazy(value = nil, &block)
1919
LazyProp.new(value, &block)
2020
end
2121

22-
def always(value = nil, &block)
23-
AlwaysProp.new(value, &block)
22+
def always(&block)
23+
AlwaysProp.new(&block)
2424
end
2525
end
2626
end

lib/inertia_rails/lazy_prop.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,19 @@
22

33
module InertiaRails
44
class LazyProp < BaseProp
5+
def initialize(value = nil, &block)
6+
raise ArgumentError, 'You must provide either a value or a block, not both' if value && block
7+
8+
@value = value
9+
@block = block
10+
end
11+
12+
def call(controller)
13+
value.respond_to?(:call) ? controller.instance_exec(&value) : value
14+
end
15+
16+
def value
17+
@value.nil? ? @block : @value
18+
end
519
end
620
end

spec/dummy/app/controllers/inertia_render_test_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def except_props
2020
first: 'first nested param',
2121
second: 'second nested param'
2222
},
23-
always: InertiaRails.always('always prop')
23+
always: InertiaRails.always { 'always prop' }
2424
}
2525
end
2626

@@ -54,7 +54,7 @@ def lazy_props
5454

5555
def always_props
5656
render inertia: 'TestComponent', props: {
57-
always: InertiaRails.always('always prop'),
57+
always: InertiaRails.always { 'always prop' },
5858
regular: 'regular prop',
5959
lazy: InertiaRails.lazy do
6060
'lazy prop'

spec/inertia/lazy_prop_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
RSpec.describe InertiaRails::LazyProp do
22
it_behaves_like 'base prop'
3+
4+
describe '#call' do
5+
subject(:call) { prop.call(controller) }
6+
let(:prop) { described_class.new('value') }
7+
let(:controller) { ApplicationController.new }
8+
9+
it { is_expected.to eq('value') }
10+
11+
context 'with false as value' do
12+
let(:prop) { described_class.new(false) }
13+
14+
it { is_expected.to eq(false) }
15+
end
16+
17+
context 'with nil as value' do
18+
let(:prop) { described_class.new(nil) }
19+
20+
it { is_expected.to eq(nil) }
21+
end
22+
23+
context 'with a callable value' do
24+
let(:prop) { described_class.new(-> { 'callable' }) }
25+
26+
it { is_expected.to eq('callable') }
27+
28+
context 'with dependency on the context of a controller' do
29+
let(:prop) { described_class.new(-> { controller_method }) }
30+
31+
it { is_expected.to eq('controller_method value') }
32+
end
33+
end
34+
end
335
end

spec/support/shared_examples.rb

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,15 @@
11
RSpec.shared_examples 'base prop' do
22
describe '#call' do
33
subject(:call) { prop.call(controller) }
4-
let(:prop) { described_class.new('value') }
4+
let(:prop) { described_class.new { 'block' } }
55
let(:controller) { ApplicationController.new }
66

7-
it { is_expected.to eq('value') }
7+
it { is_expected.to eq('block') }
88

9-
context 'with false as value' do
10-
let(:prop) { described_class.new(false) }
9+
context 'with dependency on the context of a controller' do
10+
let(:prop) { described_class.new { controller_method } }
1111

12-
it { is_expected.to eq(false) }
13-
end
14-
15-
context 'with nil as value' do
16-
let(:prop) { described_class.new(nil) }
17-
18-
it { is_expected.to eq(nil) }
19-
end
20-
21-
context 'with a callable value' do
22-
let(:prop) { described_class.new(-> { 'callable' }) }
23-
24-
it { is_expected.to eq('callable') }
25-
26-
context 'with dependency on the context of a controller' do
27-
let(:prop) { described_class.new(-> { controller_method }) }
28-
29-
it { is_expected.to eq('controller_method value') }
30-
end
31-
end
32-
33-
context 'with a block' do
34-
let(:prop) { described_class.new { 'block' } }
35-
36-
it { is_expected.to eq('block') }
37-
38-
context 'with dependency on the context of a controller' do
39-
let(:prop) { described_class.new { controller_method } }
40-
41-
it { is_expected.to eq('controller_method value') }
42-
end
12+
it { is_expected.to eq('controller_method value') }
4313
end
4414
end
4515
end

0 commit comments

Comments
 (0)