builder.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. class CustomWizard::Builder
  2. def initialize(user, wizard_id)
  3. data = PluginStore.get('custom_wizard', wizard_id)
  4. @custom_wizard = CustomWizard::Wizard.new(data)
  5. @wizard = Wizard.new(user)
  6. @wizard.id = wizard_id
  7. @wizard.save_submissions = data['save_submissions']
  8. end
  9. def self.sorted_handlers
  10. @sorted_handlers ||= []
  11. end
  12. def self.step_handlers
  13. sorted_handlers.map { |h| { wizard_id: h[:wizard_id], block: h[:block] } }
  14. end
  15. def self.add_step_handler(priority = 0, wizard_id, &block)
  16. sorted_handlers << { priority: priority, wizard_id: wizard_id, block: block }
  17. @sorted_handlers.sort_by! { |h| -h[:priority] }
  18. end
  19. def build
  20. @custom_wizard.steps.each do |s|
  21. @wizard.append_step(s['id']) do |step|
  22. step.title = s['title'] if s['title']
  23. step.banner = s['banner'] if s['banner']
  24. s['fields'].each do |f|
  25. field = step.add_field(id: f['id'],
  26. type: f['type'],
  27. label: f['label'],
  28. description: f['description'],
  29. required: f['required'])
  30. if f['type'] == 'dropdown'
  31. f['choices'].each do |c|
  32. field.add_choice(c['id'], label: c['label'])
  33. end
  34. end
  35. end
  36. step.on_update do |updater|
  37. @updater = updater
  38. input = updater.fields
  39. user = @wizard.user
  40. if @wizard.save_submissions
  41. store_key = @wizard.id
  42. submissions = Array.wrap(PluginStore.get("custom_wizard_submissions", store_key))
  43. submission = {}
  44. if submissions.last && submissions.last['completed'] === false
  45. submission = submissions.last
  46. submissions.pop(1)
  47. end
  48. submission['user_id'] = @wizard.user.id
  49. submission['completed'] = updater.step.next.nil?
  50. input.each do |key, value|
  51. submission[key] = value
  52. end
  53. submissions.push(submission)
  54. PluginStore.set('custom_wizard_submissions', store_key, submissions)
  55. end
  56. if s['actions'] && s['actions'].length
  57. s['actions'].each do |a|
  58. if a['type'] === 'create_topic'
  59. creator = PostCreator.new(user,
  60. title: input[a['title']],
  61. raw: input[a['post']],
  62. category: a['category_id'],
  63. skip_validations: true)
  64. post = creator.create
  65. if creator.errors.present?
  66. raise StandardError, creator.errors.full_messages.join(" ")
  67. end
  68. updater.result = { topic_id: post.topic.id }
  69. end
  70. if a['type'] === 'send_message'
  71. creator = PostCreator.new(user,
  72. title: input[a['title']],
  73. raw: input[a['post']],
  74. archetype: Archetype.private_message,
  75. target_usernames: a['username'])
  76. post = creator.create
  77. if creator.errors.present?
  78. raise StandardError, creator.errors.full_messages.join(" ")
  79. end
  80. updater.result = { topic_id: post.topic.id }
  81. end
  82. end
  83. end
  84. CustomWizard::Builder.step_handlers.each do |handler|
  85. if handler[:wizard_id] == @wizard.id
  86. handler[:block].call(self)
  87. end
  88. end
  89. end
  90. end
  91. end
  92. @wizard
  93. end
  94. end