wizard.rb 3.3 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. if unfinished?
  44. step_id = ::UserHistory.where(
  45. acting_user_id: @user.id,
  46. action: ::UserHistory.actions[:custom_wizard_step],
  47. context: @id,
  48. subject: @steps.map(&:id)
  49. ).order("created_at").last.subject
  50. last_index = @steps.index { |s| s.id == step_id }
  51. @steps[last_index + 1]
  52. else
  53. @first_step
  54. end
  55. end
  56. def create_updater(step_id, fields)
  57. step = @steps.find { |s| s.id == step_id }
  58. wizard = self
  59. CustomWizard::StepUpdater.new(@user, wizard, step, fields)
  60. end
  61. def unfinished?
  62. most_recent = ::UserHistory.where(
  63. acting_user_id: @user.id,
  64. action: ::UserHistory.actions[:custom_wizard_step],
  65. context: @id,
  66. ).distinct.order('updated_at DESC').first
  67. if most_recent
  68. last_finished_step = most_recent.subject
  69. last_step = CustomWizard::Wizard.step_ids(@id).last
  70. last_finished_step != last_step
  71. else
  72. true
  73. end
  74. end
  75. def completed?
  76. steps = CustomWizard::Wizard.step_ids(@id)
  77. history = ::UserHistory.where(
  78. acting_user_id: @user.id,
  79. action: ::UserHistory.actions[:custom_wizard_step],
  80. context: @id
  81. )
  82. if @completed_after
  83. history.where("updated_at > ?", @completed_after)
  84. end
  85. completed = history.distinct.order(:subject).pluck(:subject)
  86. (steps - completed).empty?
  87. end
  88. def self.after_signup
  89. rows = PluginStoreRow.where(plugin_name: 'custom_wizard')
  90. wizards = [*rows].select { |r| r.value['after_signup'] }
  91. if wizards.any?
  92. wizards.first.key
  93. else
  94. false
  95. end
  96. end
  97. def self.steps(wizard_id)
  98. wizard = PluginStore.get('custom_wizard', wizard_id)
  99. wizard ? wizard['steps'] : nil
  100. end
  101. def self.step_ids(wizard_id)
  102. steps = self.steps(wizard_id)
  103. return [] if !steps
  104. steps.map { |s| s['id'] }.flatten.uniq
  105. end
  106. def self.field_ids(wizard_id, step_id)
  107. steps = self.steps(wizard_id)
  108. return [] if !steps
  109. step = steps.select { |s| s['id'] === step_id }.first
  110. if step && fields = step['fields']
  111. fields.map { |f| f['id'] }
  112. else
  113. []
  114. end
  115. end
  116. def self.add_wizard(json)
  117. wizard = ::JSON.parse(json)
  118. PluginStore.set('custom_wizard', wizard["id"], wizard)
  119. end
  120. end