Skip to content

Instantly share code, notes, and snippets.

View sergii's full-sized avatar

Serhii Ponomarov sergii

View GitHub Profile
# app/filters/base_filter.rb
class BaseFilter
include ActiveModel::Model
include ActiveModel::Attributes
def initialize(session)
@_session = session
super(@_session.fetch(:filters, {})[filter_resource_class])
end
// This code is to be used with https://turbo.hotwire.dev. By default Turbo keeps visited pages in its cache
// so that when you visit one of those pages again, Turbo will fetch the copy from cache first and present that to the user, then
// it will fetch the updated page from the server and replace the preview. This makes for a much more responsive navigation
// between pages. We can improve this further with the code in this file. It enables automatic prefetching of a page when you
// hover with the mouse on a link or touch it on a mobile device. There is a delay between the mouseover event and the click
// event, so with this trick the page is already being fetched before the click happens, speeding up also the first
// view of a page not yet in cache. When the page has been prefetched it is then added to Turbo's cache so it's available for
// the next visit during the same session. Turbo's default behavior plus this trick make for much more responsive UIs (non SPA).
@julianrubisch
julianrubisch / 01-board.rb
Last active May 6, 2022 09:50
StimulusReflex Patterns - 1 Forms
# app/models/board.rb
class Board < ApplicationRecord
has_many :embeds
end
@swanson
swanson / .irbrc
Last active March 3, 2024 17:14
.irbrc color coding Rails console by environment
# Add color coding based on Rails environment for safety
if defined? Rails
banner = if Rails.env.production?
"\e[41;97;1m #{Rails.env} \e[0m "
else
"\e[42;97;1m #{Rails.env} \e[0m "
end
# Build a custom prompt
@seanharmer
seanharmer / membership_product.rb
Last active May 6, 2022 09:49
Example usage of new delegated_type facility
class MembershipProduct < ApplicationRecord
include Sellable
# Other stuff...
end
@seanharmer
seanharmer / gist:f0fb73170853c5628613ef90ed3b540c
Created December 10, 2020 08:36
Rails font awesome xlink
# app/helpers/application_helper.rb
module ApplicationHelper
def declare_fa_icon(options, anchor_name)
fa_icon(options, data: { 'fa-symbol': anchor_name })
end
def use_fa_icon(anchor_name, text)
%Q(<svg class="icon-fa-xs"><use xlink:href="##{anchor_name}"></use></svg>#{text}).html_safe
end
@johnmeehan
johnmeehan / index.html
Created October 18, 2020 18:34
Stimulus js Checkbox toggle
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="main.css">
<script src="bundle.js" async></script>
</head>
<body>
<div data-controller="toggler">
<label>Toggle Me</label>
@obie
obie / _alert.haml
Created October 15, 2020 03:29
Alerts in Reactive Rails style (with Shoelace web components)
%sl-alert.popup-toast{id: id, type: type, open: false, duration: duration, closable: true}
%sl-icon{slot: "icon", name: icon}
- if title.present?
%strong= title
%br
= text
@julianrubisch
julianrubisch / _article.html.erb
Last active November 9, 2020 14:34
StimulusReflex Selector Morph
<%= article.title # ... etc %>
@cschulte22
cschulte22 / _flash_notifications.html.erb
Created July 29, 2020 22:16
Rails flash with tailwind / alpinejs
<% if flash.any? %>
<div class="fixed inset-0 flex items-end justify-center px-4 py-6 pointer-events-none sm:p-6 sm:items-start sm:justify-end z-50" data-flash-notifications='true'>
<% flash.each do |msg_type, msg| %>
<div x-data="{flashVisible: false, flashType: '<%= msg_type %>'}" x-show='flashVisible' x-init="() => {flashVisible=true; setTimeout(() => {flashVisible=false}, 5000)}" class="max-w-sm w-full bg-white shadow-lg rounded-lg pointer-events-auto"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="translate-y-2 opacity-0 sm:translate-y-0 sm:translate-x-2"
x-transition:enter-end="translate-y-0 opacity-100 sm:translate-x-0"
x-transition:leave="transition ease-out duration-100"