Sha256: def7baba5e0e83f1ed5ac9d22c9f94f573c016a9c81ae34d119d6bb89f877193

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module Hippo

    module API

        module Updates
            @relaying_messages = false

            def self.user_info_for_change(model)
                if model.class.reflect_on_aggregation(:updated_by)
                    model.updated_by.as_json(only:[:id,:name,:email])
                else
                    {}
                end
            end

            def self.relay!
                if @relaying_messages
                    raise "Already relaying messages, calling relay! twice will lead to message duplication"
                end
                @relaying_messages = true
                Hippo::Model.observe(:save) do |model|
                    if model.saved_changes.any?
                        path = "#{model.class.api_path(with_module: true)}/#{model.id}"
                        Hippo::API::PubSub.publish(path, {
                            by: self.user_info_for_change(model),
                            update: model.saved_changes
                        })
                    end
                end

            end

        end


    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hippo-fw-0.9.5 lib/hippo/api/updates.rb