Skip to content

Instantly share code, notes, and snippets.

@codemilan
codemilan / customize_error.rb
Created November 9, 2021 09:46 — forked from telwell/customize_error.rb
Customize Field Errors with Rails 5 and Bootstrap
# Adapted from https://rubyplus.com/articles/3401-Customize-Field-Error-in-Rails-5
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
html = ''
form_fields = [
'textarea',
'input',
'select'
]
@codemilan
codemilan / ruby_ftp_example.rb
Created November 2, 2021 09:16 — forked from 3dd13/ruby_ftp_example.rb
Sample code of using Ruby Net::FTP library. Login to FTP server, list out files, check directory existence, upload files
require 'net/ftp'
CONTENT_SERVER_DOMAIN_NAME = "one-of-the-ftp-server.thought-sauce.com.hk"
CONTENT_SERVER_FTP_LOGIN = "saucy-ftp-server-login"
CONTENT_SERVER_FTP_PASSWORD = "saucy-ftp-server-password"
# LOGIN and LIST available files at default home directory
Net::FTP.open(CONTENT_SERVER_DOMAIN_NAME, CONTENT_SERVER_FTP_LOGIN, CONTENT_SERVER_FTP_PASSWORD) do |ftp|
files = ftp.list
@codemilan
codemilan / ruby-tools-install.sh
Created January 24, 2020 07:06 — forked from edvardm/ruby-tools-install.sh
script to install pry and some other nice tools for Ruby
#!/usr/bin/env bash
#
# Simple script to setup friendly environment for beginning Linux/Mac Ruby users
#
echo "installing pry and awesome_print"
gem install -q --no-ri --no-rdoc \
pry pry-doc pry-coolline awesome_print
# Nifty pry config copied from https://github.com/dotphiles/dotphiles/blob/master/ruby/aprc
@codemilan
codemilan / gist:7018eac78297fdb00410bc4c63a0a10c
Created December 5, 2019 10:00 — forked from benshimmin/gist:9401836
How to raise a validation error in Rails after save
# This probably isn't a good thing to want to do, but it came up for me,
# so in the spirit of helping others with weird problems (and because this
# seems to be documented almost nowhere):
after_save do
if some_failing_condition
errors.add(:something, "some failure happened.")
raise ActiveRecord::RecordInvalid.new(self)
end

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example

@codemilan
codemilan / ssl_config.rb
Last active December 2, 2019 09:15
Running puma in ssl mode in development environment.
# Ref:- 'https://gist.github.com/tadast/9932075'
# Inside your rails app root directory.
# run "mkdir config/certs && touch config/certs/.keep"
# Add below two lines in .gitignore
# /config/certs/*
# !/config/certs/.keep
# Add below code in config/puma.rb
if Rails.env.development?
@codemilan
codemilan / filterable.rb
Last active June 27, 2019 04:34 — forked from justinweiss/filterable.rb
Filterable
# https://www.justinweiss.com/articles/search-and-filter-rails-models-without-bloating-your-controller/
# Call scopes directly from your URL params:
#
# @products = Product.filter(params.slice(:status, :location, :starts_with))
module Filterable
extend ActiveSupport::Concern
module ClassMethods
# Call the class methods with the same name as the keys in <tt>filtering_params</tt>
@codemilan
codemilan / c10k.ru
Created November 4, 2018 10:23 — forked from WJWH/c10k.ru
The double hijack trick, serving 65k connections from a single ruby process
# This server demo does a socket hijack in Rack and then saves the socket to a global variable
# to prevent it from being GCed when the Puma thread ends. It will then write "BEEP" to each
# socket every ten seconds to prevent the connection timing out. During testing, it easily
# handled up to 65523 connections, after which it ran into the `ulimit` for open file descriptors.
# The bit with the waiting area is there because a normal `Set` is not thread safe and it would
# drop socket due to race conditions. The `Queue` is thread safe and will make sure all sockets
# are preserved.
# run with `rackup -q -p 8000 -o 0.0.0.0 c10k.ru`
# testing: install `ab` and then run `ab -c 20000 -n 20000 <ip adress of server>:8000/
@codemilan
codemilan / introrx.md
Created August 9, 2018 04:43 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
@codemilan
codemilan / fish_shell.md
Created August 7, 2018 04:42 — forked from idleberg/fish_shell.md
Instructions on how to install Fish shell on Mac OS X, including Oh My Fish!. Also includes several useful functions.

Installation

  1. Install fish via Brew
  2. Optionally install Oh My Fish!
  3. Add fish to known shells
  4. Set default shell to fish
brew install fish  
curl -L https://get.oh-my.fish | fish