1
0
Fork 0

Improve builder structure

Dieser Commit ist enthalten in:
Angus McLeod 2018-05-20 11:57:34 +10:00
Ursprung e005eac934
Commit 39b323c5d1

Datei anzeigen

@ -135,13 +135,8 @@ class CustomWizard::Builder
user = @wizard.user user = @wizard.user
if s['fields'] && s['fields'].length if s['fields'] && s['fields'].length
s['fields'].each do |f| s['fields'].each do |field|
value = updater.fields[f['id']] validate_field(field, updater)
min_length = f['min_length']
if min_length && value.is_a?(String) && value.length < min_length.to_i
label = f['label'] || I18n.t("#{f['key']}.label")
updater.errors.add(f['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
end
end end
end end
@ -155,9 +150,7 @@ class CustomWizard::Builder
next if updater.errors.any? next if updater.errors.any?
step_input = updater.fields.to_h data = updater.fields.to_h
data = step_input
final_step = updater.step.next.nil?
## if the wizard has data from the previous steps make that accessible to the actions. ## if the wizard has data from the previous steps make that accessible to the actions.
if @submissions && @submissions.last && !@submissions.last.key?("submitted_at") if @submissions && @submissions.last && !@submissions.last.key?("submitted_at")
@ -165,20 +158,59 @@ class CustomWizard::Builder
data = submission.merge(data) data = submission.merge(data)
end end
if s['actions'] && s['actions'].length if s['actions'] && s['actions'].length && data
s['actions'].each do |a| s['actions'].each do |action|
if a['type'] === 'create_topic' && data self.send(action['type'].to_sym, user, action, data)
end
if a['custom_title']
title = a['custom_title']
else
title = data[a['title']]
end end
if a['post_builder'] final_step = updater.step.next.nil?
post = CustomWizard::Builder.build_post(a['post_template'], user, data)
if @wizard.save_submissions && updater.errors.empty?
save_submissions(data, final_step)
elsif final_step
PluginStore.remove("#{@wizard.id}_submissions", @wizard.user.id)
end
if @wizard.after_time && final_step
@wizard.user.custom_fields.delete('redirect_to_wizard');
@wizard.user.save_custom_fields(true)
end
if updater.errors.empty?
user_redirect = user.custom_fields['redirect_to_wizard']
redirect_to = user_redirect ? "/w/#{user_redirect}" : data['redirect_to']
updater.result = { redirect_to: redirect_to } if redirect_to
end
end
end
end
end
@wizard
end
def validate_field(field, updater)
value = updater.fields[field['id']]
min_length = field['min_length']
if min_length && value.is_a?(String) && value.length < min_length.to_i
label = field['label'] || I18n.t("#{field['key']}.label")
updater.errors.add(field['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
end
end
def create_topic(user, action, data)
if action['custom_title']
title = action['custom_title']
else else
post = data[a['post']] title = data[action['title']]
end
if action['post_builder']
post = CustomWizard::Builder.build_post(action['post_template'], user, data)
else
post = data[action['post']]
end end
if title if title
@ -188,31 +220,32 @@ class CustomWizard::Builder
skip_validations: true skip_validations: true
} }
if a['custom_category_enabled'] && if action['custom_category_enabled'] &&
!a['custom_category_wizard_field'] && !action['custom_category_wizard_field'] &&
a['custom_category_user_field_key'] action['custom_category_user_field_key']
if a['custom_category_user_field_key'].include?('custom_fields')
field = a['custom_category_user_field_key'].split('.').last if action['custom_category_user_field_key'].include?('custom_fields')
field = action['custom_category_user_field_key'].split('.').last
category_id = user.custom_fields[field] category_id = user.custom_fields[field]
else else
category_id = user.send(a['custom_category_user_field_key']) category_id = user.send(action['custom_category_user_field_key'])
end end
else else
category_id = a['category_id'] category_id = action['category_id']
end end
params[:category] = category_id params[:category] = category_id
topic_custom_fields = {} topic_custom_fields = {}
if a['add_fields'] if action['add_fields']
a['add_fields'].each do |f| action['add_fields'].each do |field|
if f['value_custom'] if field['value_custom']
value = f['value_custom'] value = field['value_custom']
else else
value = data[f['value']] value = data[field['value']]
end end
key = f['key'] key = field['key']
if key && key.include?('custom_fields') if key && key.include?('custom_fields')
keyArr = key.split('.') keyArr = key.split('.')
@ -252,13 +285,13 @@ class CustomWizard::Builder
end end
end end
if a['type'] === 'send_message' && data def send_message(user, action, data)
title = data[a['title']] title = data[action['title']]
if a['post_builder'] if action['post_builder']
post = CustomWizard::Builder.build_post(a['post_template'], user, data) post = CustomWizard::Builder.build_post(action['post_template'], user, data)
else else
post = data[a['post']] post = data[action['post']]
end end
if title && post if title && post
@ -266,7 +299,8 @@ class CustomWizard::Builder
title: title, title: title,
raw: post, raw: post,
archetype: Archetype.private_message, archetype: Archetype.private_message,
target_usernames: a['username']) target_usernames: action['username']
)
post = creator.create post = creator.create
@ -278,11 +312,13 @@ class CustomWizard::Builder
end end
end end
if a['type'] === 'update_profile' && a['profile_updates'].length && data def update_profile(user, action, data)
return unless action['profile_updates'].length
attributes = {} attributes = {}
custom_fields = {} custom_fields = {}
a['profile_updates'].each do |pu| action['profile_updates'].each do |pu|
value = pu['value'] value = pu['value']
custom_field = pu['value_custom'] custom_field = pu['value_custom']
key = pu['key'] key = pu['key']
@ -295,9 +331,7 @@ class CustomWizard::Builder
end end
if custom_fields.present? if custom_fields.present?
custom_fields.each do |k, v| custom_fields.each { |k, v| user.custom_fields[k] = v }
user.custom_fields[k] = v
end
user.save_custom_fields(true) user.save_custom_fields(true)
end end
@ -306,16 +340,8 @@ class CustomWizard::Builder
user_updater.update(attributes) user_updater.update(attributes)
end end
end end
end
end
if @wizard.save_submissions && updater.errors.empty?
if step_input
step_input.each do |key, value|
data[key] = value
end
end
def save_submissions(data, final_step)
if final_step if final_step
data['submitted_at'] = Time.now.iso8601 data['submitted_at'] = Time.now.iso8601
end end
@ -326,28 +352,4 @@ class CustomWizard::Builder
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions) PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
end end
end end
# Ensure there is no submission left over after the user has completed a wizard with save_submissions off
if !@wizard.save_submissions && final_step
PluginStore.remove("#{@wizard.id}_submissions", @wizard.user.id)
end
if @wizard.after_time && final_step
@wizard.user.custom_fields.delete('redirect_to_wizard');
@wizard.user.save_custom_fields(true)
end
if updater.errors.empty?
# If the user will be redirected to a new wizard send them there straight away
user_redirect = user.custom_fields['redirect_to_wizard']
redirect_to = user_redirect ? "/w/#{user_redirect}" : data['redirect_to']
updater.result = { redirect_to: redirect_to } if redirect_to
end
end
end
end
end
@wizard
end
end end