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
|
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
|
def update
|
||||||
# hack: set the user temporarily so the model hook can reference it, then set it back
|
resource.updated_by = current_user if current_user.present?
|
||||||
temp = resource.user
|
|
||||||
resource.user = current_user
|
|
||||||
update_action
|
update_action
|
||||||
respond_with_resource
|
respond_with_resource
|
||||||
resourse.user = temp
|
|
||||||
update_action
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
# this is done so that the model hooks can use the mapping user to determine who took this action
|
resource.updated_by = current_user if current_user.present?
|
||||||
resource.user = current_user if current_user.present? # current_user should always be present
|
|
||||||
destroy_action
|
destroy_action
|
||||||
head :no_content
|
head :no_content
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,6 +19,7 @@ class MappingsController < ApplicationController
|
||||||
@mapping = Mapping.new(mapping_params)
|
@mapping = Mapping.new(mapping_params)
|
||||||
authorize @mapping
|
authorize @mapping
|
||||||
@mapping.user = current_user
|
@mapping.user = current_user
|
||||||
|
@mapping.updated_by = current_user
|
||||||
|
|
||||||
if @mapping.save
|
if @mapping.save
|
||||||
render json: @mapping, status: :created
|
render json: @mapping, status: :created
|
||||||
|
@ -31,9 +32,7 @@ class MappingsController < ApplicationController
|
||||||
def update
|
def update
|
||||||
@mapping = Mapping.find(params[:id])
|
@mapping = Mapping.find(params[:id])
|
||||||
authorize @mapping
|
authorize @mapping
|
||||||
# hack: set the user temporarily so that the model hook can reference it, and then set it back
|
@mapping.updated_by = current_user
|
||||||
temp = @mapping.user
|
|
||||||
@mapping.user = current_user
|
|
||||||
@mapping.assign_attributes(mapping_params)
|
@mapping.assign_attributes(mapping_params)
|
||||||
|
|
||||||
if @mapping.save
|
if @mapping.save
|
||||||
|
@ -41,17 +40,13 @@ class MappingsController < ApplicationController
|
||||||
else
|
else
|
||||||
render json: @mapping.errors, status: :unprocessable_entity
|
render json: @mapping.errors, status: :unprocessable_entity
|
||||||
end
|
end
|
||||||
# restore the original mapping creator
|
|
||||||
@mapping.user = temp
|
|
||||||
@mapping.save
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# DELETE /mappings/1.json
|
# DELETE /mappings/1.json
|
||||||
def destroy
|
def destroy
|
||||||
@mapping = Mapping.find(params[:id])
|
@mapping = Mapping.find(params[:id])
|
||||||
authorize @mapping
|
authorize @mapping
|
||||||
# hack: set the user temporarily so that the model hook can reference this user who is taking the action
|
@mapping.updated_by = current_user
|
||||||
@mapping.user = current_user
|
|
||||||
@mapping.destroy
|
@mapping.destroy
|
||||||
|
|
||||||
head :no_content
|
head :no_content
|
||||||
|
|
|
@ -6,6 +6,7 @@ class Mapping < ApplicationRecord
|
||||||
belongs_to :mappable, polymorphic: true
|
belongs_to :mappable, polymorphic: true
|
||||||
belongs_to :map, class_name: 'Map', foreign_key: 'map_id', touch: true
|
belongs_to :map, class_name: 'Map', foreign_key: 'map_id', touch: true
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
belongs_to :updated_by, class_name: 'User'
|
||||||
|
|
||||||
validates :xloc, presence: true,
|
validates :xloc, presence: true,
|
||||||
unless: proc { |m| m.mappable_type == 'Synapse' }
|
unless: proc { |m| m.mappable_type == 'Synapse' }
|
||||||
|
@ -40,7 +41,7 @@ class Mapping < ApplicationRecord
|
||||||
def after_updated
|
def after_updated
|
||||||
if mappable_type == 'Topic' and (xloc_changed? or yloc_changed?)
|
if mappable_type == 'Topic' and (xloc_changed? or yloc_changed?)
|
||||||
meta = {'x': xloc, 'y': yloc, 'mapping_id': id}
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -53,9 +54,9 @@ class Mapping < ApplicationRecord
|
||||||
|
|
||||||
meta = {'mapping_id': id}
|
meta = {'mapping_id': id}
|
||||||
if mappable_type == 'Topic'
|
if mappable_type == 'Topic'
|
||||||
Events::TopicRemovedFromMap.publish!(mappable, map, user, meta)
|
Events::TopicRemovedFromMap.publish!(mappable, map, updated_by, meta)
|
||||||
elsif mappable_type == 'Synapse'
|
elsif mappable_type == 'Synapse'
|
||||||
Events::SynapseRemovedFromMap.publish!(mappable, map, user, meta)
|
Events::SynapseRemovedFromMap.publish!(mappable, map, updated_by, meta)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,6 +14,7 @@ module Api
|
||||||
def self.embeddable
|
def self.embeddable
|
||||||
{
|
{
|
||||||
user: {},
|
user: {},
|
||||||
|
updated_by: {},
|
||||||
map: {}
|
map: {}
|
||||||
}
|
}
|
||||||
end
|
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
|
#type: collection
|
||||||
get:
|
get:
|
||||||
is: [ embeddable: { embedFields: "user,map" }, orderable, pageable ]
|
is: [ embeddable: { embedFields: "user,updated_by,map" }, orderable, pageable ]
|
||||||
securedBy: [ null, token, oauth_2_0, cookie ]
|
securedBy: [ null, token, oauth_2_0, cookie ]
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
|
@ -31,7 +31,7 @@ post:
|
||||||
/{id}:
|
/{id}:
|
||||||
#type: item
|
#type: item
|
||||||
get:
|
get:
|
||||||
is: [ embeddable: { embedFields: "user,map" } ]
|
is: [ embeddable: { embedFields: "user,updated_by,map" } ]
|
||||||
securedBy: [ null, token, oauth_2_0, cookie ]
|
securedBy: [ null, token, oauth_2_0, cookie ]
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
"mappable_id": 1,
|
"mappable_id": 1,
|
||||||
"mappable_type": "Synapse",
|
"mappable_type": "Synapse",
|
||||||
"user_id": 1,
|
"user_id": 1,
|
||||||
|
"updated_by_id": 1,
|
||||||
"map_id": 1
|
"map_id": 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
"mappable_type": "Topic",
|
"mappable_type": "Topic",
|
||||||
"updated_at": "2016-03-25T08:44:07.152Z",
|
"updated_at": "2016-03-25T08:44:07.152Z",
|
||||||
"user_id": 1,
|
"user_id": 1,
|
||||||
|
"updated_by_id": 1,
|
||||||
"xloc": -271,
|
"xloc": -271,
|
||||||
"yloc": 22
|
"yloc": 22
|
||||||
},
|
},
|
||||||
|
@ -19,6 +20,7 @@
|
||||||
"mappable_type": "Topic",
|
"mappable_type": "Topic",
|
||||||
"updated_at": "2016-03-25T08:44:13.907Z",
|
"updated_at": "2016-03-25T08:44:13.907Z",
|
||||||
"user_id": 1,
|
"user_id": 1,
|
||||||
|
"updated_by_id": 1,
|
||||||
"xloc": -12,
|
"xloc": -12,
|
||||||
"yloc": 61
|
"yloc": 61
|
||||||
},
|
},
|
||||||
|
@ -30,6 +32,7 @@
|
||||||
"mappable_type": "Topic",
|
"mappable_type": "Topic",
|
||||||
"updated_at": "2016-03-25T08:44:19.333Z",
|
"updated_at": "2016-03-25T08:44:19.333Z",
|
||||||
"user_id": 1,
|
"user_id": 1,
|
||||||
|
"updated_by_id": 1,
|
||||||
"xloc": -93,
|
"xloc": -93,
|
||||||
"yloc": -90
|
"yloc": -90
|
||||||
},
|
},
|
||||||
|
@ -40,7 +43,8 @@
|
||||||
"mappable_id": 1,
|
"mappable_id": 1,
|
||||||
"mappable_type": "Synapse",
|
"mappable_type": "Synapse",
|
||||||
"updated_at": "2016-03-25T08:44:21.337Z",
|
"updated_at": "2016-03-25T08:44:21.337Z",
|
||||||
"user_id": 1
|
"user_id": 1,
|
||||||
|
"updated_by_id": 1
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"page": {
|
"page": {
|
||||||
|
|
|
@ -35,6 +35,12 @@
|
||||||
},
|
},
|
||||||
"user": {
|
"user": {
|
||||||
"$ref": "_user.json"
|
"$ref": "_user.json"
|
||||||
|
},
|
||||||
|
"updated_by_id": {
|
||||||
|
"$ref": "_id.json"
|
||||||
|
},
|
||||||
|
"updated_by": {
|
||||||
|
"$ref": "_user.json"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
"required": [
|
||||||
|
@ -56,6 +62,12 @@
|
||||||
{ "required": [ "user_id" ] },
|
{ "required": [ "user_id" ] },
|
||||||
{ "required": [ "user" ] }
|
{ "required": [ "user" ] }
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"oneOf": [
|
||||||
|
{ "required": [ "updated_by_id" ] },
|
||||||
|
{ "required": [ "updated_by" ] }
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue