From 58b3bc13c90328f1c1128b873d100c591ea1015b Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 15 Jun 2021 16:18:17 +1000 Subject: [PATCH] FIX: Ensure user_email and user_option attributes are handled properly (#119) --- lib/custom_wizard/mapper.rb | 35 +++++++------ spec/components/custom_wizard/mapper_spec.rb | 52 ++++++++++++-------- spec/fixtures/mapper/inputs.json | 16 ++++++ 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index c1187b0f..0c3543cf 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -5,20 +5,27 @@ class CustomWizard::Mapper USER_FIELDS = [ 'name', 'username', - 'email', 'date_of_birth', 'title', 'locale', 'trust_level', + 'email' + ] + + USER_OPTION_FIELDS = [ 'email_level', 'email_messages_level', 'email_digests' ] - PROFILE_FIELDS = ['location', 'website', 'bio_raw'] + PROFILE_FIELDS = [ + 'location', + 'website', + 'bio_raw' + ] def self.user_fields - USER_FIELDS + PROFILE_FIELDS + USER_FIELDS + USER_OPTION_FIELDS + PROFILE_FIELDS end OPERATORS = { @@ -197,11 +204,15 @@ class CustomWizard::Mapper def map_user_field(value) if value.include?(User::USER_FIELD_PREFIX) - UserCustomField.where(user_id: user.id, name: value).pluck(:value).first + user.custom_fields[value] elsif PROFILE_FIELDS.include?(value) - UserProfile.find_by(user_id: user.id).send(value) + user.user_profile.send(value) elsif USER_FIELDS.include?(value) - User.find(user.id).send(value) + user.send(value) + elsif USER_OPTION_FIELDS.include?(value) + user.user_option.send(value) + else + nil end end @@ -217,19 +228,11 @@ class CustomWizard::Mapper return string if string.blank? if opts[:user] - string.gsub!(/u\{(.*?)\}/) do |match| - result = '' - result = user.send($1) if USER_FIELDS.include?($1) - result = user.user_profile.send($1) if PROFILE_FIELDS.include?($1) - result - end + string.gsub!(/u\{(.*?)\}/) { |match| map_user_field($1) || '' } end if opts[:wizard] - string.gsub!(/w\{(.*?)\}/) do |match| - value = recurse(data, [*$1.split('.')]) - value.present? ? value : '' - end + string.gsub!(/w\{(.*?)\}/) { |match| recurse(data, [*$1.split('.')]) || '' } end if opts[:value] diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index 434f0001..ed66d7c1 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -229,28 +229,40 @@ describe CustomWizard::Mapper do ).perform).to eq("value 2") end - it "interpolates user fields" do - expect(CustomWizard::Mapper.new( - inputs: inputs['interpolate_user_field'], - data: data, - user: user1 - ).perform).to eq("Name: Angus") - end + context "interpolates" do + it "user fields" do + expect(CustomWizard::Mapper.new( + inputs: inputs['interpolate_user_field'], + data: data, + user: user1 + ).perform).to eq("Name: Angus") + end - it "interpolates wizard fields" do - expect(CustomWizard::Mapper.new( - inputs: inputs['interpolate_wizard_field'], - data: data, - user: user1 - ).perform).to eq("Input 1: value 1") - end + it "user emails" do + expect(CustomWizard::Mapper.new( + inputs: inputs['interpolate_user_email'], + data: data, + user: user1 + ).perform).to eq("Email: angus@email.com") + end - it "interpolates date" do - expect(CustomWizard::Mapper.new( - inputs: inputs['interpolate_timestamp'], - data: data, - user: user1 - ).perform).to eq("Time: #{Time.now.strftime("%B %-d, %Y")}") + it "user options" do + user1.user_option.update_columns(email_level: UserOption.email_level_types[:never]) + + expect(CustomWizard::Mapper.new( + inputs: inputs['interpolate_user_option'], + data: data, + user: user1 + ).perform).to eq("Email Level: #{UserOption.email_level_types[:never]}") + end + + it "date" do + expect(CustomWizard::Mapper.new( + inputs: inputs['interpolate_timestamp'], + data: data, + user: user1 + ).perform).to eq("Time: #{Time.now.strftime("%B %-d, %Y")}") + end end it "handles greater than pairs" do diff --git a/spec/fixtures/mapper/inputs.json b/spec/fixtures/mapper/inputs.json index f7d98903..443f186b 100644 --- a/spec/fixtures/mapper/inputs.json +++ b/spec/fixtures/mapper/inputs.json @@ -57,6 +57,22 @@ "output": "Name: u{name}" } ], + "interpolate_user_email": [ + { + "type": "assignment", + "output_type": "text", + "output_connector": "set", + "output": "Email: u{email}" + } + ], + "interpolate_user_option": [ + { + "type": "assignment", + "output_type": "text", + "output_connector": "set", + "output": "Email Level: u{email_level}" + } + ], "interpolate_wizard_field": [ { "type": "assignment",