Skip to content

Instantly share code, notes, and snippets.

View benmacleod's full-sized avatar

Ben MacLeod benmacleod

  • Melbourne, Australia
View GitHub Profile
def password=(password)
Rails.logger.debug "Called password=#{password}"
super(password)
end
@benmacleod
benmacleod / ruby.sh
Last active December 25, 2015 03:29 — forked from anonymous/ruby.sh
sudo apt-get --force-yes install build-essential openssl libreadline6 libreadline6-dev curl \
zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev \
libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison
&&
\curl -L https://get.rvm.io | bash -s stable --autolibs=enabled
&&
@benmacleod
benmacleod / gist:6374314
Last active December 21, 2015 22:19 — forked from radar/gist:6374310
def self.find_by_id_or_first(id)
(id && where(:id => id).first) || first
end
# or...
def self.find_by_id_or_first(id)
order("id = #{id.to_i} DESC").first
end
@current_company ||= (current_user.accessible_companies.find_by_id(session[:current_company_id]) if session[:current_company_id]) || current_user.companies.first
Feature: App Jasmine specs
@javascript
Scenario: App Jasmine specs passing
Given I am on the jasmine page
Then I should not see the text "Failing"
@benmacleod
benmacleod / gist:4064150
Created November 13, 2012 05:35
Specs for validations using #send
describe "#has_no_duplicates" do
before do
account = Fabricate :account
@contact = Fabricate.build :contact, account: account, locale: account.locale
end
it "should add errors if there are duplicates" do
@contact.stub(:duplicates).and_return [mock(Contact, details: {email_address: 'foo', mobile_number: 'bar'})]
@contact.send(:has_no_duplicates)
@contact.errors.should be_present
@benmacleod
benmacleod / gist:1823672
Created February 14, 2012 05:00
Leap year agnostic day of year function for Postgres PL/SQL
CREATE OR REPLACE FUNCTION yday(time_in TIMESTAMP) RETURNS INTEGER AS $$
DECLARE
yr INTEGER;
mnth INTEGER;
yday INTEGER;
BEGIN
-- Ensure March 1st and after always have the same day of year across leap-years
-- by adding 1 to the day of year for months after Feb in non-leap years
yday := EXTRACT(doy FROM time_in);
yr := EXTRACT(year FROM time_in);
@benmacleod
benmacleod / .autotest
Created January 19, 2012 06:29
Autotest
#!/usr/bin/env ruby
#require 'redgreen/autotest'
AUTOTEST_IMAGE_PATH = File.dirname(File.symlink?(__FILE__) ? File.readlink(__FILE__) : File.expand_path(__FILE__))
module Autotest::Notify
def self.notify title, msg, img, urgency='low'
system "notify-send -c autotest -i #{img.inspect} #{title.inspect} #{msg.inspect}"
end
Autotest.add_hook :ran_command do |autotest|
@benmacleod
benmacleod / mongo.rb
Created October 25, 2011 15:53
Connection code
require 'mongo/retry'
require 'mongo/retry_replica_set_connection'
if ENV['MONGOHQ_REPLICASET_URL']
# MongoMapper.config = {RAILS_ENV => {'uri' => ENV['MONGOHQ_REPLICASET_URL']}}
parser = Mongo::URIParser.new(ENV['MONGOHQ_REPLICASET_URL']) #* begin
MongoMapper.config = {
RAILS_ENV => {
'uri' => nil,
'hosts' => parser.nodes,
@benmacleod
benmacleod / gist:1300414
Created October 20, 2011 04:26
Kill your rogue Rails server so you can start another
# Assuming it's running on port 3000
kill -9 `lsof -i :3000|awk 'NR > 1 {print $2}' -|uniq`