-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Channel model to track subscribers
- Loading branch information
Showing
12 changed files
with
87 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidCable | ||
class Channel < SolidCable::Record | ||
scope :for, ->(channel) { where(channel_hash: channel_hash_for(channel)) } | ||
|
||
def increment_subscribers! | ||
update!(subscribers: subscribers + 1) | ||
end | ||
|
||
def decrement_subscribers! | ||
update!(subscribers: [ subscribers - 1, 0 ].max) | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module ChannelHash | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def channel_hashes_for(channels) | ||
channels.map { |channel| channel_hash_for(channel) } | ||
end | ||
|
||
# Need to unpack this as a signed integer since Postgresql and SQLite | ||
# don't support unsigned integers | ||
def channel_hash_for(channel) | ||
Digest::SHA256.digest(channel.to_s).unpack1("q>") | ||
end | ||
end | ||
end |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Description: | ||
Adds Solid Cable channel migration | ||
|
||
Example: | ||
bin/rails generate solid_cable:add_channels |
15 changes: 15 additions & 0 deletions
15
lib/generators/solid_cable/add_channels/add_channels_generator.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails/generators" | ||
require "rails/generators/active_record" | ||
|
||
class SolidCable::AddChannelsGenerator < Rails::Generators::Base | ||
include ActiveRecord::Generators::Migration | ||
|
||
source_root File.expand_path("templates", __dir__) | ||
|
||
def copy_files | ||
migration_template "db/migrate/create_channels.rb", | ||
"db/cable_migrate/create_channels.rb" | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
lib/generators/solid_cable/add_channels/templates/db/migrate/create_channels.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateChannels < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table "solid_cable_channels", force: :cascade do |t| | ||
t.integer "channel_hash", limit: 8, null: false | ||
t.integer "subscribers", default: 0, null: false | ||
t.datetime "created_at", null: false | ||
t.index ["channel_hash"], name: "index_solid_cable_channels_on_channel_hash" | ||
end | ||
end | ||
end |
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
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
Binary file not shown.
Empty file.