plugin.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # name: discourse-custom-wizard
  2. # about: Allows the admins to create custom user input forms
  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/custom' => 'admin#index'
  34. get 'admin/wizards/custom/new' => 'admin#index'
  35. get 'admin/wizards/custom/all' => 'admin#all'
  36. get 'admin/wizards/custom/:wizard_id' => 'admin#find'
  37. put 'admin/wizards/custom/save' => 'admin#save'
  38. delete 'admin/wizards/custom/remove' => 'admin#remove'
  39. end
  40. end
  41. class ::Wizard
  42. attr_accessor :id
  43. end
  44. class ::Wizard::Step
  45. attr_accessor :title
  46. end
  47. ::Wizard::Field.class_eval do
  48. attr_reader :label, :description
  49. def initialize(attrs)
  50. attrs = attrs || {}
  51. @id = attrs[:id]
  52. @type = attrs[:type]
  53. @required = !!attrs[:required]
  54. @label = attrs[:label]
  55. @description = attrs[:description]
  56. @value = attrs[:value]
  57. @choices = []
  58. end
  59. end
  60. add_to_serializer(:wizard, :id) { object.id }
  61. ::WizardStepSerializer.class_eval do
  62. def title
  63. if object.title
  64. object.title
  65. else
  66. I18n.t("#{i18n_key}.title", default: '')
  67. end
  68. end
  69. end
  70. ::WizardFieldSerializer.class_eval do
  71. def label
  72. puts "LABEL: #{object.label}"
  73. if object.label
  74. object.label
  75. else
  76. I18n.t("#{i18n_key}.label", default: '')
  77. end
  78. end
  79. def description
  80. if object.description
  81. object.description
  82. else
  83. I18n.t("#{i18n_key}.description", default: '')
  84. end
  85. end
  86. end
  87. end