From a146d57f0e77704bbb20e89a31f45ea0b8818fba Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 21 Mar 2023 17:20:58 +0100 Subject: [PATCH 1/2] 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 From 076e1f4966ece5e36d7844cc9748bad89feb75fb Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 21 Mar 2023 17:22:15 +0100 Subject: [PATCH 2/2] Bump version --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 3ba2d9c2..c9cb9201 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.2.13 +# version: 2.2.14 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech