Skip to content

Instantly share code, notes, and snippets.

View tabishiqbal's full-sized avatar

Tabish Iqbal tabishiqbal

  • Toronto, Ontario
View GitHub Profile
@tabishiqbal
tabishiqbal / attached_validator.rb
Created October 26, 2023 02:27 — forked from donnfelker/attached_validator.rb
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 October 17, 2023 18:30
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>
@tabishiqbal
tabishiqbal / trix_shrine.md
Created July 20, 2023 01:25 — forked from dpaluy/trix_shrine.md
Trix and Shrine for WYSIWYG

How to use Trix and Shrine for WYSIWYG Editing with Drag-and-Drop Image Uploading

Building It

Install and Configure Shrine and Trix Dependencies

  1. Setup Shrine:

First, add the Shrine gem to your application's Gemfile:

@tabishiqbal
tabishiqbal / slug.rb
Created August 21, 2022 16:29 — forked from Merovex/slug.rb
Create random slug
# frozen_string_literal: true
module Slug
extend ActiveSupport::Concern
def self.included(base)
base.extend ClassMethods
base.before_create :set_slug
end
def to_param
@tabishiqbal
tabishiqbal / .gitattributes
Created April 2, 2022 09:22 — forked from tpope/.gitattributes
Fewer conflicts in your Rails apps
Gemfile.lock merge=bundlelock
db/schema.rb merge=railsschema
@tabishiqbal
tabishiqbal / google-sheets-dataclip-autoupdate.md
Created October 6, 2021 18:24 — forked from aGHz/google-sheets-dataclip-autoupdate.md
Auto-updating importData of Heroku Dataclips in Google Sheets

Dataclips URLs

Dataclips has a reliable way to construct the URL of a clip's CSV version:

https://dataclips.heroku.com/<hash>-<description>.csv

Thankfully the description is irrelevant, so we can just get the hash from the web interface (looks like aujqawhjdmlbbwrqxutcpzzqyika) and add -1 at the end. Every time we change the

@tabishiqbal
tabishiqbal / _form.html.erb
Last active March 1, 2024 02:33
Ruby on Rails Tom-Select Example with Stimulus controller
<%= form_with(model: team) do |form| %>
<div>
<%= form.label :name %>
<%= form.text_field :name, class: "input" %>
</div>
<div>
<%= f.select :user_id, {}, {placeholder: "Select user"}, {class: "w-full", data: { controller: "select", select_url_value: users_path }} %>
</div>
@tabishiqbal
tabishiqbal / region_reflex.rb
Last active May 3, 2021 16:10
Stimulus Reflex populate dropdowns
class RegionReflex < ApplicationReflex
def select_state
state_id = element[:value].to_i
@state = State.find(state_id)
@state_cities = @state&.cities
end
end
@tabishiqbal
tabishiqbal / categories_controller.rb
Created May 2, 2021 07:20 — forked from julianrubisch/categories_controller.rb
Dependent Select with StimulusJS
class CategoriesController < ApplicationController
def fields
render json: Field.where(category_id: params[:id]).order(:id)
end
end
@tabishiqbal
tabishiqbal / README.md
Created May 2, 2021 07:17 — forked from leastbad/README.md
Choices.js Stimulus wrapper preview

Choices.js Stimulus wrapper

https://joshuajohnson.co.uk/Choices/

Soon, this will be published as an NPM package, but there's an absence of documentation right now. It supports almost all functions from the original library; soon it will support 100% of them.

This wrapper adds Ajax pre-fetch search. Happens if controller has a data-search-path attribute.

Stimulus controller targets use new v2 syntax. Controller attaches a reference to itself on the element so that you can access the internal state from external scripts.