-
-
Save MaherSaif/49f6efc8e5200d6a056d1f8c7feb928f to your computer and use it in GitHub Desktop.
Broadcastable model concern/mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module Broadcastable | |
extend ActiveSupport::Concern | |
def prepend_operation(options = {}) | |
operation = {html: ApplicationController.render(partial: to_partial_path, locals: locals)} | |
operation.merge options | |
end | |
def broadcast_prepend(stream, options = {}) | |
cable_ready[stream].prepend(prepend_operation(options)).broadcast | |
end | |
def replace_operation(options = {}) | |
operation = { | |
selector: dom_id(self), | |
html: ApplicationController.render(partial: to_partial_path, locals: locals) | |
} | |
operation.merge options | |
end | |
def broadcast_replace(stream, options = {}) | |
cable_ready[stream].replace(replace_operation(options)).broadcast | |
end | |
def morph_operation(options = {}) | |
operation = { | |
selector: dom_id(self), | |
html: ApplicationController.render(partial: to_partial_path, assigns: {morph: true}, locals: locals) | |
} | |
operation.merge options | |
end | |
def broadcast_morph(stream, options = {}) | |
cable_ready[stream].morph(morph_operation(options)).broadcast | |
end | |
def remove_operation(options = {}) | |
operation = {selector: dom_id(self)} | |
operation.merge options | |
end | |
def broadcast_remove(stream, options = {}) | |
cable_ready[stream].remove(remove_operation(options)).broadcast | |
end | |
private | |
def locals(options = {}) | |
{self.class.table_name.singularize.to_sym => self}.merge(options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment