feature/more.events

This commit is contained in:
Connor Turland 2016-12-14 10:08:59 -05:00
parent 6f88c2a7eb
commit 32700d3ac1
10 changed files with 109 additions and 13 deletions

View file

@ -5,6 +5,21 @@ module Api
def searchable_columns
[]
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

View file

@ -22,7 +22,6 @@ class MappingsController < ApplicationController
if @mapping.save
render json: @mapping, status: :created
Events::NewMapping.publish!(@mapping, current_user)
else
render json: @mapping.errors, status: :unprocessable_entity
end
@ -32,8 +31,10 @@ class MappingsController < ApplicationController
def update
@mapping = Mapping.find(params[:id])
authorize @mapping
@mapping.user = current_user
@mapping.assign_attributes(mapping_params)
if @mapping.update_attributes(mapping_params)
if @mapping.save
head :no_content
else
render json: @mapping.errors, status: :unprocessable_entity
@ -44,14 +45,7 @@ class MappingsController < ApplicationController
def destroy
@mapping = Mapping.find(params[:id])
authorize @mapping
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.user = current_user
@mapping.destroy
head :no_content

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
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
belongs_to :eventable, polymorphic: true

View file

@ -1,9 +1,9 @@
# frozen_string_literal: true
class Events::NewMapping < Event
class Events::SynapseAddedToMap < Event
# after_create :notify_users!
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,
map: mapping.map,
user: user)

View 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

View 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

View 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

View 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

View file

@ -16,6 +16,10 @@ class Mapping < ApplicationRecord
delegate :name, to: :user, prefix: true
after_create :after_created
after_update :after_updated
before_destroy :before_destroyed
def user_image
user.image.url
end
@ -23,4 +27,34 @@ class Mapping < ApplicationRecord
def as_json(_options = {})
super(methods: [:user_name, :user_image])
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

View file

@ -0,0 +1,5 @@
class AddMetaToEvents < ActiveRecord::Migration[5.0]
def change
add_column :events, :meta, :json
end
end