diff --git a/app/controllers/super_admin/madmp_schemas_controller.rb b/app/controllers/super_admin/madmp_schemas_controller.rb index a9a8dcc..2973c4f 100644 --- a/app/controllers/super_admin/madmp_schemas_controller.rb +++ b/app/controllers/super_admin/madmp_schemas_controller.rb @@ -1,6 +1,9 @@ module SuperAdmin class MadmpSchemasController < ApplicationController + before_action :set_schema, only: %i[edit update destroy] + before_action :substitute_names, only: [:update] + # GET /madmp_schemas def index authorize(MadmpSchema) @@ -26,13 +29,11 @@ def edit authorize(MadmpSchema) - @schema = MadmpSchema.find(params[:id]) end def update authorize(MadmpSchema) - @schema = MadmpSchema.find(params[:id]) if @schema.update_attributes(permitted_params) flash.now[:notice] = success_message(@schema, _("updated")) else @@ -43,7 +44,6 @@ def destroy authorize(MadmpSchema) - @schema = MadmpSchema.find(params[:id]) if @schema.destroy msg = success_message(@schema, _("deleted")) redirect_to super_admin_madmp_schemas_path, notice: msg @@ -57,6 +57,28 @@ # Private instance methods private + # Set the @schema var + def set_schema + @schema = MadmpSchema.find(params[:id]) + end + + # Substitute "template_name" key/values for their "schema_id" equivalent in the JSON + def substitute_names + # Get the actual JSON schema from the params + json_data = permitted_params[:schema] + + # Find and replace the values + json_data = JsonPath.for(json_data).gsub('$..template_name') do |name| + MadmpSchema.find_by!(name: name).id + end.to_json + + # Replace the key names + json_data = json_data.gsub('template_name', 'schema_id') + + # Update the params + params[:madmp_schema][:schema] = json_data + end + def permitted_params params.require(:madmp_schema).permit(:label, :name, :version, :classname, :schema) end