keep mapping.user as the creator

This commit is contained in:
Connor Turland 2016-12-14 12:58:47 -05:00
parent 32700d3ac1
commit f8e7bf6d31
2 changed files with 11 additions and 3 deletions

View file

@ -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

View file

@ -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