Skip to content

Instantly share code, notes, and snippets.

View secretpray's full-sized avatar
🎯
Focusing

Aleksander secretpray

🎯
Focusing
View GitHub Profile
@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

Для интеграции ChatGPT в приложение Rails вам потребуются следующие шаги:

  1. Получите API-ключ GPT, откройте сайт https://openai.com/ и зарегистрируйте аккаунт, елкгда будут инструкции по получению ключа.

  2. Добавьте библиотеку 'httparty' в файл Gemfile вашего приложения и запустите

bundle install
  1. Создайте класс в папке 'lib' вашего приложения для обращения к сервису GPT:
@secretpray
secretpray / Horse animation(Bootstrap and svg).md
Last active March 31, 2023 04:23
Horse animation with Bootstrap and svg

view.html.erb

<div class="horse-wrapper position-absolute">
  <%= show_svg('horse.svg') %>
</div>

horse.svg

<svg class="horse" width="50pt" height="50pt" viewBox="0 0 512 512" version="1.1" xmlns="http://www.w3.org/2000/svg">
# Helpers for better embedding and manipulation of videos
# Place this code in app/helpers/videos_helper.rb
# Then from any view you can add:
#
#    <%= get_video_iframe('http://the.video.url') %>
#
# Optionally you can add width and height.
#
#    <%= get_video_iframe('http://the.video.url', '1600px', '900px') %>
@secretpray
secretpray / Elastic search in Docker of Rails 7 development.md
Created December 14, 2022 09:55
Elastic search in Docker of Rails 7 development

Universal docker method:

  1. start docker container:
docker run -d -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.17.8
  1. check Elasticsearch docker
curl http://localhost:9200

or in browser

 fetch(href, {
      headers: {
        Accept: "text/vnd.turbo-stream.html",
      },
    })
      .then(r => r.text())
      .then(html => Turbo.renderStreamMessage(html))
//  optionally reflect the url    .then(_ => history.replaceState(history.state, "", href))
  1. memoize.rb
module Memoize
  def delegate_to_values(*keys)
    keys.each do |key|
      define_method(key) do  
        return instance_variable_get(:@bar_cached) if instance_variable_defined?(:@bar_cached)

        instance_variable_set(:@bar_cached, rand)
 end