Angus McLeod 7 years ago
parent
commit
3fa2735c63

controllers/admin.rb → app/controllers/admin.rb


+ 22 - 0
app/controllers/steps.rb

@@ -0,0 +1,22 @@
+class StepsController < ApplicationController
+  before_filter :ensure_logged_in
+
+  def update
+    wizard = CustomWizard::Builder.new(current_user, params[:wizard_id]).build
+    updater = wizard.create_updater(params[:id], params[:fields])
+    updater.update
+
+    if updater.success?
+      result = { success: 'OK' }
+      result[:refresh_required] = true if updater.refresh_required?
+      render json: result
+    else
+      errors = []
+      updater.errors.messages.each do |field, msg|
+        errors << { field: field, description: msg.join }
+      end
+      render json: { errors: errors }, status: 422
+    end
+  end
+
+end

+ 7 - 3
controllers/steps.rb

@@ -1,8 +1,12 @@
-class CustomWizard::StepsController < ::ApplicationController
-  def all
+class CustomWizard::WizardController < ::ApplicationController
+  def set_layout
+    File.expand_path('../../views/layouts/custom_wizard.html.erb', __FILE__)
+  end
+
+  def index
     respond_to do |format|
       format.json do
-        wizard = CustomWizard::Builder.new(current_user, params[:wizard_id]).build
+        wizard = CustomWizard::Builder.new(current_user, params[:name]).build
         render_serialized(wizard, WizardSerializer)
       end
       format.html {}

+ 29 - 0
app/views/layouts/custom_wizard.html.erb

@@ -0,0 +1,29 @@
+<html>
+  <head>
+    <%= discourse_stylesheet_link_tag :wizard, theme_key: nil %>
+    <%= preload_script "ember_jquery" %>
+    <%= preload_script "wizard-vendor" %>
+    <%= preload_script "wizard-application" %>
+    <%= preload_script "wizard-custom" %>
+    <%= preload_script "locales/#{I18n.locale}" %>
+    <%= render partial: "common/special_font_face" %>
+    <script src="<%= Discourse.base_uri %>/extra-locales/wizard"></script>
+    <%= csrf_meta_tags %>
+
+    <meta name="discourse-base-uri" content="<%= Discourse.base_uri %>">
+
+    <%= render partial: "layouts/head" %>
+    <title><%= t 'custom_wizard.title' %></title>
+  </head>
+
+  <body class='wizard'>
+    <div id='wizard-main'></div>
+
+    <script>
+      (function() {
+        var wizard = require('wizard/wizard').default.create();
+        wizard.start();
+      })();
+    </script>
+  </body>
+</html>

+ 1 - 1
assets/javascripts/discourse/routes/admin-wizard.js.es6

