diff --git a/app/controllers/api/v2/mappings_controller.rb b/app/controllers/api/v2/mappings_controller.rb index 61f18940..186d6891 100644 --- a/app/controllers/api/v2/mappings_controller.rb +++ b/app/controllers/api/v2/mappings_controller.rb @@ -6,19 +6,23 @@ module Api [] end + def create + instantiate_resource + resource.user = current_user if current_user.present? + resource.updated_by = current_user if current_user.present? + authorize resource + create_action + respond_with_resource + end + def update - # hack: set the user temporarily so the model hook can reference it, then set it back - temp = resource.user - resource.user = current_user + resource.updated_by = current_user if current_user.present? update_action respond_with_resource - resourse.user = temp - update_action end def destroy - # this is done 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 + resource.updated_by = current_user if current_user.present? destroy_action head :no_content end diff --git a/app/controllers/mappings_controller.rb b/app/controllers/mappings_controller.rb index f6fc05bf..86db023e 100644 --- a/app/controllers/mappings_controller.rb +++ b/app/controllers/mappings_controller.rb @@ -19,6 +19,7 @@ class MappingsController < ApplicationController @mapping = Mapping.new(mapping_params) authorize @mapping @mapping.user = current_user + @mapping.updated_by = current_user if @mapping.save render json: @mapping, status: :created @@ -31,9 +32,7 @@ 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.updated_by = current_user @mapping.assign_attributes(mapping_params) if @mapping.save @@ -41,17 +40,13 @@ 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.updated_by = current_user @mapping.destroy head :no_content diff --git a/app/models/mapping.rb b/app/models/mapping.rb index 4285f4b6..99d23db0 100644 --- a/app/models/mapping.rb +++ b/app/models/mapping.rb @@ -6,6 +6,7 @@ class Mapping < ApplicationRecord belongs_to :mappable, polymorphic: true belongs_to :map, class_name: 'Map', foreign_key: 'map_id', touch: true belongs_to :user + belongs_to :updated_by, class_name: 'User' validates :xloc, presence: true, unless: proc { |m| m.mappable_type == 'Synapse' } @@ -40,7 +41,7 @@ class Mapping < ApplicationRecord def after_updated if mappable_type == 'Topic' and (xloc_changed? or yloc_changed?) meta = {'x': xloc, 'y': yloc, 'mapping_id': id} - Events::TopicMovedOnMap.publish!(mappable, map, user, meta) + Events::TopicMovedOnMap.publish!(mappable, map, updated_by, meta) end end @@ -53,9 +54,9 @@ class Mapping < ApplicationRecord meta = {'mapping_id': id} if mappable_type == 'Topic' - Events::TopicRemovedFromMap.publish!(mappable, map, user, meta) + Events::TopicRemovedFromMap.publish!(mappable, map, updated_by, meta) elsif mappable_type == 'Synapse' - Events::SynapseRemovedFromMap.publish!(mappable, map, user, meta) + Events::SynapseRemovedFromMap.publish!(mappable, map, updated_by, meta) end end end diff --git a/app/serializers/api/v2/mapping_serializer.rb b/app/serializers/api/v2/mapping_serializer.rb index 19e7318e..30c9bd7f 100644 --- a/app/serializers/api/v2/mapping_serializer.rb +++ b/app/serializers/api/v2/mapping_serializer.rb @@ -14,6 +14,7 @@ module Api def self.embeddable { user: {}, + updated_by: {}, map: {} } end diff --git a/db/migrate/20161216174257_add_updated_by_to_mappings.rb b/db/migrate/20161216174257_add_updated_by_to_mappings.rb new file mode 100644 index 00000000..e28b8281 --- /dev/null +++ b/db/migrate/20161216174257_add_updated_by_to_mappings.rb @@ -0,0 +1,5 @@ +class AddUpdatedByToMappings < ActiveRecord::Migration[5.0] + def change + add_reference :mappings, :updated_by, foreign_key: {to_table: :users} + end +end diff --git a/doc/api/apis/mappings.raml b/doc/api/apis/mappings.raml index 00298387..af4965cb 100644 --- a/doc/api/apis/mappings.raml +++ b/doc/api/apis/mappings.raml @@ -1,6 +1,6 @@ #type: collection get: - is: [ embeddable: { embedFields: "user,map" }, orderable, pageable ] + is: [ embeddable: { embedFields: "user,updated_by,map" }, orderable, pageable ] securedBy: [ null, token, oauth_2_0, cookie ] responses: 200: @@ -31,7 +31,7 @@ post: /{id}: #type: item get: - is: [ embeddable: { embedFields: "user,map" } ] + is: [ embeddable: { embedFields: "user,updated_by,map" } ] securedBy: [ null, token, oauth_2_0, cookie ] responses: 200: diff --git a/doc/api/examples/mapping.json b/doc/api/examples/mapping.json index c4aa87bf..93d38bdb 100644 --- a/doc/api/examples/mapping.json +++ b/doc/api/examples/mapping.json @@ -6,6 +6,7 @@ "mappable_id": 1, "mappable_type": "Synapse", "user_id": 1, + "updated_by_id": 1, "map_id": 1 } } diff --git a/doc/api/examples/mappings.json b/doc/api/examples/mappings.json index 5a4a99c3..99f2e58d 100644 --- a/doc/api/examples/mappings.json +++ b/doc/api/examples/mappings.json @@ -8,6 +8,7 @@ "mappable_type": "Topic", "updated_at": "2016-03-25T08:44:07.152Z", "user_id": 1, + "updated_by_id": 1, "xloc": -271, "yloc": 22 }, @@ -19,6 +20,7 @@ "mappable_type": "Topic", "updated_at": "2016-03-25T08:44:13.907Z", "user_id": 1, + "updated_by_id": 1, "xloc": -12, "yloc": 61 }, @@ -30,6 +32,7 @@ "mappable_type": "Topic", "updated_at": "2016-03-25T08:44:19.333Z", "user_id": 1, + "updated_by_id": 1, "xloc": -93, "yloc": -90 }, @@ -40,7 +43,8 @@ "mappable_id": 1, "mappable_type": "Synapse", "updated_at": "2016-03-25T08:44:21.337Z", - "user_id": 1 + "user_id": 1, + "updated_by_id": 1 } ], "page": { diff --git a/doc/api/schemas/_mapping.json b/doc/api/schemas/_mapping.json index 8789c5ec..efd12c92 100644 --- a/doc/api/schemas/_mapping.json +++ b/doc/api/schemas/_mapping.json @@ -35,6 +35,12 @@ }, "user": { "$ref": "_user.json" + }, + "updated_by_id": { + "$ref": "_id.json" + }, + "updated_by": { + "$ref": "_user.json" } }, "required": [ @@ -56,6 +62,12 @@ { "required": [ "user_id" ] }, { "required": [ "user" ] } ] + }, + { + "oneOf": [ + { "required": [ "updated_by_id" ] }, + { "required": [ "updated_by" ] } + ] } ] }