Skip to content

Instantly share code, notes, and snippets.

View artificemm's full-sized avatar
🥋
Training

Roberto Ruiz artificemm

🥋
Training
View GitHub Profile
@artificemm
artificemm / titleize_fields
Created May 24, 2015 05:38
I hate UPCASE data on the DB, so I made this to prevent it
#I hate UPCASE data on the DB, so I made this to prevent it
before_save :titleize_fields
def titleize_fields
['name', 'paternal_lastname', 'maternal_lastname', 'spouse'].each do |m|
self.send("#{m}=", instance_eval(%Q{#{m}.titleize unless #{m}.nil?}))
end
end
CREATE TEXT SEARCH CONFIGURATION fr ( COPY = french );
ALTER TEXT SEARCH CONFIGURATION fr ALTER MAPPING
FOR hword, hword_part, word WITH unaccent, french_stem;
CREATE TEXT SEARCH CONFIGURATION en ( COPY = english );
ALTER TEXT SEARCH CONFIGURATION en ALTER MAPPING
FOR hword, hword_part, word WITH unaccent, english_stem;
CREATE TEXT SEARCH CONFIGURATION de ( COPY = german );
ALTER TEXT SEARCH CONFIGURATION de ALTER MAPPING
@artificemm
artificemm / loadJson.js
Created September 11, 2016 01:33 — forked from pwittchen/loadJson.js
Loading JSON file with JavaScript. Please note: it won't run as a "local" script. JavaScript does not allow to load local files due to security reasons. You should deploy it on the server in order to load JSON file.
function loadJson(callback) {
var XmlHttpRequest = new XMLHttpRequest();
XmlHttpRequest.overrideMimeType("application/json");
XmlHttpRequest.open('GET', 'file.json', true);
XmlHttpRequest.onreadystatechange = function () {
if (XmlHttpRequest.readyState == 4 && XmlHttpRequest.status == "200") {
// .open will NOT return a value
// but simply returns undefined in async mode so use a callback
callback(XmlHttpRequest.responseText);
}
@artificemm
artificemm / SOLID.markdown
Created February 22, 2017 21:13 — forked from emaraschio/SOLID.markdown
SOLID Principles with ruby examples

#SOLID Principles with ruby examples

##SRP - Single responsibility principle A class should have only a single responsibility.

Every class should have a single responsibility, and that responsibility should be entirely encapsulated. All its services should be narrowly aligned with that responsibility, this embrace the high cohesion.

##OCP - Open/closed principle Software entities should be open for extension, but closed for modification.

@artificemm
artificemm / gist:f5b66dce0c5ef6b5af6788023104b88b
Created March 28, 2019 16:19
Instrucciones para instalar Ruby en Linux(Debian/RedHat)
--- Debian
sudo apt-get --ignore-missing install build-essential git-core curl openssl libssl-dev libcurl4-openssl-dev zlib1g zlib1g-dev libreadline6-dev libyaml-dev libsqlite3-dev libsqlite3-0 sqlite3 libxml2-dev libxslt1-dev libffi-dev software-properties-common libgdm-dev libncurses5-dev automake autoconf libtool bison postgresql postgresql-contrib libpq-dev pgadmin3 libc6-dev nodejs -y
curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install 2.6.2
rvm use 2.6.2 --default

Pry Cheat Sheet

Command Line

  • pry -r ./config/app_init_file.rb - load your app into a pry session (look at the file loaded by config.ru)
  • pry -r ./config/environment.rb - load your rails into a pry session

Debugger

function calculateDistance(rssi) {
var txPower = -59 //hard coded power value. Usually ranges between -59 to -65
if (rssi == 0) {
return -1.0;
}
var ratio = rssi*1.0/txPower;
if (ratio < 1.0) {
@artificemm
artificemm / tinnova.rb
Last active March 31, 2021 22:08
Tinnova first set of exercises
require 'set'
# Solved exercises
# Roberto Ruiz
# For Tinnova
# 1:
input = [{ "name": 'eggs', "price": 1 }, { "name": 'coffee', "price": 9.99 }, { "name": 'rice', "price": 4.04 }]
def sort_hash_by_price(ary)
@artificemm
artificemm / navigable.rb
Created March 26, 2021 06:55
Nice navigation between records module
module Navigable
extend ActiveSupport::Concern
def next
self.class.where("id > ?", self.id).order(id: :asc).first
end
def previous
self.class.where("id < ?", self.id).order(id: :desc).first
end
# from_cte_sql = ::Namespace::From.where_clause
# ..
from_table = Arel::Table.new(:from_lane)
to_table = Arel::Table.new(:to_lane)
rates_table = Arel::Table.new(:rates)
rates_table
.join(from_table).on(rates_table[:from_id].eq(from_table[:id]))