Skip to content

Instantly share code, notes, and snippets.

View adamrobbie's full-sized avatar

Adam Robbie adamrobbie

View GitHub Profile
module MiniTest
module Assertions
module ActiveRecord
# assert_association User, :has_many, :editables, :polymorphic => true
#
def assert_association(clazz, association, associate, options={})
reflected_assoc = clazz.reflect_on_association(associate)
@adamrobbie
adamrobbie / string_to_bool.rb
Created January 18, 2013 15:17
Adds a to_bool method to Ruby's primitive String class, to convert to a boolean.
class String
def to_bool
return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i)
raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
end
end
@adamrobbie
adamrobbie / gist:4235157
Created December 7, 2012 18:09
Heroku Secondary DB connection
# config/application.rb
module MyApp
class Application < Rails::Application
... other configs
config.secondary_database_url = ENV['SECONDARY_DB_URL']
end
end
We may want to override this in development / test
@adamrobbie
adamrobbie / application.rb
Created November 15, 2012 11:57
ActionMailer hook to intercept all outgoing development email
# Place me in config/application.rb or in your development file
if Rails.env.development?
class Hook
def self.delivering_email(message)
message.to = "\"#{message.to.first}\" <my@email.com>"
message.cc = nil if !message.cc.nil?
message.bcc = nil if !message.bcc.nil?
end
end
@adamrobbie
adamrobbie / Gemfile
Created November 13, 2012 15:28
Rails Endless pagination
gem 'will_paginate'
@adamrobbie
adamrobbie / Remote rep
Created November 4, 2012 12:55
Remotely create a github rep
curl -u 'USER:PASS' https://api.github.com/user/repos -d '{"name":"REPO"}'
git remote add origin git@github.com:USER/REPO.git
git push origin master
@adamrobbie
adamrobbie / partial_async.rb
Created September 1, 2012 12:53
Render view asynchronously.
First put an empty, placeholder div in the main response
<div id="pink-dancing-elephants"></div>
and then add a little jQuery to the page
$.ajax({
url: "/elephants/dancing",
cache: false,
success: function(html){
$("#pink-dancing-elephants").append(html);
@adamrobbie
adamrobbie / controller.rb
Created August 30, 2012 13:42 — forked from joakimk/controller.rb
A way to lazy load partials in Rails 3
class Controller
include LazyLoad
def show
@model = Model.find(...)
respond_to do |format|
format.html do
@html_specific_data = Model.find(...)
end
@adamrobbie
adamrobbie / handle_orphaned.task
Created August 29, 2012 13:14
Rake task to handle orphaned records
namespace :db do
desc "Handle orphans"
task :handle_orphans => :environment do
Dir[Rails.root + "app/models/**/*.rb"].each do |path|
require path
end
ActiveRecord::Base.send(:descendants).each do |model|
model.reflections.each do |association_name, reflection|
if reflection.macro == :belongs_to
model.all.each do |model_instance|
@adamrobbie
adamrobbie / async_helper.rb
Created August 3, 2012 14:48
Rails Helper to call javascript asynchronous
module AsyncHelper
def javascript_async(*args)
content_tag :script, type: "text/javascript" do
"(function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = '#{j javascript_path(*args)}';
var other = document.getElementsByTagName('script')[0];