Skip to content

Commit

Permalink
Update DeferProp signature to simplify group usage with blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Oct 21, 2024
1 parent 46f045e commit 4a41d87
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 13 deletions.
14 changes: 6 additions & 8 deletions lib/inertia_rails/base_prop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ def call
to_proc.call
end

private

def to_proc
# This is called by controller.instance_exec, which changes self to the
# controller instance. That makes the instance variables unavailable to the
# proc via closure. Copying the instance variables to local variables before
# the proc is returned keeps them in scope for the returned proc.
value = @value
block = @block
if value.respond_to?(:call)
value
elsif value
Proc.new { value }
else
block
end
return value if value.respond_to?(:call)
return Proc.new { value } if value

@block
end
end
end
2 changes: 1 addition & 1 deletion lib/inertia_rails/defer_prop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class DeferProp < IgnoreFirstLoadProp

attr_reader :group

def initialize(value = nil, group = nil, &block)
def initialize(value = nil, group: nil, &block)
super(value, &block)

@group = group || DEFAULT_GROUP
Expand Down
4 changes: 2 additions & 2 deletions lib/inertia_rails/inertia_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def lazy(value = nil, &block)
OptionalProp.new(value, &block)
end

def defer(value = nil, group = nil, &block)
DeferProp.new(value, group, &block)
def defer(value = nil, group: nil, &block)
DeferProp.new(value, group:, &block)
end

def merge(value = nil, &block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def merge_props
def deferred_props
render inertia: 'TestComponent', props: {
name: 'Brian',
sport: InertiaRails.defer('basketball', 'other'),
sport: InertiaRails.defer('basketball', group: 'other'),
level: InertiaRails.defer do
'worse than he believes'
end,
Expand Down
20 changes: 20 additions & 0 deletions spec/inertia/always_prop_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
RSpec.describe InertiaRails::AlwaysProp do
describe '#call' do
subject(:call) { prop.call }
let(:prop) { described_class.new('value') }

it { is_expected.to eq('value') }

context 'with a callable value' do
let(:prop) { described_class.new(-> { 'callable' }) }

it { is_expected.to eq('callable') }
end

context 'with a block' do
let(:prop) { described_class.new { 'block' } }

it { is_expected.to eq('block') }
end
end
end
55 changes: 55 additions & 0 deletions spec/inertia/defer_prop_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
RSpec.describe InertiaRails::DeferProp do
describe '#call' do
subject(:call) { prop.call }
let(:prop) { described_class.new('value') }

it { is_expected.to eq('value') }

it 'returns the default group' do
expect(prop.group).to eq('default')
end

context "with group" do
let(:prop) { described_class.new('value', group: 'custom') }

it 'returns the group' do
expect(prop.group).to eq('custom')
end
end

context 'with a callable value' do
let(:prop) { described_class.new(-> { 'callable' }) }

it { is_expected.to eq('callable') }

context "with group" do
let(:prop) { described_class.new(-> { 'callable' }, group: 'custom') }

it 'returns the group' do
expect(prop.group).to eq('custom')
end
end
end

context 'with a block' do
let(:prop) { described_class.new { 'block' } }

it { is_expected.to eq('block') }

context "with group" do
let(:prop) { described_class.new(group: 'custom') { 'block' } }

it 'returns the group' do
expect(prop.group).to eq('custom')
end
end
end

it 'returns the merge flag' do
expect(prop.merge?).to be_falsey
prop.merge

expect(prop.merge?).to be(true)
end
end
end
24 changes: 24 additions & 0 deletions spec/inertia/merge_prop_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
RSpec.describe InertiaRails::MergeProp do
describe '#call' do
subject(:call) { prop.call }
let(:prop) { described_class.new('value') }

it { is_expected.to eq('value') }

context 'with a callable value' do
let(:prop) { described_class.new(-> { 'callable' }) }

it { is_expected.to eq('callable') }
end

context 'with a block' do
let(:prop) { described_class.new { 'block' } }

it { is_expected.to eq('block') }
end

it 'returns the merge flag' do
expect(prop.merge?).to eq(true)
end
end
end
2 changes: 1 addition & 1 deletion spec/inertia/optional_prop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

context 'with a block' do
it 'returns the result of the block' do
expect(InertiaRails::OptionalProp.new{'thing'}.call).to eq('thing')
expect(InertiaRails::OptionalProp.new {'thing'}.call).to eq('thing')
end
end
end
Expand Down

0 comments on commit 4a41d87

Please sign in to comment.