1
0
Fork 0
discourse-custom-wizard-unl.../lib/wizard_edits.rb

127 Zeilen
2,6 KiB
Ruby

2017-11-01 05:21:14 +01:00
require_dependency 'wizard'
2017-10-15 05:58:22 +02:00
require_dependency 'wizard/field'
require_dependency 'wizard/step'
2017-11-01 05:21:14 +01:00
::Wizard.class_eval do
def self.user_requires_completion?(user)
wizard_result = self.new(user).requires_completion?
return wizard_result if wizard_result
custom_redirect = nil
if user && wizard_id = CustomWizard::Wizard.after_signup
custom_redirect = wizard_id.dasherize
if CustomWizard::Wizard.new(user, id: wizard_id).completed?
custom_redirect = nil
end
end
$redis.set('custom_wizard_redirect', custom_redirect)
!!custom_redirect
end
end
2017-10-15 05:58:22 +02:00
::Wizard::Field.class_eval do
attr_reader :label, :description, :key, :min_length
2017-11-09 03:50:48 +01:00
attr_accessor :dropdown_none
2017-10-15 05:58:22 +02:00
def initialize(attrs)
attrs = attrs || {}
@id = attrs[:id]
@type = attrs[:type]
@required = !!attrs[:required]
@label = attrs[:label]
@description = attrs[:description]
@key = attrs[:key]
@min_length = attrs[:min_length]
@value = attrs[:value]
@choices = []
2017-11-09 03:50:48 +01:00
@dropdown_none = attrs[:dropdown_none]
2017-10-15 05:58:22 +02:00
end
end
class ::Wizard::Step
attr_accessor :title, :description, :key
end
::WizardSerializer.class_eval do
2017-11-01 05:21:14 +01:00
attributes :id, :background, :completed, :required
2017-10-15 05:58:22 +02:00
def id
object.id
end
2017-10-17 09:18:53 +02:00
def include_id?
object.respond_to?(:id)
end
2017-10-15 05:58:22 +02:00
def background
object.background
end
2017-10-17 09:18:53 +02:00
def include_background?
object.respond_to?(:background)
end
2017-10-15 05:58:22 +02:00
def completed
object.completed?
end
def include_completed?
2017-11-22 11:35:43 +01:00
object.completed? && (!object.respond_to?(:multiple_submissions) || !object.multiple_submissions)
2017-10-15 05:58:22 +02:00
end
def include_start?
object.start && include_steps?
end
def include_steps?
!include_completed?
end
2017-11-01 05:21:14 +01:00
def required
object.required
end
def include_required?
object.respond_to?(:required)
end
2017-10-15 05:58:22 +02:00
end
::WizardStepSerializer.class_eval do
def title
return object.title if object.title
I18n.t("#{object.key || i18n_key}.title", default: '')
end
def description
return object.description if object.description
2017-11-06 10:06:53 +01:00
I18n.t("#{object.key || i18n_key}.description", default: '', base_url: Discourse.base_url)
2017-10-15 05:58:22 +02:00
end
end
::WizardFieldSerializer.class_eval do
2017-11-09 03:50:48 +01:00
attributes :dropdown_none
2017-10-15 05:58:22 +02:00
def label
return object.label if object.label
I18n.t("#{object.key || i18n_key}.label", default: '')
end
def description
return object.description if object.description
I18n.t("#{object.key || i18n_key}.description", default: '')
end
def placeholder
I18n.t("#{object.key || i18n_key}.placeholder", default: '')
end
2017-11-09 03:50:48 +01:00
def dropdown_none
object.dropdown_none
end
2017-10-15 05:58:22 +02:00
end