Skip to content

Instantly share code, notes, and snippets.

View johngrimes's full-sized avatar
🚴
coding and cycling

John Grimes johngrimes

🚴
coding and cycling
View GitHub Profile
@johngrimes
johngrimes / format-mount-fs.sh
Last active August 29, 2015 13:56
Format and mount a new filesystem in Linux
# Get the device identifier of the device, which is the one that currently does not have a valid partition table.
fdisk -l
# Use fdisk to create a new primary Linux partition, which takes up all of the available space.
fdisk /dev/sdb
# Make a new ext4 filesystem within the new partition.
mkfs -t ext4 /dev/sdb1
# Get the UUID of the new filesystem.
@johngrimes
johngrimes / alter-owner.sql
Created May 21, 2014 02:23
Change the owner of all relations within a PostgreSQL database
SELECT exec('ALTER TABLE ' || quote_ident(s.nspname) || '.' || quote_ident(s.relname) || ' OWNER TO $NEWUSER')
FROM (SELECT nspname, relname
FROM pg_class c JOIN pg_namespace n ON (c.relnamespace = n.oid)
WHERE nspname NOT LIKE E'pg\\_%' AND
nspname <> 'information_schema' AND
relkind IN ('r','S','v') ORDER BY relkind = 'S'
) s;
@johngrimes
johngrimes / javascript_livevalidation.js
Created May 5, 2009 13:07
Validating email addresses using JavaScript
var email = new LiveValidation('email');
email.add(Validate.Presence);
email.add(Validate.Format, { pattern: /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i });
@johngrimes
johngrimes / php_timing.php
Created May 5, 2009 14:02
Timing PHP code
$timeStart = microtime(true);
/* The code that you want to time */
$timeEnd = microtime(true);
$duration = $timeEnd - $timeStart;
@johngrimes
johngrimes / load_seed_data_from_fixtures.rb
Created June 8, 2009 21:04
Rake task to load seed data from YAML files
namespace :seed_data do
desc 'Load seed data into the database of the current environment'
task :load => :environment do
require 'active_record/fixtures'
Dir.glob(RAILS_ROOT + '/db/seed_data/*.yml').each do |file|
Fixtures.create_fixtures('db/seed_data', File.basename(file, '.*'))
end
end
end
@johngrimes
johngrimes / deploy.rb
Created April 22, 2010 09:55
Capistrano recipe for Rails app
require 'bundler/capistrano'
require 'rvm/capistrano'
set :application, 'myapp'
set :repository, 'git@github.com:johngrimes/myapp.git'
set :scm, 'git'
set :bundle_flags, '--deployment'
set :bundle_without, []
@johngrimes
johngrimes / image_optimisation.sh
Created June 3, 2010 04:51
Optimise JPEG and PNG images
find public/images -name '*.png' | xargs optipng
find public/images -name '*.jpg' | xargs jpegoptim
@johngrimes
johngrimes / resync_id_sequences.sql
Created October 6, 2010 22:32
Useful function for resetting sequence values on ID fields after importing data into PostgreSQL.
CREATE OR REPLACE FUNCTION resync_id_sequences() RETURNS void AS $$
DECLARE
table RECORD;
sequence_name VARCHAR;
id_present RECORD;
max_id RECORD;
BEGIN
FOR table IN SELECT * FROM information_schema.tables WHERE table_schema = 'public' LOOP
SELECT COUNT(*) AS count INTO id_present
FROM pg_attribute a INNER JOIN pg_class c
@johngrimes
johngrimes / newsyslog.conf
Created January 8, 2014 00:25
Keep development and test log files to a manageable size on your Mac
# Put this into /etc/newsyslog.conf, or into a .conf file under /etc/newsyslog.d/
# Format: [logfile_name] [owner:group] [mode] [count] [size] [when] [flags] [path_to_pid_file] [signal_number]
# See https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/newsyslog.conf.5.html#//apple_ref/doc/man/5/newsyslog.conf
# This trims any log file in this directory that has exceeded 10 MiB in size.
# The G flag allows you to use globbing in the logfile_name field.
/path/to/your/log/files/*.log user:group 640 0 10486 * G
def index
@areas = Area.all
@area = Area.find_by(slug: params[:area_slug]).presence || Area.find_by(slug: 'city')
conditions = { area: @area.subtree }
conditions.merge! params.select { |k,v|
[:bedrooms, :unit].include?(k) and !v.blank?
}
conditions[:property_type] = params[:type] || 'residential'