Skip to content

Instantly share code, notes, and snippets.

View syafiqfaiz's full-sized avatar

syafiq faiz syafiqfaiz

View GitHub Profile
@Aerlinger
Aerlinger / fg_find_or_create.rb
Last active February 3, 2017 14:35
find_or_create with factory girl
module FactoryGirl
def self.find_or_create(resource_name, attributes = {}, &block)
clazz = resource_name.to_s.classify.constantize
model = clazz.find_by(attributes) || FactoryGirl.build(resource_name, attributes)
yield(model) if block_given? && model.new_record?
model.tap(&:save)
end
@mcshakes
mcshakes / Rails C error.
Created October 14, 2016 23:45
Running into RVM and readline issues
$ rails c
Running via Spring preloader in process 7916
/Users/edmac/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require': dlopen(/Users/edmac/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/x86_64-darwin14/readline.bundle, 9): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib (LoadError)
Referenced from: /Users/edmac/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/x86_64-darwin14/readline.bundle
Reason: image not found - /Users/edmac/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/x86_64-darwin14/readline.bundle
Fixed with:
ln -s /usr/local/opt/readline/lib/libreadline.7.0.dylib /usr/local/opt/readline/lib/libreadline.6.dylib
@henrik
henrik / luhn_checksum.rb
Created November 30, 2011 17:02
Luhn checksum/check digit generation in Ruby.
class Luhn
def self.checksum(number)
digits = number.to_s.reverse.scan(/\d/).map { |x| x.to_i }
digits = digits.each_with_index.map { |d, i|
d *= 2 if i.even?
d > 9 ? d - 9 : d
}
sum = digits.inject(0) { |m, x| m + x }
mod = 10 - sum % 10
mod==10 ? 0 : mod
@cgoldberg
cgoldberg / geckodriver-install.sh
Last active August 18, 2023 10:20
download and install latest geckodriver for linux or mac (selenium webdriver)
#!/bin/bash
# download and install latest geckodriver for linux or mac.
# required for selenium to drive a firefox browser.
install_dir="/usr/local/bin"
json=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest)
if [[ $(uname) == "Darwin" ]]; then
url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("macos"))')
elif [[ $(uname) == "Linux" ]]; then
url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("linux64"))')
@briankung
briankung / docker-pry-rails.md
Last active December 12, 2023 10:40
Using pry-rails with Docker

First, add pry-rails to your Gemfile:
https://github.com/rweng/pry-rails

gem 'pry-rails', group: :development

Then you'll want to rebuild your Docker container to install the gems

@jsteiner
jsteiner / database_cleaner.rb
Created January 10, 2014 20:31
Database Cleaner
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
@syafiqfaiz
syafiqfaiz / how-to-copy-aws-rds-to-local.md
Last active February 21, 2024 06:00
How to copy production database on AWS RDS(postgresql) to local development database.
  1. Change your database RDS instance security group to allow your machine to access it.
    • Add your ip to the security group to acces the instance via Postgres.
  2. Make a copy of the database using pg_dump
    • $ pg_dump -h <public dns> -U <my username> -f <name of dump file .sql> <name of my database>
    • you will be asked for postgressql password.
    • a dump file(.sql) will be created
  3. Restore that dump file to your local database.
    • but you might need to drop the database and create it first
    • $ psql -U <postgresql username> -d <database name> -f <dump file that you want to restore>
  • the database is restored
@jonathanmoore
jonathanmoore / gist:2640302
Created May 8, 2012 23:17
Get the share counts from various APIs

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter

@fbn4sc
fbn4sc / command.txt
Created January 29, 2018 01:35
Delete all branches except master and develop.
git branch | grep -v "master\|develop" | xargs git branch -D
@justjanne
justjanne / Price Breakdown.md
Last active April 11, 2024 22:21 — forked from kylemanna/price.txt
Server Price Breakdown: DigitalOcean, Amazon AWS LightSail, Vultr, Linode, OVH, Hetzner, Scaleway/Online.net:

Server Price Breakdown: DigitalOcean, Amazon AWS LightSail, Vultr, Linode, OVH, Hetzner, Scaleway/Online.net:

Permalink: git.io/vps

$5/mo

Provider Type RAM Cores Storage Transfer Network Price