Class: LiveRecord::PublicationsChannel
- Inherits:
-
BaseChannel
- Object
- ActionCable::Channel::Base
- BaseChannel
- LiveRecord::PublicationsChannel
- Defined in:
- app/channels/live_record/publications_channel.rb
Overview
This channel streams new records to connected clients whenever the “where” condition supplied by the client matches This implementation can be quite inefficient because there’s only one pub-sub queue used for each model, but because of constraints ( see github.com/jrpolidario/live_record/issues/2 ) and because Users are authorised-validated anyway in each stream, then there’s already an overhead delay. I am prioritising development convenience (as Rails does), in order to achieve a simpler API; in this example, it would be something like in JS: ‘LiveRecord.Model.all.Book.subscribe(is_enabled: true)`
Defined Under Namespace
Modules: SearchAdapters
Instance Method Summary collapse
Instance Method Details
#subscribed ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/channels/live_record/publications_channel.rb', line 9 def subscribed model_class = params[:model_name].safe_constantize if !(model_class && model_class < ApplicationRecord) respond_with_error(:bad_request, 'Not a correct model name') reject_subscription end stream_from "live_record:publications:#{params[:model_name].underscore}", coder: ActiveSupport::JSON do || newly_created_record = model_class.find(['attributes']['id']) @authorised_attributes ||= (newly_created_record, current_user) active_record_relation = SearchAdapters.mapped_active_record_relation( model_class: model_class, conditions_hash: params[:where].to_h, current_user: current_user, authorised_attributes: @authorised_attributes ) if active_record_relation.exists?(id: newly_created_record.id) # if not just :id if @authorised_attributes.size > 1 = { 'action' => 'create', 'attributes' => ['attributes'] } response = (, @authorised_attributes) transmit response if response.present? end end end end |
#sync_records(data) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/channels/live_record/publications_channel.rb', line 40 def sync_records(data) params = data.symbolize_keys model_class = params[:model_name].safe_constantize if model_class && model_class < ApplicationRecord records = model_class.where( 'created_at >= ?', DateTime.parse(params[:stale_since]) - LiveRecord.configuration.sync_record_buffer_time ) # we `transmmit` a message back to client for each matching record records.find_each do |record| # now we check each record if it is part of the "where" condition = (record, current_user) active_record_relation = SearchAdapters.mapped_active_record_relation( model_class: model_class, conditions_hash: params[:where].to_h, current_user: current_user, authorised_attributes: ) if active_record_relation.exists?(id: record) = { 'action' => 'create', 'attributes' => record.attributes } response = (, ) transmit response if response.present? end end else respond_with_error(:bad_request, 'Not a correct model name') reject_subscription end end |