From a6bb688d0cd1a1b71987db82907fc25dd55733a4 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Wed, 14 Dec 2016 22:08:51 -0500 Subject: [PATCH] capture topic and synapse updates, store the old values --- app/models/event.rb | 3 ++- app/models/events/synapse_updated.rb | 11 +++++++++++ app/models/events/topic_updated.rb | 11 +++++++++++ app/models/synapse.rb | 12 ++++++++++++ app/models/topic.rb | 11 +++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 app/models/events/synapse_updated.rb create mode 100644 app/models/events/topic_updated.rb diff --git a/app/models/event.rb b/app/models/event.rb index 59966fba..cf974664 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -2,7 +2,8 @@ class Event < ApplicationRecord 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 + synapse_added_to_map synapse_removed_from_map + topic_updated synapse_updated).freeze belongs_to :eventable, polymorphic: true belongs_to :map diff --git a/app/models/events/synapse_updated.rb b/app/models/events/synapse_updated.rb new file mode 100644 index 00000000..0d85cbe8 --- /dev/null +++ b/app/models/events/synapse_updated.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true +class Events::SynapseUpdated < Event + # after_create :notify_users! + + def self.publish!(synapse, user, meta) + create!(kind: 'synapse_updated', + eventable: synapse, + user: user, + meta: meta) + end +end diff --git a/app/models/events/topic_updated.rb b/app/models/events/topic_updated.rb new file mode 100644 index 00000000..fd41a4d6 --- /dev/null +++ b/app/models/events/topic_updated.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true +class Events::TopicUpdated < Event + # after_create :notify_users! + + def self.publish!(topic, user, meta) + create!(kind: 'topic_updated', + eventable: topic, + user: user, + meta: meta) + end +end diff --git a/app/models/synapse.rb b/app/models/synapse.rb index 08512e4f..31e997f4 100644 --- a/app/models/synapse.rb +++ b/app/models/synapse.rb @@ -22,6 +22,8 @@ class Synapse < ApplicationRecord where(topic1_id: topic_id).or(where(topic2_id: topic_id)) } + after_update :after_updated + delegate :name, to: :user, prefix: true def user_image @@ -39,4 +41,14 @@ class Synapse < ApplicationRecord def as_json(_options = {}) super(methods: [:user_name, :user_image, :collaborator_ids]) end + + def after_updated + attrs = ['desc', 'category', 'permission', 'defer_to_map_id'] + if attrs.any? {|k| changed_attributes.key?(k)} + new = self.attributes.select {|k,v| attrs.include?(k) } + old = changed_attributes.select {|k,v| attrs.include?(k) } + meta = new.merge(old) # we are prioritizing the old values, keeping them + Events::SynapseUpdated.publish!(self, user, meta) + end + end end diff --git a/app/models/topic.rb b/app/models/topic.rb index 256fc604..4670c286 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -16,6 +16,7 @@ class Topic < ApplicationRecord belongs_to :metacode before_create :create_metamap? + after_update :after_updated validates :permission, presence: true validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) } @@ -135,4 +136,14 @@ class Topic < ApplicationRecord self.link = Rails.application.routes.url_helpers .map_url(host: ENV['MAILER_DEFAULT_URL'], id: @map.id) end + + def after_updated + attrs = ['name', 'desc', 'link', 'metacode_id', 'permission', 'defer_to_map_id'] + if attrs.any? {|k| changed_attributes.key?(k)} + new = self.attributes.select {|k,v| attrs.include?(k) } + old = changed_attributes.select {|k,v| attrs.include?(k) } + meta = new.merge(old) # we are prioritizing the old values, keeping them + Events::TopicUpdated.publish!(self, user, meta) + end + end end