Skip to content

Instantly share code, notes, and snippets.

View openfirmware's full-sized avatar

James Badger openfirmware

View GitHub Profile
# Potential solution to "Using accepts_nested_attributes_for with a belongs_to association, and using find_or_create_by_attr behaviour"
# http://stackoverflow.com/questions/2970255/using-accepts-nested-attributes-for-with-a-belongs-to-association-and-using-find
class Upload < AR:B
belongs_to :user
belongs_to :observed_property
belongs_to :sensor
attr_accessor :observed_property_attributes,
:sensor_attributes
attr_accessible :observed_property_attributes,
@openfirmware
openfirmware / 6147-patch.diff
Created December 16, 2010 02:01
[PATCH] Fix find_or_create_by_x on association bug [#6147 state:resolved]
From 12c8326e8c6c1f21a6b56f4d0a5fcdb932d0fa56 Mon Sep 17 00:00:00 2001
From: James Badger <Jamesbadger@gmail.com>
Date: Wed, 15 Dec 2010 15:55:51 -0700
Subject: [PATCH] Fix find_or_create_by_x on association bug [#6147 state:resolved]
Commit fdfc8e3b9c4905057677fd009f463a377be60b93 fixed the
find_or_create_by_x find() being confused by the extra parameters,
but the create_by_x code from b64d1fe637d16916c59e2ddec403d9c7cd54b0f8
does not convert the array properly for create(). This patch allows
sending of params with additional hash attributes by changing the
@openfirmware
openfirmware / gist:747804
Created December 19, 2010 23:03
Return iTunes Selection
(*
Return the quoted paths of the currently selected tracks in iTunes
Returns error if iTunes is not running
Call from command line with 'osascript -e itunes_selection.scpt'
*)
on run
tell application "System Events"
if (name of processes) contains "iTunes" is false then
return "Error: iTunes is not running"
@openfirmware
openfirmware / gist:748778
Created December 20, 2010 18:32
A really quick and dirty script to parse log files, look for errors, and send a report to multiple email addresses
#!/usr/bin/env ruby
require 'date'
require 'net/smtp'
# Who to send the report to
RECIPIENTS = %w( me@example.com )
# Who the message will be sent as
FROM_ADDRESS = "root@example.com"
@openfirmware
openfirmware / gs_dedupe_queue.js
Created June 11, 2011 22:37
Remove duplicate songs from Grooveshark queue. Keeps first instance of song. Compares using SongID, so will not catch different uploads for the same song. Tested with Google Chrome 12, Safari 5, and Firefox 4.
song_hash = {};
removal_list = [];
for(var i = 0; i < window.GS.player.queue.songs.length; i++) {
var song = window.GS.player.queue.songs[i];
if(song_hash[song["SongID"]] == undefined) {
song_hash[song["SongID"]] = 1;
} else {
removal_list.push(song["queueSongID"]);
}
}
@openfirmware
openfirmware / check_rsnapshot.rb
Created October 13, 2011 20:46
Nagios check rsnapshot
#!/usr/bin/env ruby
#
# Nagios check for rsnapshot activity and errors
# Author: James Badger <jamesbadger@gmail.com>
#
# == Documentation
#
# This script will check rsnapshot's log file to determine if it is actively
# running and that it is running without errors.
#
When /^I send a GET request for "([^"]*)"$/ do |path|
get path
end
Then /^the response code should be "([^"]*)"$/ do |code|
last_response.status.should == code.to_i
end
When /^I send a GET request for "([^"]*)"$/ do |path|
get path
end
When /^I send more than one GET request in a second to "([^"]*)"$/ do |path|
# We'll assume this happens in < 1 second
get path
get path
end
Before "@no-throttle" do
@app = Rack::Builder.new {
map "/api" do
run proc { |env|
[200, {}, ["OK"]]
}
end
}
end
@openfirmware
openfirmware / passenger_standalone.rb
Created March 9, 2012 18:30 — forked from reu/passenger_standalone.rb
Capistrano recipe for passenger standalone
set :rails_env, "production"
set :passenger_port, 9292
set :passenger_cmd, "#{bundle_cmd} exec passenger"
namespace :deploy do
task :start, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path} && #{passenger_cmd} start -e #{rails_env} -p #{passenger_port} -d"
end
task :stop, :roles => :app, :except => { :no_release => true } do