Skip to content

Instantly share code, notes, and snippets.

View guihatano's full-sized avatar
🏠
Working from home

Guilherme Y. Hatano guihatano

🏠
Working from home
View GitHub Profile
@guihatano
guihatano / cheatsheet.rb
Created December 6, 2023 14:15 — forked from mabenson00/cheatsheet.rb
Rails ActiveRecord JSON cheatsheet
# Basic key operators to query the JSON objects :
# #> : Get the JSON object at that path (if you need to do something fancy)
# -> : Get the JSON object at that path (if you don't)
# ->> : Get the JSON object at that path as text
# {obj, n} : Get the nth item in that object
# https://www.postgresql.org/docs/9.4/functions-json.html#FUNCTIONS-JSONB-OP-TABLE
# Date
# date before today
@guihatano
guihatano / .bashrc
Created August 30, 2021 14:39
git bash show branch name
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\e[01;32m\]\u@\h \[\e[01;34m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]$ "
@guihatano
guihatano / depends_on.js
Created August 28, 2020 00:10 — forked from masonjm/depends_on.js
Show/Hide form fields based on the value in another field.
(function( $ ){
$.fn.dependsOn = function(element, value) {
var elements = this;
var hideOrShow = function() {
var $this = $(this);
var showEm;
if ( $this.is('input[type="checkbox"]') ) {
showEm = $this.is(':checked');
} else if ($this.is('select')) {
@guihatano
guihatano / load_yml_with_env.rb
Last active March 12, 2020 12:49
Use ENV on .yml files
require 'dotenv/load'
configuration_file = File.join(File.expand_path('..', __FILE__), 'config.yml')
configuration = YAML.load(File.read(configuration_file).gsub(/\<\%.*\[\'([^\']+)\'\].*\%\>/) { ENV[$1] } )
@guihatano
guihatano / mysql-docker.sh
Created June 14, 2019 14:30 — forked from spalladino/mysql-docker.sh
Backup and restore a mysql database from a running Docker mysql container
# Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
# Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
@guihatano
guihatano / download_pics.sh
Last active June 5, 2019 01:41
script to download pictures from an event
#!/bin/bash
#kendo_start 8866
#kendo_last 9079
#8814
initial_pic=8866
#last_pic=9181
last_pic=9079
url="http://www.saocarlos.sp.gov.br/images/stories/abril2019/MESDASARTESMARCIAIS/DSC_"
@guihatano
guihatano / 01-safe-download.rb
Created January 14, 2019 20:06 — forked from janko/01-safe-download.rb
A safe way in Ruby to download a file to disk using open-uri (with/without comments)
require "open-uri"
require "net/http"
Error = Class.new(StandardError)
DOWNLOAD_ERRORS = [
SocketError,
OpenURI::HTTPError,
RuntimeError,
URI::InvalidURIError,
@guihatano
guihatano / postgres_docker_restore.md
Last active April 13, 2020 17:19
Restore database with pg_restore

pg_restore --verbose --clean --no-acl --no-owner -n public -h db -U loldesign -d mcr_development --port 5432 ./dbdump

On Docker

docker-compose run mcr pg_restore --verbose --clean --no-acl --no-owner -n public -h db -U loldesign -d mcr_development --port 5432 ./dbdump

With text .sql bkp

cat your_dump.sql | docker exec -i your-db-container psql -U postgres

Backup database:

@guihatano
guihatano / Rails-RestClient-with-mailgun.rb
Last active April 10, 2018 23:24
Rails using RestClient to call mailgun with attachment. When I was searching I just found same ways using RestClient::Request.execute, so I'm sharing this other way
require 'json'
result = RestClient.post "https://api:key-xxxxxxxxx-xxxxxxx@api.mailgun.net/v2/domain.com/",
:from => "YourDomain <your-email@domain.com>",
:to => "to-email@email.com",
:subject => "Mailgun with attachment",
:text => "Hello, Put something here",
:multipart => true,
:attachment => File.new('path/to/file', 'rb')
@guihatano
guihatano / divide_text_helper.rb
Last active September 22, 2017 17:30
Ruby On Rails code used to divide a html text with <p> tags. It is just a piece of code, you can copy and paste in a helper.
# After some research on how to divide a text with html tags my final solution is here
# this should work with strings.
# If you found a better solution, please let me know.
def divided_text(text)
# we have the start index of </p>, so we need do +3 to get the end
middle_index = middle_paragraph_index(text)+3
first_text = text[0..middle_index]
# and here we have the '>' so we need do +1
second_text = text[middle_index+1..-1]