0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 18:50:27 +01:00

FIX: Ensure user_email and user_option attributes are handled properly (#119)

Dieser Commit ist enthalten in:
Angus McLeod 2021-06-15 16:18:17 +10:00 committet von GitHub
Ursprung af3e61fe75
Commit 58b3bc13c9
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
3 geänderte Dateien mit 67 neuen und 36 gelöschten Zeilen

Datei anzeigen

@ -5,20 +5,27 @@ class CustomWizard::Mapper
USER_FIELDS = [ USER_FIELDS = [
'name', 'name',
'username', 'username',
'email',
'date_of_birth', 'date_of_birth',
'title', 'title',
'locale', 'locale',
'trust_level', 'trust_level',
'email'
]
USER_OPTION_FIELDS = [
'email_level', 'email_level',
'email_messages_level', 'email_messages_level',
'email_digests' 'email_digests'
] ]
PROFILE_FIELDS = ['location', 'website', 'bio_raw'] PROFILE_FIELDS = [
'location',
'website',
'bio_raw'
]
def self.user_fields def self.user_fields
USER_FIELDS + PROFILE_FIELDS USER_FIELDS + USER_OPTION_FIELDS + PROFILE_FIELDS
end end
OPERATORS = { OPERATORS = {
@ -197,11 +204,15 @@ class CustomWizard::Mapper
def map_user_field(value) def map_user_field(value)
if value.include?(User::USER_FIELD_PREFIX) 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) elsif PROFILE_FIELDS.include?(value)
UserProfile.find_by(user_id: user.id).send(value) user.user_profile.send(value)
elsif USER_FIELDS.include?(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
end end
@ -217,19 +228,11 @@ class CustomWizard::Mapper
return string if string.blank? return string if string.blank?
if opts[:user] if opts[:user]
string.gsub!(/u\{(.*?)\}/) do |match| string.gsub!(/u\{(.*?)\}/) { |match| map_user_field($1) || '' }
result = ''
result = user.send($1) if USER_FIELDS.include?($1)
result = user.user_profile.send($1) if PROFILE_FIELDS.include?($1)
result
end
end end
if opts[:wizard] if opts[:wizard]
string.gsub!(/w\{(.*?)\}/) do |match| string.gsub!(/w\{(.*?)\}/) { |match| recurse(data, [*$1.split('.')]) || '' }
value = recurse(data, [*$1.split('.')])
value.present? ? value : ''
end
end end
if opts[:value] if opts[:value]

Datei anzeigen

@ -229,28 +229,40 @@ describe CustomWizard::Mapper do
).perform).to eq("value 2") ).perform).to eq("value 2")
end end
it "interpolates user fields" do context "interpolates" do
expect(CustomWizard::Mapper.new( it "user fields" do
inputs: inputs['interpolate_user_field'], expect(CustomWizard::Mapper.new(
data: data, inputs: inputs['interpolate_user_field'],
user: user1 data: data,
).perform).to eq("Name: Angus") user: user1
end ).perform).to eq("Name: Angus")
end
it "interpolates wizard fields" do it "user emails" do
expect(CustomWizard::Mapper.new( expect(CustomWizard::Mapper.new(
inputs: inputs['interpolate_wizard_field'], inputs: inputs['interpolate_user_email'],
data: data, data: data,
user: user1 user: user1
).perform).to eq("Input 1: value 1") ).perform).to eq("Email: angus@email.com")
end end
it "interpolates date" do it "user options" do
expect(CustomWizard::Mapper.new( user1.user_option.update_columns(email_level: UserOption.email_level_types[:never])
inputs: inputs['interpolate_timestamp'],
data: data, expect(CustomWizard::Mapper.new(
user: user1 inputs: inputs['interpolate_user_option'],
).perform).to eq("Time: #{Time.now.strftime("%B %-d, %Y")}") 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 end
it "handles greater than pairs" do it "handles greater than pairs" do

Datei anzeigen

@ -57,6 +57,22 @@
"output": "Name: u{name}" "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": [ "interpolate_wizard_field": [
{ {
"type": "assignment", "type": "assignment",