Sha256: bb49bdc5e3a3bbbbe29b2964a5729e946cae912b88ff4dbd578cfc3ec3f3efb1

Contents?: true

Size: 1.02 KB

Versions: 13

Compression:

Stored size: 1.02 KB

Contents

module Lanes

    module API

        module Updates
            @relaying_messages = false

            def self.user_info_for_change(model)
                if model.has_attribute?(:updated_by_id)
                    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
                Lanes::Model.observe(:save) do |model|
                    if model.changes.any?
                        path = "/#{model.class.api_path}/#{model.id}"

                        Lanes::API::PubSub.publish(path, {
                            by: self.user_info_for_change(model),
                            update: model.changes
                          })
                    end
                end

            end

        end


    end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
lanes-0.1.9.5 lib/lanes/api/updates.rb
lanes-0.1.9 lib/lanes/api/updates.rb
lanes-0.1.8 lib/lanes/api/updates.rb
lanes-0.1.7 lib/lanes/api/updates.rb
lanes-0.1.6 lib/lanes/api/updates.rb
lanes-0.1.5 lib/lanes/api/updates.rb
lanes-0.1.2 lib/lanes/api/updates.rb
lanes-0.1.0 lib/lanes/api/updates.rb
lanes-0.0.8 lib/lanes/api/updates.rb
lanes-0.0.5 lib/lanes/api/updates.rb
lanes-0.0.3 lib/lanes/api/updates.rb
lanes-0.0.2 lib/lanes/api/updates.rb
lanes-0.0.1 lib/lanes/api/updates.rb