From a146d57f0e77704bbb20e89a31f45ea0b8818fba Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 21 Mar 2023 17:20:58 +0100 Subject: [PATCH] Ensure we're not interpolating an object --- lib/custom_wizard/mapper.rb | 9 ++++- spec/components/custom_wizard/mapper_spec.rb | 39 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index 9d26c82e..41519b07 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -44,7 +44,7 @@ class CustomWizard::Mapper def initialize(params) @inputs = params[:inputs] || {} - @data = params[:data] || {} + @data = params[:data].with_indifferent_access || {} @user = params[:user] @opts = params[:opts] || {} end @@ -267,7 +267,12 @@ class CustomWizard::Mapper return nil if data.nil? k = keys.shift result = data[k] - keys.empty? ? result : self.recurse(result, keys) + + if keys.empty? + result.is_a?(Object) ? "" : result + else + self.recurse(result, keys) + end end def bool(value) diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index f460cc01..510632a9 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -58,6 +58,11 @@ describe CustomWizard::Mapper do "step_1_field_3" => "Value" } } + let(:template_params_object) { + { + "step_1_field_1": get_wizard_fixture("field/upload") + } + } def create_template_mapper(data, user) CustomWizard::Mapper.new( @@ -448,6 +453,40 @@ describe CustomWizard::Mapper do expect(result).to eq(template) end + it "handles correct object variable references" do + template = <<-LIQUID.strip + {%- if "w{step_1_field_1.id}" == "step_2_field_7" -%} + Correct + {%- else -%} + Incorrect + {%-endif-%} + LIQUID + mapper = create_template_mapper(template_params_object, user1) + result = mapper.interpolate( + template.dup, + template: true, + wizard: true + ) + expect(result).to eq("Correct") + end + + it "handles incorrect object variable references" do + template = <<-LIQUID.strip + {%- if "w{step_1_field_1}" == "step_2_field_7" -%} + Correct + {%- else -%} + Incorrect + {%-endif-%} + LIQUID + mapper = create_template_mapper(template_params_object, user1) + result = mapper.interpolate( + template.dup, + template: true, + wizard: true + ) + expect(result).to eq("Incorrect") + end + context "custom filter: 'first_non_empty'" do it "gives first non empty element from list" do template = <<-LIQUID.strip