Skip to content

Instantly share code, notes, and snippets.

View rhombl4's full-sized avatar

Serhii Koliada rhombl4

View GitHub Profile
# frozen_string_literal: true
# Shortcuts for ApplicationRecord class methods
class DB < ApplicationRecord
# response = DB.q DB.s(['SELECT * FROM users WHERE name LIKE :name', name: params[:name]])
#
# instead of
#
# query = 'SELECT * FROM users WHERE name LIKE :name'
# sanitized_query = ApplicationRecord.sanitize_sql(query, name: params[:name])
@rhombl4
rhombl4 / transactions
Last active March 10, 2021 16:05
Playing with transaction
ActiveRecord::Base.after_rollback do
Rails.logger.info("Rollback happened! Class##{self.class.name}<--")
Rails.logger.info(self&.attributes)
# some additional details can be available at ActiveRecord::Base.connection.current_transaction
end
ActiveRecord::ConnectionAdapters::AbstractAdapter.set_callback :checkout, :after, ->(conn) {
puts 'Connection taken from pool'
puts conn.__id__
}

Disable HTML Form Input Autocomplete and Autofill

  1. Add autocomplete="off" onto <form> element;
  2. Add hidden <input> with autocomplete="false" as a first children element of the form.
<form autocomplete="off" method="post" action="">
    <input autocomplete="false" name="hidden" type="text" style="display:none;">
    ...
@rhombl4
rhombl4 / view_helper.rb
Created September 18, 2019 10:02
view_helper for haml
...
def view_helper
Haml::Engine.new(<<~HAML).render(self)
.form
= form_tag root_path do
= submit_tag :submit
HAML
end
@rhombl4
rhombl4 / gist:082afd4cdcfc562e20ed82636ff1957c
Created August 1, 2019 06:42
some helpful aliases for rails and bash
cat << \EOF > ~/.bash_profile
alias rails_pid='echo $(lsof -t -i:3000)'
alias kill_rails='kill -KILL $(rails_pid)'
EOF
source ~/.bash_profile
@rhombl4
rhombl4 / stack.rb
Created April 2, 2019 19:56 — forked from amiralles/stack.rb
Stack implemented in Ruby.
class Stack
class Node
attr_accessor :next, :data
def initialize data
self.data = data
self.next = nil
end
end
attr_accessor :head, :tail, :length
@rhombl4
rhombl4 / closures-in-ruby.rb
Last active March 22, 2019 20:25
Closures in Ruby from some person with a name Paul
# CLOSURES IN RUBY Paul Cantrell https://innig.net
# Email: username "paul", domain name "innig.net"
# I recommend executing this file, then reading it alongside its output.
#
# Alteratively, you can give yourself an unreasonable Ruby test by deleting all
# the comments, then trying to guess the output of the code!
#
# (Naive HR departments, please do not use that idea as a hiring quiz.)
@rhombl4
rhombl4 / gist:d2deccbfd9e893470310f70f1bb2c5b4
Created February 25, 2019 13:15
Remove four last branches
# TODO: need to improve by adding a filter for reference type "only-branch".
# Currently the result is all references including tags
#
git branch -D `git for-each-ref --sort='-committerdate' --count=4 --format='%(refname:short)'`
@rhombl4
rhombl4 / gist:1ee980e8c3344d954e2a9e54241c91db
Created December 26, 2018 14:01
regex for searching rescues with nil
```^.*rescue(\n|\ )(.|\n)*?(\ ){2,}nil\n(\ )*end$```
@rhombl4
rhombl4 / Gemfile.local
Last active October 3, 2018 07:48
Gemfile local with personalized gems
# This file will give you an opportunity to have a personal gemset and
# to enjoy your favourite gems without changing the application's Gemfile
# Usage:
# 1) copy this file to app folder
# 2) set your gems below
# 3) compile bundle `bundle install --gemfile=Gemfile.local`
# 4) Depending on what you want you can run it by different ways:
# a) for running separate commands like rails-console:
# `BUNDLE_GEMFILE=Gemfile.local bundle exec {{gem command}}`
# b) global inside app: