remove the hack in favor of a legit way
This commit is contained in:
parent
60898d2112
commit
8856ea7a56
9 changed files with 44 additions and 21 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -14,6 +14,7 @@ module Api
|
|||
def self.embeddable
|
||||
{
|
||||
user: {},
|
||||
updated_by: {},
|
||||
map: {}
|
||||
}
|
||||
end
|
||||
|
|
5
db/migrate/20161216174257_add_updated_by_to_mappings.rb
Normal file
5
db/migrate/20161216174257_add_updated_by_to_mappings.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class AddUpdatedByToMappings < ActiveRecord::Migration[5.0]
|
||||
def change
|
||||
add_reference :mappings, :updated_by, foreign_key: {to_table: :users}
|
||||
end
|
||||
end
|
|
@ -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:
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
"mappable_id": 1,
|
||||
"mappable_type": "Synapse",
|
||||
"user_id": 1,
|
||||
"updated_by_id": 1,
|
||||
"map_id": 1
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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" ] }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue