capture topic and synapse updates, store the old values
This commit is contained in:
parent
24aeebedf7
commit
a6bb688d0c
5 changed files with 47 additions and 1 deletions
|
@ -2,7 +2,8 @@
|
||||||
class Event < ApplicationRecord
|
class Event < ApplicationRecord
|
||||||
KINDS = %w(user_present_on_map conversation_started_on_map
|
KINDS = %w(user_present_on_map conversation_started_on_map
|
||||||
topic_added_to_map topic_moved_on_map topic_removed_from_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 :eventable, polymorphic: true
|
||||||
belongs_to :map
|
belongs_to :map
|
||||||
|
|
11
app/models/events/synapse_updated.rb
Normal file
11
app/models/events/synapse_updated.rb
Normal 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
|
11
app/models/events/topic_updated.rb
Normal file
11
app/models/events/topic_updated.rb
Normal 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
|
|
@ -22,6 +22,8 @@ class Synapse < ApplicationRecord
|
||||||
where(topic1_id: topic_id).or(where(topic2_id: topic_id))
|
where(topic1_id: topic_id).or(where(topic2_id: topic_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
after_update :after_updated
|
||||||
|
|
||||||
delegate :name, to: :user, prefix: true
|
delegate :name, to: :user, prefix: true
|
||||||
|
|
||||||
def user_image
|
def user_image
|
||||||
|
@ -39,4 +41,14 @@ class Synapse < ApplicationRecord
|
||||||
def as_json(_options = {})
|
def as_json(_options = {})
|
||||||
super(methods: [:user_name, :user_image, :collaborator_ids])
|
super(methods: [:user_name, :user_image, :collaborator_ids])
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -16,6 +16,7 @@ class Topic < ApplicationRecord
|
||||||
belongs_to :metacode
|
belongs_to :metacode
|
||||||
|
|
||||||
before_create :create_metamap?
|
before_create :create_metamap?
|
||||||
|
after_update :after_updated
|
||||||
|
|
||||||
validates :permission, presence: true
|
validates :permission, presence: true
|
||||||
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
|
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
|
||||||
|
@ -135,4 +136,14 @@ class Topic < ApplicationRecord
|
||||||
self.link = Rails.application.routes.url_helpers
|
self.link = Rails.application.routes.url_helpers
|
||||||
.map_url(host: ENV['MAILER_DEFAULT_URL'], id: @map.id)
|
.map_url(host: ENV['MAILER_DEFAULT_URL'], id: @map.id)
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue