Skip to content

Instantly share code, notes, and snippets.

Diego E. Salazar DiegoSalazar

View GitHub Profile
@DiegoSalazar
DiegoSalazar / prompt_func
Created Dec 5, 2011
terminal bash prompt
View prompt_func
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
@DiegoSalazar
DiegoSalazar / gist:1726679
Created Feb 3, 2012
pre-commit hook to alert if a certain string found in code
View gist:1726679
#!/bin/sh
RED="\033[0;31m"
WHITE="\033[1;37m"
if git diff-index -p -M --cached HEAD | grep '#debug' > /dev/null; then
#echo 'debug lines found in commit. Aborting' >&2
#exit 1
echo "\n\t${RED}You left a debug comment in the code!${WHITE}\n" >&2
fi
View example output
attachments: parent_id, asset_id
domain_names: organisation_id
event_memberships: user_id, event_id
events: editor_id
group_actions: user_id, group_id
groups: user_id
icons: parent_id
invitations: sender_id
legacy_actions: item_upon_id
news_items: author_id
View gist:3149112
named_scope :with_over_lapping_dates, lambda {|starting_date, ending_date| {:conditions => ["? BETWEEN `start_date` AND `end_date` OR ? BETWEEN `start_date` AND `end_date`", starting_date, ending_date]}}
View order_spoof_finder.rb
class DasSpoofenSpleiza
def initialize(product_id)
@product = Product.find product_id, :include => :variants
end
def filter
start = Time.now
puts "\nGetting all order_items for product #{@product.id}..."
order_items = OrderItem.all({
:joins => [:order],
@DiegoSalazar
DiegoSalazar / validate_credit_card.js
Last active Jul 31, 2020
Luhn algorithm in Javascript. Check valid credit card numbers
View validate_credit_card.js
// Takes a credit card string value and returns true on valid number
function valid_credit_card(value) {
// Accept only digits, dashes or spaces
if (/[^0-9-\s]+/.test(value)) return false;
// The Luhn Algorithm. It's so pretty.
let nCheck = 0, bEven = false;
value = value.replace(/\D/g, "");
for (var n = value.length - 1; n >= 0; n--) {
View exact_squares.rb
# A program that takes any single number (max) and returns all the numbers below (max) that is an exact square
def exact_squares(max)
s = []
while max > 0
s << max if is_exact_square? max
max -= 1
end
View mini_fizzbuzz.rb
# A program that takes any single number (max) and returns all the numbers below (max) that are divisible by five or two but not by both five and two.
def mini_fizzbuzz(max)
s = []
max.downto 0 do |i|
s << i if (i % 2 == 0 || i % 5 == 0) && !(i % 2 == 0 && i % 5 == 0)
end
s
end
@DiegoSalazar
DiegoSalazar / .bash_profile
Created Oct 9, 2013
bashprofile prompt with git branch
View .bash_profile
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
@DiegoSalazar
DiegoSalazar / select_has_many_through.rb
Last active Dec 28, 2015
Using a drop down to add a relationship between has_many :through models.
View select_has_many_through.rb
# models
class Genotype < ActiveRecord::Base
has_many :genotypes_colors
has_many :colors, through: :genotypes_colors
# to post nested attributes in a form using fields_for
accepts_nested_attributes_for :genotypes_colors
# if you're using Rails 3 attr_accessible
attr_accessible :genotypes_colors_attributes, ...rest of accessible attribs
You can’t perform that action at this time.