FIX: Ensure user_email and user_option attributes are handled properly (#119)
Dieser Commit ist enthalten in:
Ursprung
af3e61fe75
Commit
58b3bc13c9
3 geänderte Dateien mit 67 neuen und 36 gelöschten Zeilen
|
@ -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]
|
||||||
|
|
|
@ -229,7 +229,8 @@ describe CustomWizard::Mapper do
|
||||||
).perform).to eq("value 2")
|
).perform).to eq("value 2")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "interpolates user fields" do
|
context "interpolates" do
|
||||||
|
it "user fields" do
|
||||||
expect(CustomWizard::Mapper.new(
|
expect(CustomWizard::Mapper.new(
|
||||||
inputs: inputs['interpolate_user_field'],
|
inputs: inputs['interpolate_user_field'],
|
||||||
data: data,
|
data: data,
|
||||||
|
@ -237,21 +238,32 @@ describe CustomWizard::Mapper do
|
||||||
).perform).to eq("Name: Angus")
|
).perform).to eq("Name: Angus")
|
||||||
end
|
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
|
||||||
|
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(
|
expect(CustomWizard::Mapper.new(
|
||||||
inputs: inputs['interpolate_timestamp'],
|
inputs: inputs['interpolate_timestamp'],
|
||||||
data: data,
|
data: data,
|
||||||
user: user1
|
user: user1
|
||||||
).perform).to eq("Time: #{Time.now.strftime("%B %-d, %Y")}")
|
).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
|
||||||
expect(CustomWizard::Mapper.new(
|
expect(CustomWizard::Mapper.new(
|
||||||
|
|
16
spec/fixtures/mapper/inputs.json
gevendort
16
spec/fixtures/mapper/inputs.json
gevendort
|
@ -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",
|
||||||
|
|
Laden …
In neuem Issue referenzieren