Skip to content

Instantly share code, notes, and snippets.

View tabishiqbal's full-sized avatar

Tabish Iqbal tabishiqbal

  • Toronto, Ontario
View GitHub Profile
@donnfelker
donnfelker / attached_validator.rb
Last active October 28, 2023 02:51
Custom Active Storage Validator
class AttachedValidator < ActiveModel::EachValidator
# Active Storage validator to ensure that an attachment is attached.
#
# usage:
# validates :upload, attached: true
#
def validate_each(record, attribute, _value)
return if record.send(attribute).attached?
errors_options = {}
@tabishiqbal
tabishiqbal / _form.html.erb
Last active April 10, 2024 11:03
Ruby on Rails simple multiple select using Tom-select
<%= form_with(model: product) do |form| %>
<div>
<%= form.label :name %>
<%= form.text_field :name, class: "input" %>
</div>
<div>
<%= form.label :categories %>
<%= form.collection_select :category_ids, Category.all, :id, :name, {}, {multiple: true, id: 'category-select', class: "dropdown"} %>
</div>
@hopsoft
hopsoft / 00_do_stuff_job.rb
Last active April 18, 2024 00:50
ActiveJob as Service Worker
# ActiveJob natively captures constructor arguments in an `@arguments` instance variable
# which is also exposed as an `arguments` property on each job instance.
#
# Calls to `perform_now` and `perform_later` both forward arguments to the constructor.
#
# For example, all of these invocation styles work.
#
# result = DoStuffJob.new("foobar").perform # sync
# result = DoStuffJob.new.perform("foobar") # sync
# result = DoStuffJob.perform_now("foobar") # sync
@connorbey13
connorbey13 / application_helper.rb
Last active July 8, 2022 22:17
turbo_custom_cable_stream
module ApplicationHelper
def turbo_custom_stream_from(*streamables, **attributes)
attributes[:channel] = attributes[:channel]&.to_s || "Turbo::StreamsChannel"
attributes[:"signed-stream-name"] = Turbo::StreamsChannel.signed_stream_name(streamables)
tag.turbo_custom_cable_stream_source(**attributes)
end
end
@rlogwood
rlogwood / application.html.erb
Last active October 20, 2021 14:49
Example Stimulus Page Reloader Controller for Rails 7
<!DOCTYPE html>
<html>
<head>
<title>RefresherDemo</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
</head>
@jaredcwhite
jaredcwhite / cable_car_example.html
Last active June 9, 2021 17:31
CableCar + web component
<cable-car href="/cc/cable_car" csrf-token="94IkWdvagUHWPGVkD+VeBFVjGoM0zXAf6tgm6BPU+kEpOj2BTtI6bXJQzEDXGAF+R6Dc1ek7oNouatTMrbUy">
<button
onclick="this.closest('cable-car').post(this, {extra: 123})"
data-x="1"
output="#show-the-things"
>
Do a thing!
</button>
<section id="show-the-things"></section>
@dhh
dhh / pagination_controller.js
Last active April 24, 2024 10:53
HEY's Stimulus Pagination Controller
/*
ERB template chunk from The Feed's display of emails:
<section class="postings postings--feed-style" id="postings"
data-controller="pagination" data-pagination-root-margin-value="40px">
<%= render partial: "postings/snippet", collection: @page.records, as: :posting, cached: true %>
<%= link_to(spinner_tag, url_for(page: @page.next_param),
class: "pagination-link", data: { pagination_target: "nextPageLink", preload: @page.first? }) unless @page.last? %>
</section>
@julianrubisch
julianrubisch / turbo_frame_history_controller.js
Last active May 8, 2021 02:39
Tabbed navigation with turbo
import { navigator } from "@hotwired/turbo";
import { Controller } from "stimulus";
import { useMutation } from "stimulus-use";
export default class extends Controller {
connect() {
useMutation(this, { attributes: true, childList: true, subtree: true });
}
mutate(entries) {
// DISCLAIMER : You can now probably use `data-turbo-action="advance"` on your frame to perform what this controller is aiming to do
// https://turbo.hotwired.dev/handbook/frames#promoting-a-frame-navigation-to-a-page-visit
// Note that you probably want to disable turbo cache as well for those page to make popstate work properly
import { navigator } from '@hotwired/turbo'
import { Controller } from '@hotwired/stimulus'
import { useMutation } from 'stimulus-use'
export default class extends Controller {
connect (): void {