wizard_edits.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. require_dependency 'wizard'
  2. require_dependency 'wizard/field'
  3. require_dependency 'wizard/step'
  4. ::Wizard.class_eval do
  5. def self.user_requires_completion?(user)
  6. wizard_result = self.new(user).requires_completion?
  7. return wizard_result if wizard_result
  8. custom_redirect = false
  9. if user && wizard_id = CustomWizard::Wizard.after_signup
  10. wizard = CustomWizard::Wizard.create(user, wizard_id)
  11. if !wizard.completed? && wizard.permitted?
  12. custom_redirect = true
  13. CustomWizard::Wizard.set_wizard_redirect(user, wizard_id)
  14. end
  15. end
  16. !!custom_redirect
  17. end
  18. end
  19. ::Wizard::Field.class_eval do
  20. attr_reader :label, :description, :image, :key, :min_length
  21. attr_accessor :dropdown_none
  22. def initialize(attrs)
  23. @attrs = attrs || {}
  24. @id = attrs[:id]
  25. @type = attrs[:type]
  26. @required = !!attrs[:required]
  27. @description = attrs[:description]
  28. @image = attrs[:image]
  29. @key = attrs[:key]
  30. @min_length = attrs[:min_length]
  31. @value = attrs[:value]
  32. @choices = []
  33. @dropdown_none = attrs[:dropdown_none]
  34. end
  35. def label
  36. @label ||= PrettyText.cook(@attrs[:label])
  37. end
  38. end
  39. ::Wizard::Choice.class_eval do
  40. def initialize(id, opts)
  41. @id = id
  42. @opts = opts
  43. @data = opts[:data]
  44. @extra_label = opts[:extra_label]
  45. @icon = opts[:icon]
  46. end
  47. def label
  48. @label ||= PrettyText.cook(@opts[:label])
  49. end
  50. end
  51. class ::Wizard::Step
  52. attr_accessor :title, :description, :key
  53. end
  54. ::WizardSerializer.class_eval do
  55. attributes :id, :background, :completed, :required, :min_trust, :permitted
  56. def id
  57. object.id
  58. end
  59. def include_id?
  60. object.respond_to?(:id)
  61. end
  62. def background
  63. object.background
  64. end
  65. def include_background?
  66. object.respond_to?(:background)
  67. end
  68. def completed
  69. object.completed?
  70. end
  71. def include_completed?
  72. object.completed? &&
  73. (!object.respond_to?(:multiple_submissions) || !object.multiple_submissions) &&
  74. !scope.is_admin?
  75. end
  76. def min_trust
  77. object.min_trust
  78. end
  79. def include_min_trust?
  80. object.respond_to?(:min_trust)
  81. end
  82. def permitted
  83. object.permitted?
  84. end
  85. def include_permitted?
  86. object.respond_to?(:permitted?)
  87. end
  88. def include_start?
  89. object.start && include_steps?
  90. end
  91. def include_steps?
  92. !include_completed?
  93. end
  94. def required
  95. object.required
  96. end
  97. def include_required?
  98. object.respond_to?(:required)
  99. end
  100. end
  101. ::WizardStepSerializer.class_eval do
  102. def title
  103. return object.title if object.title
  104. I18n.t("#{object.key || i18n_key}.title", default: '')
  105. end
  106. def description
  107. return object.description if object.description
  108. PrettyText.cook(I18n.t("#{object.key || i18n_key}.description", default: '', base_url: Discourse.base_url))
  109. end
  110. end
  111. ::WizardFieldSerializer.class_eval do
  112. attributes :dropdown_none, :image
  113. def label
  114. return object.label if object.label.present?
  115. I18n.t("#{object.key || i18n_key}.label", default: '')
  116. end
  117. def description
  118. return object.description if object.description.present?
  119. I18n.t("#{object.key || i18n_key}.description", default: '', base_url: Discourse.base_url)
  120. end
  121. def image
  122. object.image
  123. end
  124. def include_image?
  125. object.image.present?
  126. end
  127. def placeholder
  128. I18n.t("#{object.key || i18n_key}.placeholder", default: '')
  129. end
  130. def dropdown_none
  131. object.dropdown_none
  132. end
  133. end