Skip to content

Instantly share code, notes, and snippets.

View michalkorzawski's full-sized avatar

michalkorzawski

View GitHub Profile
...
module ActiveStorage
class Engine < Rails::Engine
...
config.active_storage.variable_content_types = %w(
image/png
image/gif
# app/jobs/update_leaderboard_job.rb
class UpdateLeaderboardJob < ApplicationJob
queue_as :default
delegate :render, to: :ApplicationController
include CableReady::Broadcaster
def perform
scores = ServiceForFetchingScores.fetch
# app/channels/leaderboard_channel.rb
class LeaderboardChannel < ApplicationCable::Channel
def subscribed
stream_for 'scores'
end
end
# app/services/importers/users.rb
module Importers
class Users
include CableReady::Broadcaster
def initialize(file:)
@file = file
end
// app/javascript/channels/user_channel.js
import consumer from "./consumer";
import CableReady from "cable_ready";
consumer.subscriptions.create("UserChannel", {
received(data) {
if (data.cableReady) CableReady.perform(data.operations);
// Called when there's incoming data on the websocket for this channel
},
# app/channels/user_channel.rb
class UserChannel < ApplicationCable::Channel
def subscribed
stream_for current_user
end
end
# app/services/importers/users.rb
module Importers
class Users
include CableReady::Broadcaster
...
end
end
# app/services/importers/users.rb
module Importers
class Users
def initialize(file:)
@file = file
end
def import_csv
CSV.foreach(@file.path, headers: true) do |csv_row|
def results
cable_ready.morph(
children_only: true,
selector: '#id_of_a_dom_element',
html: render(
PaginationComponent.new(
pagy: pagy,
def results
params_hash = element.dataset['params-hash'].present? ? JSON.parse(element.dataset['params-hash']) : filtered_params
query = (element.dataset.from_search_input ? element.value : element.value.presence) || params_hash['query']
params_hash['query'] = query
pagy, collection = pagy(active_record_collection(filters), page: page)
cable_ready.morph(
children_only: true,
selector: '#id_of_a_dom_element',
html: render(