if a metafield value is an empty string, then it's considered as nil in a liquid template
did
committed Mar 17, 2016
commit fe0aec753ba253a69bbb4c23923776e5440740e9
Showing 2
changed files with
15 additions
and 2 deletions
locomotive/steam/liquid/drops/metafields.rb b/lib/locomotive/steam/liquid/drops/metafields.rb
+6
-2
| @@ | @@ -22,7 +22,7 @@ module Locomotive |
| def find_value(name) | |
| if field = fields[name] | |
| - | t(values[name], field['localized']) |
| + | safe_value(t(values[name], field['localized'])) |
| else | |
| Locomotive::Common::Logger.warn "[Liquid template] unknown site metafield \"#{name}\" under #{@namespace['name']}" | |
| nil | |
| @@ | @@ -45,7 +45,7 @@ module Locomotive |
| { | |
| 'name' => field['name'], | |
| 'label' => t(field['label']) || field['name'].humanize, | |
| - | 'value' => t(value, localized) |
| + | 'value' => safe_value(t(value, localized)) |
| } | |
| end | |
| end | |
| @@ | @@ -66,6 +66,10 @@ module Locomotive |
| value[key] | |
| end | |
| + | def safe_value(value) |
| + | value.blank? ? nil : value |
| + | end |
| + | |
| end | |
| class Metafields < Base | |
spec/unit/liquid/drops/metafields_spec.rb
+9
-0
| @@ | @@ -54,8 +54,17 @@ describe Locomotive::Steam::Liquid::Drops::Metafields do |
| it { is_expected.to eq '42' } | |
| + | context 'the value exists but is an empty string' do |
| + | |
| + | let(:metafields) { { 'my_namespace' => { 'analytics_id' => { 'default' => '' }, 'street' => { 'en' => '7 Albert Camus Alley', 'fr' => '7 allée Albert Camus' } } } } |
| + | it { is_expected.to eq nil } |
| + | |
| + | end |
| + | |
| end | |
| + | |
| + | |
| context "the value doesn't exist" do | |
| subject { namespace.before_method(:country) } | |