From d57f260defa01052fa0e9f544add48efc3606982 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sat, 12 Mar 2022 15:20:54 +0100 Subject: [PATCH] Cleanup after merge --- app/controllers/custom_wizard/admin/logs.rb | 41 +++++++++++++++++-- .../custom_wizard/log_serializer.rb | 8 +++- .../custom_wizard/submission_serializer.rb | 34 +++++++++++---- spec/plugin_helper.rb | 17 -------- 4 files changed, 70 insertions(+), 30 deletions(-) diff --git a/app/controllers/custom_wizard/admin/logs.rb b/app/controllers/custom_wizard/admin/logs.rb index 976814f8..7ca37bb2 100644 --- a/app/controllers/custom_wizard/admin/logs.rb +++ b/app/controllers/custom_wizard/admin/logs.rb @@ -1,9 +1,44 @@ # frozen_string_literal: true class CustomWizard::AdminLogsController < CustomWizard::AdminController + before_action :find_wizard, except: [:index] + def index - render_serialized( - CustomWizard::Log.list(params[:page].to_i, params[:limit].to_i), - CustomWizard::LogSerializer + render json: ActiveModel::ArraySerializer.new( + CustomWizard::Wizard.list(current_user), + each_serializer: CustomWizard::BasicWizardSerializer ) end + + def show + render_json_dump( + wizard: CustomWizard::BasicWizardSerializer.new(@wizard, root: false), + logs: ActiveModel::ArraySerializer.new( + log_list.logs, + each_serializer: CustomWizard::LogSerializer + ), + total: log_list.total + ) + end + + protected + + def log_list + @log_list ||= begin + list = CustomWizard::Log.list(params[:page].to_i, params[:limit].to_i, params[:wizard_id]) + + if list.logs.any? && (usernames = list.logs.map(&:username)).present? + user_map = User.where(username: usernames) + .reduce({}) do |result, user| + result[user.username] = user + result + end + + list.logs.each do |log_item| + log_item.user = user_map[log_item.username] + end + end + + list + end + end end diff --git a/app/serializers/custom_wizard/log_serializer.rb b/app/serializers/custom_wizard/log_serializer.rb index e521c573..56c5fd8f 100644 --- a/app/serializers/custom_wizard/log_serializer.rb +++ b/app/serializers/custom_wizard/log_serializer.rb @@ -1,4 +1,10 @@ # frozen_string_literal: true + class CustomWizard::LogSerializer < ApplicationSerializer - attributes :message, :date + attributes :date, + :action, + :username, + :message + + has_one :user, serializer: ::BasicUserSerializer, embed: :objects end diff --git a/app/serializers/custom_wizard/submission_serializer.rb b/app/serializers/custom_wizard/submission_serializer.rb index 52f0cb32..9c2f8133 100644 --- a/app/serializers/custom_wizard/submission_serializer.rb +++ b/app/serializers/custom_wizard/submission_serializer.rb @@ -1,16 +1,32 @@ # frozen_string_literal: true class CustomWizard::SubmissionSerializer < ApplicationSerializer attributes :id, - :username, :fields, - :submitted_at, - :route_to, - :redirect_on_complete, - :redirect_to + :submitted_at - def username - object.user.present? ? - object.user.username : - I18n.t('admin.wizard.submission.no_user', user_id: object.user_id) + has_one :user, serializer: ::BasicUserSerializer, embed: :objects + + def include_user? + object.user.present? + end + + def fields + @fields ||= begin + result = {} + + object.wizard.template['steps'].each do |step| + step['fields'].each do |field| + if value = object.fields[field['id']] + result[field['id']] = { + value: value, + type: field['type'], + label: field['label'] + } + end + end + end + + result + end end end diff --git a/spec/plugin_helper.rb b/spec/plugin_helper.rb index 232a2411..6e340ccf 100644 --- a/spec/plugin_helper.rb +++ b/spec/plugin_helper.rb @@ -1,22 +1,5 @@ # frozen_string_literal: true -if ENV['SIMPLECOV'] - require 'simplecov' - - SimpleCov.start do - root "plugins/discourse-custom-wizard" - track_files "plugins/discourse-custom-wizard/**/*.rb" - add_filter { |src| src.filename =~ /(\/spec\/|\/db\/|plugin\.rb|api|gems)/ } - SimpleCov.minimum_coverage 80 - end -end - -require 'oj' -Oj.default_options = Oj.default_options.merge(cache_str: -1) - -require 'rails_helper' -require 'webmock/rspec' - def get_wizard_fixture(path) JSON.parse( File.open(