Skip to content

Instantly share code, notes, and snippets.

View chrisbloom7's full-sized avatar
💭
I may be slow to respond.

Chris Bloom chrisbloom7

💭
I may be slow to respond.
View GitHub Profile
@chrisbloom7
chrisbloom7 / BeyondCompare-CrossOver.sh
Created September 29, 2010 18:05
Bash shell script to use Beyond Compare from Versions.app
#!/bin/bash
# This script will open a file compare dialog for files you select in Versions.app. This script requires
# Beyond Compare to be installed into CrossOver for Mac. Note that the BC window doesn't always open
# in the foreground nor does it appear in the Dock, so you will need to CMD-TAB to find it. If you have problems
# using this from Versions try executing the commands below in a normal Terminal window running bash and replace
# the $1 and $2 variables with complete paths to two existing files.
# Setup some variables that CrossOver will need. You should replace these with your own values which can
# be found by opening CrossOver, going to Programs -> Run Command, selecting the bottle you installed
@chrisbloom7
chrisbloom7 / mod_xsendfile_example.php
Created March 4, 2011 04:27
Example of using mod_xsendfile to send a file as a download.
<?php
/**
* In your Apache conf file, set the following to enable XSendfile:
*
<Directory "/">
# This should be handled by the XSendFile module, but a bug in 0.11.1 prevented it from being set properly.
EnableSendfile on
XSendFile on
@chrisbloom7
chrisbloom7 / php_debugging_functions.php
Created March 4, 2011 05:08
Some standard debugging functions I use in almost every project. Include this file after the main config file has been loaded.
<?php
// This should probably be ported to a custom class...
// DEBUGGING - set to TRUE to force debugging or comment out to use dynamic
// debugging. The debugging behaviour is unreliable if this is set to FALSE
// define("X_GLOBAL_DEBUG", TRUE);
// LOG_DIR needs to be defined here rather than in the local config file because we
// may need access to it from pages where the local conf hasn't been loaded yet.
define('LOG_DIR', dirname(__FILE__) . '/../../logs');
@chrisbloom7
chrisbloom7 / README.md
Created June 6, 2011 07:16
A cheap knock off of the default validates_length_of validator, but checks the filesize of a Carrierwave attachment

Note that this validation runs both after the file is uploaded and after CarrierWave has processed the image. If your base uploader includes a filter to resize the image then the validation will be run against the resized image, not the original one that was uploaded. If this causes a problem for you, then you should avoid using a resizing filter on the base uploader and put any specific size requirements in a version instead.

So instead of this:

require 'carrierwave/processing/mini_magick'

@chrisbloom7
chrisbloom7 / example_spec.rb
Created June 12, 2011 01:10
An RSpec2 matcher that asserts that only one of the passed elements is present in the source array
require 'include_only_one_of'
describe "SomeTest" do
it "should return only one wide format promotion if available" do
first_wide_promotion = Factory(:promotion, :brand => @brand, :format => Promotion::FORMATS[:wide])
second_wide_promotion = Factory(:promotion, :brand => @brand, :format => Promotion::FORMATS[:wide])
@brand.featured_promotions.should include_only_one_of(first_wide_promotion, second_wide_promotion)
end
end
@chrisbloom7
chrisbloom7 / pretty_join.php
Last active September 26, 2015 01:58
Given an array of 0 or more values, pretty_join() will concatenate all of the values together using a common delimiter (a comma by default) except for the last two values which will be joined by the final separator (an ampersand by default).
<?php
function pretty_join($array, $intermediate_separator = ', ', $final_separator = ' & ')
{
if (!is_array($array)) return null;
switch (sizeof($array))
{
case 0: return null; break;
case 1: return reset($array); break;
case 2: return implode($final_separator, $array); break;
default:
@chrisbloom7
chrisbloom7 / longest_common_substring.php
Created June 12, 2011 03:27
Find the longest common substring in an array of strings (PHP)
<?php
function longest_common_substring($words)
{
$words = array_map('strtolower', array_map('trim', $words));
$sort_by_strlen = create_function('$a, $b', 'if (strlen($a) == strlen($b)) { return strcmp($a, $b); } return (strlen($a) < strlen($b)) ? -1 : 1;');
usort($words, $sort_by_strlen);
// We have to assume that each string has something in common with the first
// string (post sort), we just need to figure out what the longest common
// string is. If any string DOES NOT have something in common with the first
// string, return false.
@chrisbloom7
chrisbloom7 / application.html.erb
Created June 14, 2011 02:55
Static named redirect routes in Rails 3
<ul class="social-links">
<li class="facebook"><%= link_to "Like us on Facebook", facebook_path %></li>
<li class="twitter"><%= link_to "Follow us on Twitter", twitter_path %></li>
</ul>
@chrisbloom7
chrisbloom7 / hash_comparison_spec.rb
Created June 29, 2011 05:54
An RSpec2 spec that demonstrates the issue reported at https://github.com/rspec/rspec-expectations/issues/81 regarding comparison of arrays of multiple hashes with different keys and/or values when using the =~ operator. See failure reports #15 and #18 in
require 'spec_helper'
describe "hash comparison tests" do
context "comparing single hashes with same keys, different values" do
let(:me) { { :foo => 'bar' } }
let(:me2) { { :foo => 'baz' } }
it "should fail gracefully when using ==" do
me.should == me2
end
@chrisbloom7
chrisbloom7 / rspec-syntax-cheat-sheet.rb
Created July 22, 2011 00:53 — forked from dnagir/rspec-syntax-cheat-sheet.rb
RSpec 2 syntax cheat sheet by example
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")