Skip to content

Instantly share code, notes, and snippets.

View secretpray's full-sized avatar
🎯
Focusing

Aleksander secretpray

🎯
Focusing
View GitHub Profile

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/stimulus@3.2.1/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:stimulus-use@0.52.0/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, 2024 15:55
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
@secretpray
secretpray / Autohide flash message (Rails 7, Tailwind, Stimulus).md
Last active June 11, 2024 13:50
Autohide flash message (Rails 7, Tailwind, Stimulus)
clip.mp4

app/views/layouts/application.html.erb

  <body class="min-h-screen relative">
    <%= render "layouts/flash" %>
  </body>

app/views/layouts/_flash.html.erb

@secretpray
secretpray / Add Swiper support for Bootstrap 5 (Rails 6 webpack).md
Last active April 25, 2023 06:34
Adding touchscreen gesture support for Bootstrap 5 Tab switching in a Ruby on Rails 6 with Turbo Hotwire/Stimulus application

Stimulus controller (name: 'content-swipe-tab' )

import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
  static targets = ["tabs", 'tabsPane', "swipeArea"]
  //[TODO] перенести у static values
  static swipeThreshold = 100 // Пороговое значение для определения свайпа (чтобы мелкие движения не учитывались)
  startX = null
  startY = null
@secretpray
secretpray / Struct, OpenStruct, Class, Hash, OrderedOptions.md
Created April 17, 2023 07:26
How OpenStruct and OrderedOptions Can Kill Performance
require "active_support/ordered_options"
require 'benchmark/ips'
require 'ostruct'

data = { x: 100, y: 200 }

PointStruct = Struct.new(:x, :y)
point_options = ActiveSupport::OrderedOptions.new