feature/more.events
This commit is contained in:
parent
6f88c2a7eb
commit
32700d3ac1
10 changed files with 109 additions and 13 deletions
|
@ -5,6 +5,21 @@ module Api
|
||||||
def searchable_columns
|
def searchable_columns
|
||||||
[]
|
[]
|
||||||
end
|
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
|
||||||
|
update_action
|
||||||
|
respond_with_resource
|
||||||
|
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
|
||||||
|
destroy_action
|
||||||
|
head :no_content
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,7 +22,6 @@ class MappingsController < ApplicationController
|
||||||
|
|
||||||
if @mapping.save
|
if @mapping.save
|
||||||
render json: @mapping, status: :created
|
render json: @mapping, status: :created
|
||||||
Events::NewMapping.publish!(@mapping, current_user)
|
|
||||||
else
|
else
|
||||||
render json: @mapping.errors, status: :unprocessable_entity
|
render json: @mapping.errors, status: :unprocessable_entity
|
||||||
end
|
end
|
||||||
|
@ -32,8 +31,10 @@ class MappingsController < ApplicationController
|
||||||
def update
|
def update
|
||||||
@mapping = Mapping.find(params[:id])
|
@mapping = Mapping.find(params[:id])
|
||||||
authorize @mapping
|
authorize @mapping
|
||||||
|
@mapping.user = current_user
|
||||||
|
@mapping.assign_attributes(mapping_params)
|
||||||
|
|
||||||
if @mapping.update_attributes(mapping_params)
|
if @mapping.save
|
||||||
head :no_content
|
head :no_content
|
||||||
else
|
else
|
||||||
render json: @mapping.errors, status: :unprocessable_entity
|
render json: @mapping.errors, status: :unprocessable_entity
|
||||||
|
@ -44,14 +45,7 @@ class MappingsController < ApplicationController
|
||||||
def destroy
|
def destroy
|
||||||
@mapping = Mapping.find(params[:id])
|
@mapping = Mapping.find(params[:id])
|
||||||
authorize @mapping
|
authorize @mapping
|
||||||
|
@mapping.user = current_user
|
||||||
mappable = @mapping.mappable
|
|
||||||
if mappable.defer_to_map
|
|
||||||
mappable.permission = mappable.defer_to_map.permission
|
|
||||||
mappable.defer_to_map_id = nil
|
|
||||||
mappable.save
|
|
||||||
end
|
|
||||||
|
|
||||||
@mapping.destroy
|
@mapping.destroy
|
||||||
|
|
||||||
head :no_content
|
head :no_content
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
class Event < ApplicationRecord
|
class Event < ApplicationRecord
|
||||||
KINDS = %w(user_present_on_map conversation_started_on_map topic_added_to_map synapse_added_to_map).freeze
|
KINDS = %w(user_present_on_map conversation_started_on_map
|
||||||
|
topic_added_to_map topic_moved_on_map topic_removed_from_map
|
||||||
|
synapse_added_to_map synapse_removed_from_map).freeze
|
||||||
|
|
||||||
# has_many :notifications, dependent: :destroy
|
# has_many :notifications, dependent: :destroy
|
||||||
belongs_to :eventable, polymorphic: true
|
belongs_to :eventable, polymorphic: true
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
class Events::NewMapping < Event
|
class Events::SynapseAddedToMap < Event
|
||||||
# after_create :notify_users!
|
# after_create :notify_users!
|
||||||
|
|
||||||
def self.publish!(mapping, user)
|
def self.publish!(mapping, user)
|
||||||
create!(kind: mapping.mappable_type == 'Topic' ? 'topic_added_to_map' : 'synapse_added_to_map',
|
create!(kind: 'synapse_added_to_map',
|
||||||
eventable: mapping,
|
eventable: mapping,
|
||||||
map: mapping.map,
|
map: mapping.map,
|
||||||
user: user)
|
user: user)
|
11
app/models/events/synapse_removed_from_map.rb
Normal file
11
app/models/events/synapse_removed_from_map.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
class Events::SynapseRemovedFromMap < Event
|
||||||
|
# after_create :notify_users!
|
||||||
|
|
||||||
|
def self.publish!(mapping, user)
|
||||||
|
create!(kind: 'synapse_removed_from_map',
|
||||||
|
eventable: mapping,
|
||||||
|
map: mapping.map,
|
||||||
|
user: user)
|
||||||
|
end
|
||||||
|
end
|
12
app/models/events/topic_added_to_map.rb
Normal file
12
app/models/events/topic_added_to_map.rb
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
class Events::TopicAddedToMap < Event
|
||||||
|
# after_create :notify_users!
|
||||||
|
|
||||||
|
def self.publish!(mapping, user, meta)
|
||||||
|
create!(kind: 'topic_added_to_map',
|
||||||
|
eventable: mapping,
|
||||||
|
map: mapping.map,
|
||||||
|
user: user,
|
||||||
|
meta: meta)
|
||||||
|
end
|
||||||
|
end
|
12
app/models/events/topic_moved_on_map.rb
Normal file
12
app/models/events/topic_moved_on_map.rb
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
class Events::TopicMovedOnMap < Event
|
||||||
|
# after_create :notify_users!
|
||||||
|
|
||||||
|
def self.publish!(mapping, user, meta)
|
||||||
|
create!(kind: 'topic_moved_on_map',
|
||||||
|
eventable: mapping,
|
||||||
|
map: mapping.map,
|
||||||
|
user: user,
|
||||||
|
meta: meta)
|
||||||
|
end
|
||||||
|
end
|
11
app/models/events/topic_removed_from_map.rb
Normal file
11
app/models/events/topic_removed_from_map.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
class Events::TopicRemovedFromMap < Event
|
||||||
|
# after_create :notify_users!
|
||||||
|
|
||||||
|
def self.publish!(mapping, user)
|
||||||
|
create!(kind: 'topic_removed_from_map',
|
||||||
|
eventable: mapping,
|
||||||
|
map: mapping.map,
|
||||||
|
user: user)
|
||||||
|
end
|
||||||
|
end
|
|
@ -16,6 +16,10 @@ class Mapping < ApplicationRecord
|
||||||
|
|
||||||
delegate :name, to: :user, prefix: true
|
delegate :name, to: :user, prefix: true
|
||||||
|
|
||||||
|
after_create :after_created
|
||||||
|
after_update :after_updated
|
||||||
|
before_destroy :before_destroyed
|
||||||
|
|
||||||
def user_image
|
def user_image
|
||||||
user.image.url
|
user.image.url
|
||||||
end
|
end
|
||||||
|
@ -23,4 +27,34 @@ class Mapping < ApplicationRecord
|
||||||
def as_json(_options = {})
|
def as_json(_options = {})
|
||||||
super(methods: [:user_name, :user_image])
|
super(methods: [:user_name, :user_image])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def after_created
|
||||||
|
if mappable_type == 'Topic'
|
||||||
|
meta = {'x': xloc, 'y': yloc}
|
||||||
|
Events::TopicAddedToMap.publish!(self, user, meta)
|
||||||
|
elsif mappable_type == 'Synapse'
|
||||||
|
Events::SynapseAddedToMap.publish!(self, user)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def after_updated
|
||||||
|
if mappable_type == 'Topic' and (xloc_changed? or yloc_changed?)
|
||||||
|
meta = {'x': xloc, 'y': yloc}
|
||||||
|
Events::TopicMovedOnMap.publish!(self, user, meta)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def before_destroyed
|
||||||
|
if mappable.defer_to_map
|
||||||
|
mappable.permission = mappable.defer_to_map.permission
|
||||||
|
mappable.defer_to_map_id = nil
|
||||||
|
mappable.save
|
||||||
|
end
|
||||||
|
|
||||||
|
if mappable_type == 'Topic'
|
||||||
|
Events::TopicRemovedFromMap.publish!(self, user)
|
||||||
|
elsif mappable_type == 'Synapse'
|
||||||
|
Events::SynapseRemovedFromMap.publish!(self, user)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
5
db/migrate/20161214140124_add_meta_to_events.rb
Normal file
5
db/migrate/20161214140124_add_meta_to_events.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class AddMetaToEvents < ActiveRecord::Migration[5.0]
|
||||||
|
def change
|
||||||
|
add_column :events, :meta, :json
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue