From 5db1695694e1cbf12892d5dd67c7773282a669af Mon Sep 17 00:00:00 2001 From: Derrick Reimer Date: Wed, 9 Oct 2013 16:07:32 -0700 Subject: [PATCH 1/2] Add default filter to standard filters --- lib/liquid/standardfilters.rb | 5 +++++ test/liquid/standard_filter_test.rb | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 718a8d923..c938f3a46 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -245,6 +245,11 @@ def modulo(input, operand) apply_operation(input, operand, :%) end + def default(input, default_value = "") + is_blank = input.respond_to?(:empty?) ? input.empty? : !input + is_blank ? default_value : input + end + private def flatten_if_necessary(input) diff --git a/test/liquid/standard_filter_test.rb b/test/liquid/standard_filter_test.rb index fc1a3cb53..91c219d0b 100644 --- a/test/liquid/standard_filter_test.rb +++ b/test/liquid/standard_filter_test.rb @@ -274,6 +274,15 @@ def test_prepend assert_template_result('abc',"{{ a | prepend: b}}",assigns) end + def test_default + assert_equal "foo", @filters.default("foo", "bar") + assert_equal "bar", @filters.default(nil, "bar") + assert_equal "bar", @filters.default("", "bar") + assert_equal "bar", @filters.default(false, "bar") + assert_equal "bar", @filters.default([], "bar") + assert_equal "bar", @filters.default({}, "bar") + end + def test_cannot_access_private_methods assert_template_result('a',"{{ 'a' | to_number }}") end From 6710ef60bc088b7721a6b09b5db722e4653e96da Mon Sep 17 00:00:00 2001 From: Derrick Reimer Date: Thu, 10 Oct 2013 09:15:01 -0700 Subject: [PATCH 2/2] Add default filter to history --- History.md | 1 + 1 file changed, 1 insertion(+) diff --git a/History.md b/History.md index e148af99b..35fde61d3 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,7 @@ ## 3.0.0 / not yet released / branch "master" * ... +* Add default filter to standard filters, see #267 [Derrick Reimer, djreimer] * Add optional strict parsing and warn parsing, see #235 [Tristan Hume, trishume] * Add I18n syntax error translation, see #241 [Simon Hørup Eskildsen, Sirupsen] * Make sort filter work on enumerable drops, see #239 [Florian Weingarten, fw42]