wizard.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. require_dependency 'wizard/step'
  2. require_dependency 'wizard/field'
  3. require_dependency 'wizard/step_updater'
  4. require_dependency 'wizard/builder'
  5. class CustomWizard::Wizard
  6. attr_reader :steps, :user
  7. attr_accessor :id,
  8. :name,
  9. :background,
  10. :save_submissions,
  11. :multiple_submissions,
  12. :after_time,
  13. :after_signup,
  14. :required
  15. def initialize(user, attrs = {})
  16. @steps = []
  17. @user = user
  18. @first_step = nil
  19. attrs.each do |key, value|
  20. setter = "#{key}="
  21. send(setter, value) if respond_to?(setter.to_sym, false)
  22. end
  23. end
  24. def create_step(step_name)
  25. ::Wizard::Step.new(step_name)
  26. end
  27. def append_step(step)
  28. step = create_step(step) if step.is_a?(String)
  29. yield step if block_given?
  30. last_step = @steps.last
  31. @steps << step
  32. # If it's the first step
  33. if @steps.size == 1
  34. @first_step = step
  35. step.index = 0
  36. elsif last_step.present?
  37. last_step.next = step
  38. step.previous = last_step
  39. step.index = last_step.index + 1
  40. end
  41. end
  42. def start
  43. completed = ::UserHistory.where(
  44. acting_user_id: @user.id,
  45. action: ::UserHistory.actions[:custom_wizard_step],
  46. context: @id,
  47. subject: @steps.map(&:id)
  48. ).uniq.pluck(:subject)
  49. @steps.each do |s|
  50. return s unless completed.include?(s.id)
  51. end
  52. @first_step
  53. end
  54. def create_updater(step_id, fields)
  55. step = @steps.find { |s| s.id == step_id }
  56. wizard = self
  57. CustomWizard::StepUpdater.new(@user, wizard, step, fields)
  58. end
  59. def unfinished?
  60. most_recent = ::UserHistory.where(
  61. acting_user_id: @user.id,
  62. action: ::UserHistory.actions[:custom_wizard_step],
  63. context: @id,
  64. ).distinct.order('updated_at DESC').first
  65. if most_recent
  66. last_finished_step = most_recent.subject
  67. last_step = CustomWizard::Wizard.step_ids(@id).last
  68. last_finished_step != last_step
  69. else
  70. true
  71. end
  72. end
  73. def completed?
  74. steps = CustomWizard::Wizard.step_ids(@id)
  75. history = ::UserHistory.where(
  76. acting_user_id: @user.id,
  77. action: ::UserHistory.actions[:custom_wizard_step],
  78. context: @id
  79. )
  80. if @completed_after
  81. history.where("updated_at > ?", @completed_after)
  82. end
  83. completed = history.distinct.order(:subject).pluck(:subject)
  84. (steps - completed).empty?
  85. end
  86. def self.after_signup
  87. rows = PluginStoreRow.where(plugin_name: 'custom_wizard')
  88. wizards = [*rows].select { |r| r.value['after_signup'] }
  89. if wizards.any?
  90. wizards.first.key
  91. else
  92. false
  93. end
  94. end
  95. def self.steps(wizard_id)
  96. wizard = PluginStore.get('custom_wizard', wizard_id)
  97. wizard ? wizard['steps'] : nil
  98. end
  99. def self.step_ids(wizard_id)
  100. steps = self.steps(wizard_id)
  101. return [] if !steps
  102. steps.map { |s| s['id'] }.flatten.uniq
  103. end
  104. def self.field_ids(wizard_id, step_id)
  105. steps = self.steps(wizard_id)
  106. return [] if !steps
  107. step = steps.select { |s| s['id'] === step_id }.first
  108. if step && fields = step['fields']
  109. fields.map { |f| f['id'] }
  110. else
  111. []
  112. end
  113. end
  114. def self.add_wizard(json)
  115. wizard = ::JSON.parse(json)
  116. PluginStore.set('custom_wizard', wizard["id"], wizard)
  117. end
  118. end