From 3fa2735c63507a5ad8a723fc7c95c4e33d791c95 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 25 Sep 2017 22:47:40 +0800 Subject: [PATCH] various --- {controllers => app/controllers}/admin.rb | 0 app/controllers/steps.rb | 22 ++++++++++++ .../steps.rb => app/controllers/wizard.rb | 10 ++++-- app/views/layouts/custom_wizard.html.erb | 29 +++++++++++++++ .../discourse/routes/admin-wizard.js.es6 | 2 +- assets/javascripts/wizard-custom.js | 3 ++ .../wizard/initializers/custom.js.es6 | 13 +++++++ .../wizard/models/custom-wizard.js.es6 | 22 ++++++++++++ .../wizard/routes/application.js.es6 | 7 ++++ .../javascripts/wizard/routes/custom.js.es6 | 11 ++++++ config/locales/server.en.yml | 3 ++ lib/builder.rb | 31 +++++++++------- plugin.rb | 36 ++++++++++++------- 13 files changed, 160 insertions(+), 29 deletions(-) rename {controllers => app/controllers}/admin.rb (100%) create mode 100644 app/controllers/steps.rb rename controllers/steps.rb => app/controllers/wizard.rb (52%) create mode 100644 app/views/layouts/custom_wizard.html.erb create mode 100644 assets/javascripts/wizard-custom.js create mode 100644 assets/javascripts/wizard/initializers/custom.js.es6 create mode 100644 assets/javascripts/wizard/models/custom-wizard.js.es6 create mode 100644 assets/javascripts/wizard/routes/application.js.es6 create mode 100644 assets/javascripts/wizard/routes/custom.js.es6 create mode 100644 config/locales/server.en.yml diff --git a/controllers/admin.rb b/app/controllers/admin.rb similarity index 100% rename from controllers/admin.rb rename to app/controllers/admin.rb diff --git a/app/controllers/steps.rb b/app/controllers/steps.rb new file mode 100644 index 00000000..720980d0 --- /dev/null +++ b/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 diff --git a/controllers/steps.rb b/app/controllers/wizard.rb similarity index 52% rename from controllers/steps.rb rename to app/controllers/wizard.rb index 2c73901d..94405b8f 100644 --- a/controllers/steps.rb +++ b/app/controllers/wizard.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 {} diff --git a/app/views/layouts/custom_wizard.html.erb b/app/views/layouts/custom_wizard.html.erb new file mode 100644 index 00000000..03e63aaf --- /dev/null +++ b/app/views/layouts/custom_wizard.html.erb @@ -0,0 +1,29 @@ + + + <%= 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" %> + + <%= csrf_meta_tags %> + + + + <%= render partial: "layouts/head" %> + <%= t 'custom_wizard.title' %> + + + +
+ + + + diff --git a/assets/javascripts/discourse/routes/admin-wizard.js.es6 b/assets/javascripts/discourse/routes/admin-wizard.js.es6 index c60c7227..f3f8a018 100644 --- a/assets/javascripts/discourse/routes/admin-wizard.js.es6 +++ b/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'); diff --git a/assets/javascripts/wizard-custom.js b/assets/javascripts/wizard-custom.js new file mode 100644 index 00000000..2e1b5507 --- /dev/null +++ b/assets/javascripts/wizard-custom.js @@ -0,0 +1,3 @@ +//= require_tree ./wizard/initializers +//= require_tree ./wizard/models +//= require_tree ./wizard/routes diff --git a/assets/javascripts/wizard/initializers/custom.js.es6 b/assets/javascripts/wizard/initializers/custom.js.es6 new file mode 100644 index 00000000..d454d387 --- /dev/null +++ b/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' }); + }); + }); + } +}; diff --git a/assets/javascripts/wizard/models/custom-wizard.js.es6 b/assets/javascripts/wizard/models/custom-wizard.js.es6 new file mode 100644 index 00000000..bfe855c7 --- /dev/null +++ b/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); + }); +} diff --git a/assets/javascripts/wizard/routes/application.js.es6 b/assets/javascripts/wizard/routes/application.js.es6 new file mode 100644 index 00000000..7326beed --- /dev/null +++ b/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); + } +}); diff --git a/assets/javascripts/wizard/routes/custom.js.es6 b/assets/javascripts/wizard/routes/custom.js.es6 new file mode 100644 index 00000000..b5d1a9b2 --- /dev/null +++ b/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); + } +}); diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml new file mode 100644 index 00000000..1b332097 --- /dev/null +++ b/config/locales/server.en.yml @@ -0,0 +1,3 @@ +en: + wizard: + title: "Wizard" diff --git a/lib/builder.rb b/lib/builder.rb index 13b75f3a..020cc804 100644 --- a/lib/builder.rb +++ b/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 diff --git a/plugin.rb b/plugin.rb index fb86bdcd..4c6e2ac7 100644 --- a/plugin.rb +++ b/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