0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-08 11:27:59 +01:00
discourse-custom-wizard/plugin.rb

189 Zeilen
5,2 KiB
Ruby

2017-09-23 04:34:07 +02:00
# name: discourse-custom-wizard
2017-10-05 02:36:46 +02:00
# about: Create custom wizards
2017-09-23 04:34:07 +02:00
# version: 0.1
# authors: Angus McLeod
2017-10-13 15:02:34 +02:00
register_asset 'stylesheets/wizard_custom_admin.scss'
2017-09-23 04:34:07 +02:00
2017-09-25 16:47:40 +02:00
config = Rails.application.config
config.assets.paths << Rails.root.join("plugins", "discourse-custom-wizard", "assets", "javascripts")
2017-10-13 15:02:34 +02:00
config.assets.paths << Rails.root.join("plugins", "discourse-custom-wizard", "assets", "stylesheets", "wizard")
2017-09-25 16:47:40 +02:00
2017-09-23 04:34:07 +02:00
after_initialize do
require_dependency "application_controller"
module ::CustomWizard
class Engine < ::Rails::Engine
engine_name "custom_wizard"
isolate_namespace CustomWizard
end
end
2017-09-25 16:47:40 +02:00
load File.expand_path('../lib/builder.rb', __FILE__)
2017-10-09 07:52:09 +02:00
load File.expand_path('../lib/field.rb', __FILE__)
2017-09-25 16:47:40 +02:00
load File.expand_path('../lib/wizard.rb', __FILE__)
load File.expand_path('../app/controllers/wizard.rb', __FILE__)
load File.expand_path('../app/controllers/steps.rb', __FILE__)
load File.expand_path('../app/controllers/admin.rb', __FILE__)
2017-09-23 04:34:07 +02:00
CustomWizard::Engine.routes.draw do
get ':wizard_id' => 'wizard#index'
get ':wizard_id/steps' => 'steps#index'
get ':wizard_id/steps/:step_id' => 'wizard#index'
put ':wizard_id/steps/:step_id' => 'steps#update'
2017-09-23 04:34:07 +02:00
end
require_dependency 'admin_constraint'
Discourse::Application.routes.append do
2017-10-13 15:02:34 +02:00
mount ::CustomWizard::Engine, at: 'w'
2017-09-23 04:34:07 +02:00
2017-09-25 16:47:40 +02:00
scope module: 'custom_wizard', constraints: AdminConstraint.new do
2017-10-05 02:36:46 +02:00
get 'admin/wizards' => 'admin#index'
get 'admin/wizards/field-types' => 'admin#field_types'
2017-09-25 16:47:40 +02:00
get 'admin/wizards/custom' => 'admin#index'
get 'admin/wizards/custom/new' => 'admin#index'
2017-10-05 02:36:46 +02:00
get 'admin/wizards/custom/all' => 'admin#custom_wizards'
get 'admin/wizards/custom/:wizard_id' => 'admin#find_wizard'
2017-09-25 16:47:40 +02:00
put 'admin/wizards/custom/save' => 'admin#save'
delete 'admin/wizards/custom/remove' => 'admin#remove'
2017-10-05 02:36:46 +02:00
get 'admin/wizards/submissions' => 'admin#index'
get 'admin/wizards/submissions/all' => 'admin#submissions'
get 'admin/wizards/submissions/:wizard_id' => 'admin#find_submissions'
end
end
2017-10-13 15:02:34 +02:00
require_dependency 'wizard'
require_dependency 'wizard/step'
require_dependency 'wizard/step_updater'
require_dependency 'wizard/field'
2017-10-13 15:02:34 +02:00
::Wizard.class_eval do
attr_accessor :id, :background, :save_submissions, :multiple_submissions
def initialize(user, attrs = {})
@steps = []
@user = user
@first_step = nil
@max_topics_to_require_completion = 15
@id = attrs[:id] if attrs[:id]
@save_submissions = attrs[:save_submissions] if attrs[:save_submissions]
@multiple_submissions = attrs[:multiple_submissions] if attrs[:multiple_submissions]
@background = attrs[:background] if attrs[:background]
@custom = attrs[:custom] if attrs[:custom]
end
2017-10-13 15:02:34 +02:00
def completed?
completed_steps?(@steps.map(&:id))
end
def completed_steps?(steps)
steps = [steps].flatten.uniq
completed = UserHistory.where(
acting_user_id: @user.id,
action: UserHistory.actions[:wizard_step]
).where(context: steps)
.distinct.order(:context).pluck(:context)
steps.sort == completed
end
def start
completed = UserHistory.where(
acting_user_id: @user.id,
action: UserHistory.actions[:wizard_step]
).where(context: @steps.map(&:id))
.uniq.pluck(:context)
# First uncompleted step
steps = @custom ? @steps : steps_with_fields
steps.each do |s|
return s unless completed.include?(s.id)
end
@first_step
end
2017-10-05 02:36:46 +02:00
end
2017-10-13 15:02:34 +02:00
::Wizard::Field.class_eval do
attr_reader :label, :description, :key, :min_length
def initialize(attrs)
attrs = attrs || {}
@id = attrs[:id]
@type = attrs[:type]
@required = !!attrs[:required]
@label = attrs[:label]
@description = attrs[:description]
2017-10-13 15:02:34 +02:00
@key = attrs[:key]
@min_length = attrs[:min_length]
@value = attrs[:value]
@choices = []
end
end
2017-10-13 15:02:34 +02:00
class ::Wizard::Step
attr_accessor :title, :description, :key
end
class ::Wizard::StepUpdater
attr_accessor :result, :step
end
2017-10-07 04:27:38 +02:00
::WizardSerializer.class_eval do
2017-10-13 15:02:34 +02:00
attributes :id, :background, :completed
2017-10-07 04:27:38 +02:00
def id
object.id
end
def background
object.background
end
2017-10-13 15:02:34 +02:00
def completed
object.completed?
end
def include_completed?
object.completed? && !object.multiple_submissions && !scope.current_user.admin?
end
2017-10-07 04:27:38 +02:00
def include_start?
2017-10-13 15:02:34 +02:00
object.start && include_steps?
end
def include_steps?
!include_completed?
2017-10-07 04:27:38 +02:00
end
end
::WizardStepSerializer.class_eval do
def title
2017-10-07 04:27:38 +02:00
return object.title if object.title
2017-10-13 15:02:34 +02:00
I18n.t("#{object.key || i18n_key}.title", default: '')
2017-10-07 04:27:38 +02:00
end
def description
return object.description if object.description
2017-10-13 15:02:34 +02:00
I18n.t("#{object.key || i18n_key}.description", default: '')
end
end
::WizardFieldSerializer.class_eval do
def label
2017-10-07 04:27:38 +02:00
return object.label if object.label
2017-10-13 15:02:34 +02:00
I18n.t("#{object.key || i18n_key}.label", default: '')
end
def description
2017-10-07 04:27:38 +02:00
return object.description if object.description
2017-10-13 15:02:34 +02:00
I18n.t("#{object.key || i18n_key}.description", default: '')
end
def placeholder
I18n.t("#{object.key || i18n_key}.placeholder", default: '')
end
end
2017-09-23 04:34:07 +02:00
end