plugin.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # name: discourse-custom-wizard
  2. # about: Create custom wizards
  3. # version: 0.1
  4. # authors: Angus McLeod
  5. register_asset 'stylesheets/custom_wizard.scss'
  6. config = Rails.application.config
  7. config.assets.paths << Rails.root.join("plugins", "discourse-custom-wizard", "assets", "javascripts")
  8. after_initialize do
  9. require_dependency "application_controller"
  10. module ::CustomWizard
  11. class Engine < ::Rails::Engine
  12. engine_name "custom_wizard"
  13. isolate_namespace CustomWizard
  14. end
  15. end
  16. load File.expand_path('../lib/builder.rb', __FILE__)
  17. load File.expand_path('../lib/wizard.rb', __FILE__)
  18. load File.expand_path('../app/controllers/wizard.rb', __FILE__)
  19. load File.expand_path('../app/controllers/steps.rb', __FILE__)
  20. load File.expand_path('../app/controllers/admin.rb', __FILE__)
  21. CustomWizard::Engine.routes.draw do
  22. get ':wizard_id' => 'wizard#index'
  23. get ':wizard_id/steps' => 'steps#index'
  24. get ':wizard_id/steps/:step_id' => 'wizard#index'
  25. put ':wizard_id/steps/:step_id' => 'steps#update'
  26. end
  27. require_dependency 'admin_constraint'
  28. Discourse::Application.routes.append do
  29. namespace :wizard do
  30. mount ::CustomWizard::Engine, at: 'custom'
  31. end
  32. scope module: 'custom_wizard', constraints: AdminConstraint.new do
  33. get 'admin/wizards' => 'admin#index'
  34. get 'admin/wizards/field-types' => 'admin#field_types'
  35. get 'admin/wizards/custom' => 'admin#index'
  36. get 'admin/wizards/custom/new' => 'admin#index'
  37. get 'admin/wizards/custom/all' => 'admin#custom_wizards'
  38. get 'admin/wizards/custom/:wizard_id' => 'admin#find_wizard'
  39. put 'admin/wizards/custom/save' => 'admin#save'
  40. delete 'admin/wizards/custom/remove' => 'admin#remove'
  41. get 'admin/wizards/submissions' => 'admin#index'
  42. get 'admin/wizards/submissions/all' => 'admin#submissions'
  43. get 'admin/wizards/submissions/:wizard_id' => 'admin#find_submissions'
  44. end
  45. end
  46. class CustomWizard::FieldTypes
  47. def self.all
  48. @types ||= ['dropdown', 'image', 'radio', 'text', 'textarea']
  49. end
  50. def self.add(type)
  51. all.push(*type)
  52. end
  53. end
  54. class ::Wizard
  55. attr_accessor :id, :background, :save_submissions
  56. end
  57. class ::Wizard::Step
  58. attr_accessor :title, :description, :translation_key
  59. end
  60. class ::Wizard::StepUpdater
  61. attr_accessor :result, :step
  62. end
  63. require_dependency 'wizard/field'
  64. Wizard::Field.class_eval do
  65. attr_reader :label, :description, :translation_key
  66. def initialize(attrs)
  67. attrs = attrs || {}
  68. @id = attrs[:id]
  69. @type = attrs[:type]
  70. @required = !!attrs[:required]
  71. @label = attrs[:label]
  72. @description = attrs[:description]
  73. @translation_key = attrs[:translation_key]
  74. @value = attrs[:value]
  75. @choices = []
  76. end
  77. end
  78. ::WizardSerializer.class_eval do
  79. attributes :id, :background
  80. def id
  81. object.id
  82. end
  83. def background
  84. object.background
  85. end
  86. def include_start?
  87. object.start
  88. end
  89. end
  90. ::WizardStepSerializer.class_eval do
  91. def title
  92. return object.title if object.title
  93. I18n.t("#{object.translation_key || i18n_key}.title", default: '')
  94. end
  95. def description
  96. return object.description if object.description
  97. I18n.t("#{object.translation_key || i18n_key}.description", default: '')
  98. end
  99. end
  100. ::WizardFieldSerializer.class_eval do
  101. def label
  102. return object.label if object.label
  103. I18n.t("#{object.translation_key || i18n_key}.label", default: '')
  104. end
  105. def description
  106. return object.description if object.description
  107. I18n.t("#{object.translation_key || i18n_key}.description", default: '')
  108. end
  109. end
  110. end