plugin.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/field.rb', __FILE__)
  18. load File.expand_path('../lib/wizard.rb', __FILE__)
  19. load File.expand_path('../app/controllers/wizard.rb', __FILE__)
  20. load File.expand_path('../app/controllers/steps.rb', __FILE__)
  21. load File.expand_path('../app/controllers/admin.rb', __FILE__)
  22. CustomWizard::Engine.routes.draw do
  23. get ':wizard_id' => 'wizard#index'
  24. get ':wizard_id/steps' => 'steps#index'
  25. get ':wizard_id/steps/:step_id' => 'wizard#index'
  26. put ':wizard_id/steps/:step_id' => 'steps#update'
  27. end
  28. require_dependency 'admin_constraint'
  29. Discourse::Application.routes.append do
  30. namespace :wizard do
  31. mount ::CustomWizard::Engine, at: 'custom'
  32. end
  33. scope module: 'custom_wizard', constraints: AdminConstraint.new do
  34. get 'admin/wizards' => 'admin#index'
  35. get 'admin/wizards/field-types' => 'admin#field_types'
  36. get 'admin/wizards/custom' => 'admin#index'
  37. get 'admin/wizards/custom/new' => 'admin#index'
  38. get 'admin/wizards/custom/all' => 'admin#custom_wizards'
  39. get 'admin/wizards/custom/:wizard_id' => 'admin#find_wizard'
  40. put 'admin/wizards/custom/save' => 'admin#save'
  41. delete 'admin/wizards/custom/remove' => 'admin#remove'
  42. get 'admin/wizards/submissions' => 'admin#index'
  43. get 'admin/wizards/submissions/all' => 'admin#submissions'
  44. get 'admin/wizards/submissions/:wizard_id' => 'admin#find_submissions'
  45. end
  46. end
  47. class ::Wizard
  48. attr_accessor :id, :background, :save_submissions
  49. end
  50. class ::Wizard::Step
  51. attr_accessor :title, :description, :translation_key
  52. end
  53. class ::Wizard::StepUpdater
  54. attr_accessor :result, :step
  55. end
  56. require_dependency 'wizard/field'
  57. Wizard::Field.class_eval do
  58. attr_reader :label, :description, :translation_key
  59. def initialize(attrs)
  60. attrs = attrs || {}
  61. @id = attrs[:id]
  62. @type = attrs[:type]
  63. @required = !!attrs[:required]
  64. @label = attrs[:label]
  65. @description = attrs[:description]
  66. @translation_key = attrs[:translation_key]
  67. @value = attrs[:value]
  68. @choices = []
  69. end
  70. end
  71. ::WizardSerializer.class_eval do
  72. attributes :id, :background
  73. def id
  74. object.id
  75. end
  76. def background
  77. object.background
  78. end
  79. def include_start?
  80. object.start
  81. end
  82. end
  83. ::WizardStepSerializer.class_eval do
  84. def title
  85. return object.title if object.title
  86. I18n.t("#{object.translation_key || i18n_key}.title", default: '')
  87. end
  88. def description
  89. return object.description if object.description
  90. I18n.t("#{object.translation_key || i18n_key}.description", default: '')
  91. end
  92. end
  93. ::WizardFieldSerializer.class_eval do
  94. def label
  95. return object.label if object.label
  96. I18n.t("#{object.translation_key || i18n_key}.label", default: '')
  97. end
  98. def description
  99. return object.description if object.description
  100. I18n.t("#{object.translation_key || i18n_key}.description", default: '')
  101. end
  102. end
  103. end