Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
CliffordAnderson / brew-install-script.sh
Last active February 14, 2024 08:29
Brew script for installing packages and applications on OSX
#!/bin/sh
# Homebrew Script for OSX
# To execute: save and `chmod +x ./brew-install-script.sh` then `./brew-install-script.sh`
echo "Installing brew..."
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
echo "Installing brew cask..."
brew tap homebrew/cask
@panmari
panmari / postgresql_random_retrieve.rb
Last active May 11, 2018 10:59
Benchmark of various methods to retrieve a random record from a table using ActiveRecord and a Postgresql db behind.
require 'benchmark'
# Disable logging if in development/test mode
ActiveRecord::Base.logger = nil
model_name = Icd
N = 100
Benchmark.bm(11) do |x|
x.report('ruby rand') { N.times { model_name.offset(rand(model_name.count)).first } }
@nicholasrq
nicholasrq / sunspot_reindex.rb
Created September 17, 2014 04:40
Method to reindex sunspot model with progress bar. You just need to add it to any of your models.
def self.reindex!
model_name = self.name.to_s
total = self.count
indexed = 0.to_f
progress = 0.to_f
row_length = 100.to_f
log_level = Sunspot::Rails::LogSubscriber.logger.level
time_start = Time.now
each_second = Time.now
per_second = 0
@AhmedElSharkasy
AhmedElSharkasy / best_practices
Last active April 8, 2019 14:53
Rails 3 AntiPatterns, Useful snippets and Recommended Refactoring. Note: Some of them are collected from different online resources/posts/blogs with some tweaks.
Rails as it has never been before :)
Rails 3 AntiPatterns, Useful snippets and Recommended Refactoring.
Note: Some of them are collected from different online resources/posts/blogs with some tweaks.
@shime
shime / readme.md
Last active March 13, 2020 09:39
bake your own code reloader for rails

Let's build our own code reloader for Rails, shall we?

Require dependency problems

Run this inside rails/railties:

$ grep -rn "eager_load_paths" .

You should get the results from Rails::Engine::Configuration and Rails::Engine. As you know, each Rails application is actually a Rails::Engine and Rails::Engine::Configuration is that thing wrapped inside Rails.application.config block.

@natritmeyer
natritmeyer / debug_httparty_request.rb
Last active November 8, 2023 15:47
How to debug HTTParty requests
class MyResource
include HTTParty
debug_output $stdout # <= will spit out all request details to the console
#...
end
@cpjolicoeur
cpjolicoeur / gist:3590737
Created September 1, 2012 23:15
Ordering a query result set by an arbitrary list in PostgreSQL

I'm hunting for the best solution on how to handle keeping large sets of DB records "sorted" in a performant manner.

Problem Description

Most of us have work on projects at some point where we have needed to have ordered lists of objects. Whether it be a to-do list sorted by priority, or a list of documents that a user can sort in whatever order they want.

A traditional approach for this on a Rails project is to use something like the acts_as_list gem, or something similar. These systems typically add some sort of "postion" or "sort order" column to each record, which is then used when querying out the records in a traditional order by position SQL query.

This approach seems to work fine for smaller datasets, but can be hard to manage on large data sets with hundreds (or thousands) of records needing to be sorted. Changing the sort position of even a single object will require updating every single record in the database that is in the same sort group. This requires potentially thousands of wri

@raedatoui
raedatoui / gist:2055952
Created March 17, 2012 07:05
active admin sort by association
### Model
class ModelA < ActiveRecord::Base
has_many :modelb
end
class ModelB < ActiveRecord::Base
belongs_to :modela
has_many :modelc
end
@brentertz
brentertz / rvm2rbenv.txt
Created November 21, 2011 23:00
Switch from RVM to RBENV
## Prepare ###################################################################
# Remove RVM
rvm implode
# Ensure your homebrew is working properly and up to date
brew doctor
brew update
## Install ###################################################################
@bkimble
bkimble / gist:1365005
Last active May 2, 2024 01:27
List local memcached keys using Ruby
#!/usr/bin/env ruby
# List all keys stored in memcache.
# Credit to Graham King at http://www.darkcoding.net/software/memcached-list-all-keys/ for the original article on how to get the data from memcache in the first place.
require 'net/telnet'
headings = %w(id expires bytes cache_key)
rows = []