From f8e7bf6d31dd889a39ad92e6ba6c5c79f80ed419 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Wed, 14 Dec 2016 12:58:47 -0500 Subject: [PATCH] keep mapping.user as the creator --- app/controllers/api/v2/mappings_controller.rb | 8 +++++--- app/controllers/mappings_controller.rb | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v2/mappings_controller.rb b/app/controllers/api/v2/mappings_controller.rb index ae25a3c2..61f18940 100644 --- a/app/controllers/api/v2/mappings_controller.rb +++ b/app/controllers/api/v2/mappings_controller.rb @@ -7,11 +7,13 @@ module Api end def update - # mappings are the only things where the user is set to the latest updater - # done this way so that the model hooks can use the mapping user to determine who took this action - resource.user = current_user if current_user.present? # current_user should always be present + # hack: set the user temporarily so the model hook can reference it, then set it back + temp = resource.user + resource.user = current_user update_action respond_with_resource + resourse.user = temp + update_action end def destroy diff --git a/app/controllers/mappings_controller.rb b/app/controllers/mappings_controller.rb index 61270948..f6fc05bf 100644 --- a/app/controllers/mappings_controller.rb +++ b/app/controllers/mappings_controller.rb @@ -31,6 +31,8 @@ class MappingsController < ApplicationController def update @mapping = Mapping.find(params[:id]) authorize @mapping + # hack: set the user temporarily so that the model hook can reference it, and then set it back + temp = @mapping.user @mapping.user = current_user @mapping.assign_attributes(mapping_params) @@ -39,12 +41,16 @@ class MappingsController < ApplicationController else render json: @mapping.errors, status: :unprocessable_entity end + # restore the original mapping creator + @mapping.user = temp + @mapping.save end # DELETE /mappings/1.json def destroy @mapping = Mapping.find(params[:id]) authorize @mapping + # hack: set the user temporarily so that the model hook can reference this user who is taking the action @mapping.user = current_user @mapping.destroy