Skip to content

Commit ccd05e8

Browse files
tobiclaude
andcommitted
Make blank/empty comparisons invariant to ActiveSupport
- Implement liquid_blank? and liquid_empty? methods in Condition to emulate ActiveSupport's behavior when it's not loaded - This ensures templates like `{% if x == blank %}` work identically whether ActiveSupport is loaded or not - Update liquid-spec adapters for new API (ctx parameter) - Add rake spec task for running liquid-spec matrix 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a4a29f3 commit ccd05e8

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ end
3333

3434
group :spec do
3535
# Using feature branch until https://github.com/Shopify/liquid-spec/pull/97 is merged
36-
gem 'liquid-spec', github: 'Shopify/liquid-spec', branch: 'add-per-spec-required-features'
36+
gem 'liquid-spec', github: 'Shopify/liquid-spec', branch: 'main'
3737
gem 'activesupport', require: false
3838
end

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,9 @@ end
148148
task :console do
149149
exec 'irb -I lib -r liquid'
150150
end
151+
152+
desc('run liquid-spec suite across all adapters')
153+
task :spec do
154+
adapters = Dir['./spec/*.rb'].join(',')
155+
sh "bundle exec liquid-spec matrix --adapters=#{adapters} --reference=ruby_liquid"
156+
end

lib/liquid/condition.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,30 +126,34 @@ def equal_variables(left, right)
126126
def call_method_literal(literal, value)
127127
method_name = literal.method_name
128128

129-
# If the object responds to the method, use it
129+
# If the object responds to the method (e.g., ActiveSupport is loaded), use it
130130
if value.respond_to?(method_name)
131-
return value.send(method_name)
132-
end
133-
134-
# Implement blank?/empty? for common types that don't have it
135-
# (ActiveSupport adds these, but Liquid should work without it)
136-
case method_name
137-
when :blank?
138-
liquid_blank?(value)
139-
when :empty?
140-
liquid_empty?(value)
131+
value.send(method_name)
132+
else
133+
# Emulate ActiveSupport's blank?/empty? to make Liquid invariant
134+
# to whether ActiveSupport is loaded or not
135+
case method_name
136+
when :blank?
137+
liquid_blank?(value)
138+
when :empty?
139+
liquid_empty?(value)
140+
else
141+
false
142+
end
141143
end
142144
end
143145

144146
# Implement blank? semantics matching ActiveSupport
147+
# blank? returns true for nil, false, empty strings, whitespace-only strings,
148+
# empty arrays, and empty hashes
145149
def liquid_blank?(value)
146150
case value
147151
when NilClass, FalseClass
148152
true
149153
when TrueClass, Numeric
150154
false
151155
when String
152-
# Blank if empty or whitespace only
156+
# Blank if empty or whitespace only (matches ActiveSupport)
153157
value.empty? || value.match?(/\A\s*\z/)
154158
when Array, Hash
155159
value.empty?
@@ -160,7 +164,7 @@ def liquid_blank?(value)
160164
end
161165

162166
# Implement empty? semantics
163-
# Note: nil is NOT empty (but IS blank). empty? checks if a collection has zero elements.
167+
# Note: nil is NOT empty. empty? checks if a collection has zero elements.
164168
def liquid_empty?(value)
165169
case value
166170
when String, Array, Hash

spec/ruby_liquid.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
end
1414

1515
# Compile a template string into a Liquid::Template
16-
LiquidSpec.compile do |source, options|
16+
LiquidSpec.compile do |ctx, source, options|
1717
Liquid::Template.parse(source, **options)
1818
end
1919

2020
# Render a compiled template with the given context
21+
# @param ctx [Hash] adapter context (unused)
2122
# @param template [Liquid::Template] compiled template
2223
# @param assigns [Hash] environment variables
2324
# @param options [Hash] :registers, :strict_errors, :exception_renderer
24-
LiquidSpec.render do |template, assigns, options|
25+
LiquidSpec.render do |ctx, template, assigns, options|
2526
registers = Liquid::Registers.new(options[:registers] || {})
2627

2728
context = Liquid::Context.build(

spec/ruby_liquid_with_active_support.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
end
1515

1616
# Compile a template string into a Liquid::Template
17-
LiquidSpec.compile do |source, options|
17+
LiquidSpec.compile do |ctx, source, options|
1818
Liquid::Template.parse(source, **options)
1919
end
2020

2121
# Render a compiled template with the given context
22+
# @param ctx [Hash] adapter context (unused)
2223
# @param template [Liquid::Template] compiled template
2324
# @param assigns [Hash] environment variables
2425
# @param options [Hash] :registers, :strict_errors, :exception_renderer
25-
LiquidSpec.render do |template, assigns, options|
26+
LiquidSpec.render do |ctx, template, assigns, options|
2627
registers = Liquid::Registers.new(options[:registers] || {})
2728

2829
context = Liquid::Context.build(

0 commit comments

Comments
 (0)