mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
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 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
a4a29f3e08
commit
ccd05e869c
@@ -33,6 +33,6 @@ end
|
|||||||
|
|
||||||
group :spec do
|
group :spec do
|
||||||
# Using feature branch until https://github.com/Shopify/liquid-spec/pull/97 is merged
|
# Using feature branch until https://github.com/Shopify/liquid-spec/pull/97 is merged
|
||||||
gem 'liquid-spec', github: 'Shopify/liquid-spec', branch: 'add-per-spec-required-features'
|
gem 'liquid-spec', github: 'Shopify/liquid-spec', branch: 'main'
|
||||||
gem 'activesupport', require: false
|
gem 'activesupport', require: false
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -148,3 +148,9 @@ end
|
|||||||
task :console do
|
task :console do
|
||||||
exec 'irb -I lib -r liquid'
|
exec 'irb -I lib -r liquid'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
desc('run liquid-spec suite across all adapters')
|
||||||
|
task :spec do
|
||||||
|
adapters = Dir['./spec/*.rb'].join(',')
|
||||||
|
sh "bundle exec liquid-spec matrix --adapters=#{adapters} --reference=ruby_liquid"
|
||||||
|
end
|
||||||
|
|||||||
+17
-13
@@ -126,22 +126,26 @@ module Liquid
|
|||||||
def call_method_literal(literal, value)
|
def call_method_literal(literal, value)
|
||||||
method_name = literal.method_name
|
method_name = literal.method_name
|
||||||
|
|
||||||
# If the object responds to the method, use it
|
# If the object responds to the method (e.g., ActiveSupport is loaded), use it
|
||||||
if value.respond_to?(method_name)
|
if value.respond_to?(method_name)
|
||||||
return value.send(method_name)
|
value.send(method_name)
|
||||||
end
|
else
|
||||||
|
# Emulate ActiveSupport's blank?/empty? to make Liquid invariant
|
||||||
# Implement blank?/empty? for common types that don't have it
|
# to whether ActiveSupport is loaded or not
|
||||||
# (ActiveSupport adds these, but Liquid should work without it)
|
case method_name
|
||||||
case method_name
|
when :blank?
|
||||||
when :blank?
|
liquid_blank?(value)
|
||||||
liquid_blank?(value)
|
when :empty?
|
||||||
when :empty?
|
liquid_empty?(value)
|
||||||
liquid_empty?(value)
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Implement blank? semantics matching ActiveSupport
|
# Implement blank? semantics matching ActiveSupport
|
||||||
|
# blank? returns true for nil, false, empty strings, whitespace-only strings,
|
||||||
|
# empty arrays, and empty hashes
|
||||||
def liquid_blank?(value)
|
def liquid_blank?(value)
|
||||||
case value
|
case value
|
||||||
when NilClass, FalseClass
|
when NilClass, FalseClass
|
||||||
@@ -149,7 +153,7 @@ module Liquid
|
|||||||
when TrueClass, Numeric
|
when TrueClass, Numeric
|
||||||
false
|
false
|
||||||
when String
|
when String
|
||||||
# Blank if empty or whitespace only
|
# Blank if empty or whitespace only (matches ActiveSupport)
|
||||||
value.empty? || value.match?(/\A\s*\z/)
|
value.empty? || value.match?(/\A\s*\z/)
|
||||||
when Array, Hash
|
when Array, Hash
|
||||||
value.empty?
|
value.empty?
|
||||||
@@ -160,7 +164,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Implement empty? semantics
|
# Implement empty? semantics
|
||||||
# Note: nil is NOT empty (but IS blank). empty? checks if a collection has zero elements.
|
# Note: nil is NOT empty. empty? checks if a collection has zero elements.
|
||||||
def liquid_empty?(value)
|
def liquid_empty?(value)
|
||||||
case value
|
case value
|
||||||
when String, Array, Hash
|
when String, Array, Hash
|
||||||
|
|||||||
+3
-2
@@ -13,15 +13,16 @@ LiquidSpec.configure do |config|
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Compile a template string into a Liquid::Template
|
# Compile a template string into a Liquid::Template
|
||||||
LiquidSpec.compile do |source, options|
|
LiquidSpec.compile do |ctx, source, options|
|
||||||
Liquid::Template.parse(source, **options)
|
Liquid::Template.parse(source, **options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Render a compiled template with the given context
|
# Render a compiled template with the given context
|
||||||
|
# @param ctx [Hash] adapter context (unused)
|
||||||
# @param template [Liquid::Template] compiled template
|
# @param template [Liquid::Template] compiled template
|
||||||
# @param assigns [Hash] environment variables
|
# @param assigns [Hash] environment variables
|
||||||
# @param options [Hash] :registers, :strict_errors, :exception_renderer
|
# @param options [Hash] :registers, :strict_errors, :exception_renderer
|
||||||
LiquidSpec.render do |template, assigns, options|
|
LiquidSpec.render do |ctx, template, assigns, options|
|
||||||
registers = Liquid::Registers.new(options[:registers] || {})
|
registers = Liquid::Registers.new(options[:registers] || {})
|
||||||
|
|
||||||
context = Liquid::Context.build(
|
context = Liquid::Context.build(
|
||||||
|
|||||||
@@ -14,15 +14,16 @@ LiquidSpec.configure do |config|
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Compile a template string into a Liquid::Template
|
# Compile a template string into a Liquid::Template
|
||||||
LiquidSpec.compile do |source, options|
|
LiquidSpec.compile do |ctx, source, options|
|
||||||
Liquid::Template.parse(source, **options)
|
Liquid::Template.parse(source, **options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Render a compiled template with the given context
|
# Render a compiled template with the given context
|
||||||
|
# @param ctx [Hash] adapter context (unused)
|
||||||
# @param template [Liquid::Template] compiled template
|
# @param template [Liquid::Template] compiled template
|
||||||
# @param assigns [Hash] environment variables
|
# @param assigns [Hash] environment variables
|
||||||
# @param options [Hash] :registers, :strict_errors, :exception_renderer
|
# @param options [Hash] :registers, :strict_errors, :exception_renderer
|
||||||
LiquidSpec.render do |template, assigns, options|
|
LiquidSpec.render do |ctx, template, assigns, options|
|
||||||
registers = Liquid::Registers.new(options[:registers] || {})
|
registers = Liquid::Registers.new(options[:registers] || {})
|
||||||
|
|
||||||
context = Liquid::Context.build(
|
context = Liquid::Context.build(
|
||||||
|
|||||||
Reference in New Issue
Block a user