Skip to content

Instantly share code, notes, and snippets.

View secretpray's full-sized avatar
🎯
Focusing

Aleksander secretpray

🎯
Focusing
View GitHub Profile
@secretpray
secretpray / Slim-select.md
Created November 6, 2022 04:46
Rails 7 with Importmap and slim-select
  1. Addd slim-select
bin/importmap pin slim-select 

optionaly

bin/importmap pin slim-select  --download
  1. Add css (3 variants)
  • add to app/views/layouts/application.html.erb
@secretpray
secretpray / New tally method (Ruby 2.7).md
Last active February 28, 2024 16:49
New tally method (Ruby 2.7)
def search_result
  categories.each_with_object([]) do |category, keywords|
    keywords << category.keywords.split(', ').select{ |c| c.include?(query) } unless category.keywords.blank?
  end.flatten.tally.first(3).map(&:first)
end
[1, 2, 2, 3].tally
// Custom price range slider

let rangeSlider = ((containerSelector, minSelector, maxSelector, selectionSelector, inputCallback, changeCallback) => {
    inputCallback = inputCallback || function () { };
    changeCallback = changeCallback || function () { };
    let timeout;
    let sliderContainer = document.querySelector(containerSelector);
    let sliderMinElement = document.querySelector(minSelector);
    let sliderMaxElement = document.querySelector(maxSelector);
@secretpray
secretpray / Impressionist.md
Last active June 30, 2022 05:28
impressionist gem Rails 6 with Turbo and webpacker

Gemfile

gem 'impressionist',
  git: '[email protected]:charlotte-ruby/impressionist.git',
  ref: '46a582ff8cd3496da64f174b30b91f9d97e86643'

db/migrate/xxxxxxxxxxxxxxx_add_impressions_count_to_product.rb

class AddImpressionsCountToProduct < ActiveRecord::Migration[6.1]
  1. In target Stimulus Controller
connect() {
  this.element[this.identifier] = this
 }

or if name have special symbol (like auto_search_controller.js -> auto-search)

connect() {
 this.element[ (str =&gt; { return str
@secretpray
secretpray / Install Bootstrap on Rails 7 with cssbudling.md
Last active October 31, 2022 18:20
Add bootstrap to Rails 7 with cssbundling

Add bootstrap to Rails 7 with cssbundling

ruby -v

-> 3+

rails -v

-> 7+

@secretpray
secretpray / Infinity scroll.md
Last active June 26, 2022 09:05
Infinity scroll (Endless) on Ruby on Rails 7 Hotwire (Turbo)

Edition 1 simple (on Observer)

import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="infinite-scroll"
export default class extends Controller {

  initialize() {
    this.observer = new IntersectionObserver(entries => this.callback(entries))
@secretpray
secretpray / Rails routes.md
Last active March 12, 2022 10:14
Search in rails routes

Searching with Browser

You will have four outputs for each route. These outputs are the route name (if it exists), the HTTP verb, the url pattern and the routing parameters (if they are used). These four pieces of information are very useful in determining the full route including namespaces, what the components of the URI are, the action that will be performed when the endpoint is hit and what kind of parameters can be expected.

https://localhost:3000/rails/info/routes
@secretpray
secretpray / Convert DB in Rails App.md
Last active March 3, 2022 08:53
Convert sqlite3 to PostgreSQL (and other DB) in Rails

Rails 6+

bin/rails db:system:change --to=postgresql

See list of other database type below:

rails db:system:change --to=mysql, 
rails db:system:change --to=sqlite3, 
# app/views/messages/_message.html.erb
<%= message.decorate.formatted_for_room %>
#app/decorators/message_decorator.rb

class MessageDecorator < Draper::Decorator
 delegate_all