Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract broadcastable and trimmable to concerns #30

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions app/models/solid_cable/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@

module SolidCable
class Message < SolidCable::Record
scope :trimmable, lambda {
where(created_at: ..::SolidCable.message_retention.ago)
}
scope :broadcastable, lambda { |channels, last_id|
where(channel_hash: channel_hashes_for(channels)).
where(id: (last_id + 1)..).order(:id)
}
include Broadcastable, Trimmable

class << self
def broadcast(channel, payload)
insert({ channel:, payload:, channel_hash: channel_hash_for(channel) })
end

def channel_hashes_for(channels)
channels.map { |channel| channel_hash_for(channel) }
end
Expand Down
20 changes: 20 additions & 0 deletions app/models/solid_cable/message/broadcastable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module SolidCable
module Message::Broadcastable
extend ActiveSupport::Concern

included do
scope :broadcastable, lambda { |channels, last_id|
where(channel_hash: channel_hashes_for(channels)).
where(id: (last_id + 1)..).order(:id)
}
end

class_methods do
def broadcast(channel, payload)
insert({ channel:, payload:, channel_hash: channel_hash_for(channel) })
end
end
end
end
13 changes: 13 additions & 0 deletions app/models/solid_cable/message/trimmable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module SolidCable
module Message::Trimmable
extend ActiveSupport::Concern

included do
scope :trimmable, lambda {
where(created_at: ..::SolidCable.message_retention.ago)
}
end
end
end