Skip to content

Instantly share code, notes, and snippets.

View frullah's full-sized avatar
🎯
Focusing

Fajarullah frullah

🎯
Focusing
View GitHub Profile
@frullah
frullah / create_rails_app_file.rb
Last active July 23, 2022 04:56
Used to create rails app file with module and classes included
#!/usr/bin/env rails runner
#
# Used to create rails app file with module and classes included
#
### How to use
# - Copy this into your bin folder.
# - Make this file executable by using `chmod +x`.
sub_directory = ARGV[0]
constant_name = ARGV[1]
@frullah
frullah / rails_sql_log_parser.rb
Created March 21, 2022 17:15
read query from activerecord log
Rails.application.config.colorize_logging = false
io = StringIO.new
logger = Logger.new(io)
logger.formatter = proc do |_severity, _time, _progname, message|
sql = message.sub(/^\s*[A-Za-z0-9]+ Load \(\d+\.\d+ms\)\s+/, "").chomp
sql = sql.sub(/^\s*\(\d+\.\d+ms\)\s+/, "").chomp
IO.popen("sql-formatter", "r+") do |pipe|
pipe.puts sql
pipe.close_write
@frullah
frullah / script.sh
Last active March 15, 2022 17:09
shutdown until process finishes
# shutdown until process finishes
#
## arguments
# first argument is a PID
pid=$1
while true
do
if ! ps -p $pid > /dev/null
## Utilities
```
gem install rubocop
```
@frullah
frullah / vite-stimulus-loading.js
Last active October 24, 2021 10:07
vite stimulus lazy loader
// this code is used for lazy loading stimulus controller with Vite
// Vite lazy import only works with relative path
// so place this code in stimulus controllers folder
// reference: https://github.com/vitejs/vite/issues/1931#issuecomment-868310388
// this code is originally from https://github.com/hotwired/stimulus-rails/blob/49cfc34beb92ad56140a78db6be4c7733207be30/app/assets/javascripts/stimulus-loading.js
const controllerAttribute = "data-controller"
const registeredControllers = {}
@frullah
frullah / active_admin.js
Last active October 27, 2021 06:44
using Rails activeadmin with Vite bundler
// required dependencies
// - jquery
// - jquery-ui
// - @activeadmin/activeadmin
import jQuery from 'jquery'
window.jQuery = jQuery
window.$ = jQuery

change resource name in sidebar

define method plural_name that returns String

Hide resource in sidebar

set self.visible_on_sidebar = true in Resource

@frullah
frullah / dynamic_form_controller.js
Last active September 16, 2021 09:14
Stimulus JS dynamic form
import { Controller } from 'stimulus'
// used for changing form fields dynamically based on what user want in one page
export default class extends Controller {
static values = { targetContainerId: String, templateId: String }
async connect() {
// store array of DocumentFragment by id
this.templates = {}
// store container elements by id
@frullah
frullah / scaffold-docs.md
Last active July 19, 2021 13:04
Rails scaffold helpers docs

Rails Scaffold

Template

if you use erb, template files should be in lib/templates/erb/scaffold

if you use slim, template files should be in lib/templates/slim/scaffold

the template file will evaluate erb tag, if you don't want to evaluate it, use double % (<%% ... %%>)

@frullah
frullah / setup.sh
Last active June 25, 2021 18:40
oh my zsh syntax plugins
#!/bin/zsh
# install auto suggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# install syntax highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
sed -z --regexp-extended 's/(plugins=\(.*)\)/\1\tzsh-autosuggestions\n\tzs-syntax-highlighting\n)/g' ~/.zshrc