@@ -9,7 +9,7 @@ export default Discourse.Route.extend({
 
     this.set('new', false);
 
-    const wizard = this.modelFor('admin-wizards-custom').findBy('dasherizedName', params.name );
+    const wizard = this.modelFor('admin-wizards-custom').findBy('dasherizedName', params.name);
 
     if (!wizard) return this.transitionTo('adminWizardsCustom.index');
 

+ 3 - 0
assets/javascripts/wizard-custom.js

@@ -0,0 +1,3 @@
+//= require_tree ./wizard/initializers
+//= require_tree ./wizard/models
+//= require_tree ./wizard/routes

+ 13 - 0
assets/javascripts/wizard/initializers/custom.js.es6

@@ -0,0 +1,13 @@
+import Router from 'wizard/router';
+
+export default {
+  name: 'custom-routes',
+
+  initialize() {
+    Router.map(function() {
+      this.route('custom', { path: '/custom/:name' }, function() {
+        this.route('step', { path: '/steps/:step_id' });
+      });
+    });
+  }
+};

+ 22 - 0
assets/javascripts/wizard/models/custom-wizard.js.es6

@@ -0,0 +1,22 @@
+import Step from 'wizard/models/step';
+import WizardField from 'wizard/models/wizard-field';
+import { ajax } from 'wizard/lib/ajax';
+import computed from 'ember-addons/ember-computed-decorators';
+
+const CustomWizard = Ember.Object.extend({
+  @computed('steps.length')
+  totalSteps: length => length
+});
+
+export function findCustomWizard(name) {
+  return ajax({ url: `/wizard/custom/${name}.json` }).then(response => {
+    const wizard = response.wizard;
+    wizard.steps = wizard.steps.map(step => {
+      const stepObj = Step.create(step);
+      stepObj.fields = stepObj.fields.map(f => WizardField.create(f));
+      return stepObj;
+    });
+
+    return CustomWizard.create(wizard);
+  });
+}

+ 7 - 0
assets/javascripts/wizard/routes/application.js.es6

@@ -0,0 +1,7 @@
+import { findCustomWizard } from '../models/custom-wizard';
+
+export default Ember.Route.extend({
+  model(params) {
+    return findCustomWizard(params.name);
+  }
+});

+ 11 - 0
assets/javascripts/wizard/routes/custom.js.es6

@@ -0,0 +1,11 @@
+import { findCustomWizard } from '../models/custom-wizard';
+
+export default Ember.Route.extend({
+  model(params) {
+    return findCustomWizard(params.name);
+  },
+
+  afterModel(model) {
+    this.replaceWith('step', model.start);
+  }
+});

+ 3 - 0
config/locales/server.en.yml

@@ -0,0 +1,3 @@
+en:
+  wizard:
+    title: "Wizard"

+ 19 - 12
lib/builder.rb

@@ -1,23 +1,30 @@
 class CustomWizard::Builder
-  def initialize(user, wizard_id)
+  def initialize(user, wizard_name)
+    rows = PluginStoreRow.where(plugin_name: 'custom_wizards')
+    return if !rows
+
+    [*rows].each do |r|
+      wizard = CustomWizard::Wizard.new(r.value)
+      @template = wizard if wizard.name.dasherize.downcase == wizard_name
+    end
+
     @wizard = Wizard.new(user)
-    @template = PluginStore.get('custom_wizard', wizard_id)
   end
 
   def build
-    @template.each do |s|
-      @wizard.append_step(s.title) do |step|
+    @template.steps.each do |s|
+      @wizard.append_step(s['title']) do |step|
 
-        step.banner = s.banner if s.banner
+        step.banner = s['banner'] if s['banner']
 
-        s.fields.each do |f|
-          field = step.add_field(id: f.id,
-                                 type: f.type,
-                                 required: f.required,
-                                 value: f.value)
+        s['fields'].each do |f|
+          field = step.add_field(id: f['id'],
+                                 type: f['type'],
+                                 required: f['required'],
+                                 value: f['value'])
 
-          if f.type == 'dropdown'
-            f.choices.each do |c|
+          if f['type'] == 'dropdown'
+            f['choices'].each do |c|
               field.add_choice(c)
             end
           end

+ 23 - 13
plugin.rb

@@ -5,6 +5,9 @@
 
 register_asset 'stylesheets/custom-wizard.scss'
 
+config = Rails.application.config
+config.assets.paths << Rails.root.join("plugins", "discourse-custom-wizard", "assets", "javascripts")
+
 after_initialize do
   require_dependency "application_controller"
   module ::CustomWizard
@@ -14,25 +17,32 @@ after_initialize do
     end
   end
 
+  load File.expand_path('../lib/builder.rb', __FILE__)
+  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__)
+
   CustomWizard::Engine.routes.draw do
-    get 'custom' => 'admin#index'
-    get 'custom/new' => 'admin#index'
-    get 'custom/all' => "admin#all"
-    get 'custom/:id' => "admin#find"
-    put 'custom/save' => "admin#save"
-    delete 'custom/remove' => "admin#remove"
+    get ':name' => 'wizard#index'
+    get ':name/steps' => 'steps#index'
+    get ':name/steps/:id' => 'wizard#index'
+    put ':name/steps/:id' => 'steps#update'
   end
 
   require_dependency 'admin_constraint'
   Discourse::Application.routes.append do
+    namespace :wizard do
+      mount ::CustomWizard::Engine, at: 'custom'
+    end
 
-    namespace :admin, constraints: AdminConstraint.new do
-      mount ::CustomWizard::Engine, at: 'wizards'
+    scope module: 'custom_wizard', constraints: AdminConstraint.new do
+      get 'admin/wizards/custom' => 'admin#index'
+      get 'admin/wizards/custom/new' => 'admin#index'
+      get 'admin/wizards/custom/all' => 'admin#all'
+      get 'admin/wizards/custom/:id' => 'admin#find'
+      put 'admin/wizards/custom/save' => 'admin#save'
+      delete 'admin/wizards/custom/remove' => 'admin#remove'
     end
   end
-
-  load File.expand_path('../lib/builder.rb', __FILE__)
-  load File.expand_path('../lib/wizard.rb', __FILE__)
-  load File.expand_path('../controllers/steps.rb', __FILE__)
-  load File.expand_path('../controllers/admin.rb', __FILE__)
 end