Skip to content

Commit 3f5a7fa

Browse files
committed
Adopt 'undef context='
1 parent 8d8105a commit 3f5a7fa

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

lib/liquid/self_drop.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def key?(key)
3131
@self_context.variable_defined?(key)
3232
end
3333

34-
undef context
35-
3634
def to_liquid
3735
self
3836
end
37+
38+
undef context=
3939
end
4040
end

lib/liquid/template.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ def render(*args)
151151

152152
c
153153
when Liquid::Drop
154-
drop = args.shift
155-
drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, {}, @environment)
154+
drop = args.shift
155+
c = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, {}, @environment)
156+
drop.context = c if drop.respond_to?(:context=)
157+
c
156158
when Hash
157159
Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, {}, @environment)
158160
when nil

test/integration/self_drop_context_test.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ def test_self_drop_reflects_variables_assigned_after_creation
6969
assert_template_result('42', source)
7070
end
7171

72-
def test_self_drop_context_writer_is_a_noop
72+
def test_self_drop_context_setter_is_undefined
7373
context = Context.new
7474
drop = SelfDrop.new(context)
75-
assert(drop.respond_to?(:context=))
76-
drop.context = Context.new
77-
assert_nil(drop.instance_variable_get(:@context))
75+
refute(drop.respond_to?(:context=))
76+
77+
assert_template_result('42', '{{ self.x }}', { 'x' => 42 })
7878
end
7979

8080
def test_self_drop_with_strict_variables_does_not_raise_for_defined_var
@@ -88,4 +88,11 @@ def test_self_drop_with_strict_variables_returns_nil_for_undefined_var
8888
result = t.render({}, strict_variables: true)
8989
assert_equal('', result)
9090
end
91+
92+
def test_self_drop_can_be_passed_as_bare_drop_to_render
93+
t = Template.parse('{{ self.x }}')
94+
drop = SelfDrop.new(Context.new({ 'x' => 42 }))
95+
result = t.render(drop)
96+
assert_equal('42', result)
97+
end
9198
end

0 commit comments

Comments
 (0)