diff --git a/app/controllers/api/v2/mappings_controller.rb b/app/controllers/api/v2/mappings_controller.rb index 4490e4af..ae25a3c2 100644 --- a/app/controllers/api/v2/mappings_controller.rb +++ b/app/controllers/api/v2/mappings_controller.rb @@ -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 diff --git a/app/controllers/mappings_controller.rb b/app/controllers/mappings_controller.rb index de2c8ea1..61270948 100644 --- a/app/controllers/mappings_controller.rb +++ b/app/controllers/mappings_controller.rb @@ -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 diff --git a/app/models/event.rb b/app/models/event.rb index 02c6d698..d88cb3bb 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -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 diff --git a/app/models/events/new_mapping.rb b/app/models/events/synapse_added_to_map.rb similarity index 59% rename from app/models/events/new_mapping.rb rename to app/models/events/synapse_added_to_map.rb index 889c69bc..c468fad6 100644 --- a/app/models/events/new_mapping.rb +++ b/app/models/events/synapse_added_to_map.rb @@ -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) diff --git a/app/models/events/synapse_removed_from_map.rb b/app/models/events/synapse_removed_from_map.rb new file mode 100644 index 00000000..765ed95d --- /dev/null +++ b/app/models/events/synapse_removed_from_map.rb @@ -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 diff --git a/app/models/events/topic_added_to_map.rb b/app/models/events/topic_added_to_map.rb new file mode 100644 index 00000000..70a4497d --- /dev/null +++ b/app/models/events/topic_added_to_map.rb @@ -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 diff --git a/app/models/events/topic_moved_on_map.rb b/app/models/events/topic_moved_on_map.rb new file mode 100644 index 00000000..d2fcd96e --- /dev/null +++ b/app/models/events/topic_moved_on_map.rb @@ -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 diff --git a/app/models/events/topic_removed_from_map.rb b/app/models/events/topic_removed_from_map.rb new file mode 100644 index 00000000..437c881f --- /dev/null +++ b/app/models/events/topic_removed_from_map.rb @@ -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 diff --git a/app/models/mapping.rb b/app/models/mapping.rb index f7219008..50aa47a7 100644 --- a/app/models/mapping.rb +++ b/app/models/mapping.rb @@ -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 diff --git a/db/migrate/20161214140124_add_meta_to_events.rb b/db/migrate/20161214140124_add_meta_to_events.rb new file mode 100644 index 00000000..edbee3d6 --- /dev/null +++ b/db/migrate/20161214140124_add_meta_to_events.rb @@ -0,0 +1,5 @@ +class AddMetaToEvents < ActiveRecord::Migration[5.0] + def change + add_column :events, :meta, :json + end +end