Skip to content

Instantly share code, notes, and snippets.

View secretpray's full-sized avatar
🎯
Focusing

Aleksander secretpray

🎯
Focusing
View GitHub Profile
@secretpray
secretpray / single_database_for_solid_cable.md
Last active March 23, 2025 06:36
Solid Cable in single database (Ruby on Rails 8)

Single Database Configuration steps:

rails g migration singlify_solid_cable (or whatever you want to call it)

Copy the contents of db/cable_schema.rb into the migration. Mine ended up looking like this:

class SinglifySolidCable < ActiveRecord::Migration[8.0]
  def change
    create_table "solid_cable_messages", force: :cascade do |t|
      t.binary "channel", limit: 1024, null: false
@secretpray
secretpray / Rails Stimulus timezone.md
Created September 16, 2024 07:11
Rails 7-8 Add Timezone support

app/javascript/controllers/timezone_controller.js

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
    static targets = ["time"]

    connect() {
        this.convertToLocalTime();
 }
@secretpray
secretpray / Add custom fonts to Rails, Tailwind.md
Created July 28, 2024 04:28
Add custom fonts to Rails, Tailwind

Prepare

Download your font. Create a new folder in the 'public' folder named 'fonts' and place your font in it. It might look something like this: public/fonts/Broken.otf.

  1. app/assets/stylesheets/application.tailwind.css
@layer components {
 @font-face {

Building

  1. Робите окрему папку (поки для простоти так, потім коли розберетесь зрозумієте що можна по-іншому)

  2. Створюєте Dockerfile в тій папці

  3. В Dockerfile пишете щось виду

FROM ubuntu:22.04
@secretpray
secretpray / Add masked_email and display_name to Rails User model.md
Created January 21, 2024 15:40
Add masked_email and display_name to Rails User model
  def display_name
    Rails.cache.fetch("profile_#{id}_display_name", expires_in: 1.month) do
      nickname_from_email
    end
  end

  def masked_email
 Rails.cache.fetch("profile_#{id}_masked_email", expires_in: 1.month) do
@secretpray
secretpray / Integration of TinyMCE in Rails 7 with Turbo Drive Support.md
Created January 13, 2024 23:16
Integration of TinyMCE in Rails 7 with Turbo Drive Support

Integration of TinyMCE in Rails 7 with Turbo Drive Support

Since Rails 7 the turbo-rails library is included by default. Turbo Drive intercepts link clicks and form submits. It makes sure that only the <body> part of the page is rerendered instead of the whole page.

This leads to TinyMCE not being properly detached and reattached when a Turbo Drive response is rendered. The textarea will appear without the TinyMCE editor. In this post we expand on the tinymce-rails gem with a Stimulus controller to prevent this issue. The controller helps to reattach TinyMCE and respects the settings in config/tinymce.yml.

If you want to follow along or check out the end result you can find an example respository here: https://github.com/david-uhlig/example-tinymce-rails7-turbo

Instructions

@secretpray
secretpray / Animate Tailwind dropdown in Rails 7 ( importmap).md
Created November 19, 2023 20:04
Animate Tailwind dropdown in Rails 7 ( importmap)
  1. config/importmap.rb
# Pin npm packages by running ./bin/importmap

pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "https://ga.jspm.io/npm:@hotwired/[email protected]/dist/stimulus.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin "stimulus-use", to: "https://ga.jspm.io/npm:[email protected]/dist/index.js"
@secretpray
secretpray / Create an avatar from initials or by importing from oauth.md
Created November 12, 2023 18:27
Create an avatar from initials or by importing from oauth
  1. Gemfile
gem 'rmagick'

and

bundle
  1. app/jobs/avatar_creation_job.rb
@secretpray
secretpray / 5 star rating Stimulus component (Rails 6+).md
Last active February 26, 2025 17:23
5 star rating Stimulus component (Rails 6+).md
clip.mp4

Stimulus Controller (name: 'star-rating')

import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
  static values = {
    createInput: { type: Boolean, default: false },
    inputName: { type: String, default: "rating" },
@secretpray
secretpray / Autorize broadcast object (method 1).md
Last active October 5, 2023 06:56
Autorize broadcast object (method 1) Rails 6-7 Turbo

Model Comment

class Comment < ApplicationRecord
  belongs_to :user
  has_rich_text :content

  after_create_commit do
    broadcast_append_to :comments, target: "comments", partial: "comments/comment_for_stream", locals: { comment: self }
    update_counter
  end