capture topic and synapse updates, store the old values

This commit is contained in:
Connor Turland 2016-12-14 22:08:51 -05:00
parent 24aeebedf7
commit a6bb688d0c
5 changed files with 47 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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