Skip to content

Instantly share code, notes, and snippets.

@timdiggins
timdiggins / rake_testing_support.rb
Created March 20, 2015 13:11
testing rake in rspec (etc)
# ./spec/support/rake_testing_support.rb
def invoke_rake_task_in_test(task_name)
require 'rake'
Rails.application.load_tasks if Rake.application.tasks.count < 10
Rake::Task[task_name].reenable
Rake::Task[task_name].invoke
end
@timdiggins
timdiggins / gist:294f0a37312fcbeaac70
Created December 18, 2014 10:44
delayedjob plugin to log backtraces
module Delayed
module Plugins
class LogErrors < Plugin
callbacks do |lifecycle|
lifecycle.around(:invoke_job) do |job, &block|
begin
block.call(job)
rescue Exception => error
Delayed::Worker.logger.error(error.message)
Delayed::Worker.logger.error(error.backtrace.join("\n"))
@timdiggins
timdiggins / delayedjob-bugsnag.rb
Last active August 29, 2015 14:11 — forked from loopj/delayedjob-bugsnag.rb
notify bugsnag when delayed job errors occur
# some references which helped
# http://blog.salsify.com/engineering/delayed-jobs-callbacks-and-hooks-in-rails
# http://stackoverflow.com/a/16639849/109175
# example plugin: https://github.com/collectiveidea/delayed_job/blob/master/lib/delayed/plugins/clear_locks.rb
module Delayed
module Plugins
class Bugsnag < Plugin
callbacks do |lifecycle|
lifecycle.around(:invoke_job) do |job, &block|
begin
@timdiggins
timdiggins / traceback.txt
Created January 24, 2013 12:56
traceback for stack overflow problem
ActionView::Template::Error: undefined method `action_methods' for nil:NilClass
/Users/tim/.rvm/gems/ruby-1.9.2-p320@buddy/gems/authlogic-3.2.0/lib/authlogic/controller_adapters/abstract_adapter.rb:63:in `method_missing'
/Users/tim/.rvm/gems/ruby-1.9.2-p320@buddy/gems/activeadmin-0.5.1/lib/active_admin/resource/action_items.rb:56:in `block in add_default_action_items'
/Users/tim/.rvm/gems/ruby-1.9.2-p320@buddy/gems/activeadmin-0.5.1/lib/active_admin/views/action_items.rb:9:in `instance_eval'
/Users/tim/.rvm/gems/ruby-1.9.2-p320@buddy/gems/activeadmin-0.5.1/lib/active_admin/views/action_items.rb:9:in `block (2 levels) in build'
/Users/tim/.rvm/gems/ruby-1.9.2-p320@buddy/gems/arbre-1.0.1/lib/arbre/element/builder_methods.rb:31:in `block in build_tag'
/Users/tim/.rvm/gems/ruby-1.9.2-p320@buddy/gems/arbre-1.0.1/lib/arbre/context.rb:92:in `with_current_arbre_element'
/Users/tim/.rvm/gems/ruby-1.9.2-p320@buddy/gems/arbre-1.0.1/lib/arbre/element/builder_methods.rb:49:in `with_current_arbre_element'
/Users/tim/.rvm/g
@timdiggins
timdiggins / gist:2899995
Created June 9, 2012 07:35
method to extract parts from a rails Mail::Message
def parts(email)
splits = email.encoded.split(/(\r\n)*----==_mimepart_[a-zA-Z0-9_]+(\r\n)*/)
parts = {header: splits[0]}
splits[1..-2].each do |part|
headers, body = part.split(/\r\n\r\n/, 2)
if(m = /Content-Type: ([^; ]+);?\s/.match(headers))
type = m[1]
parts[type] = OpenStruct.new(headers:headers, body:body)
end
end
@timdiggins
timdiggins / attr_test_css.rb
Created May 26, 2012 07:14
Quick demo of problem with css attribute search with slash
require 'test/unit'
require 'nokogiri'
class AttributeWithSlashCssTest < Test::Unit::TestCase
def setup
@doc = Nokogiri.parse(
<<-END
<html><body>
<a href=\"/my/path\">My link text</a>
<a href=\"/my-other-path\">
/usr/lib/ruby/gems/1.8/gems/chef-0.9.14/bin/../lib/chef/data_bag_item.rb:95:in `raw_data=': Data Bag Items must have an id key in the hash! {"evq"=>"https://api.opscode.com/organizations/ima_evq/data/apps/evq"} (Chef::Exceptions::ValidationFailed)
from /usr/lib/ruby/gems/1.8/gems/chef-0.9.14/bin/../lib/chef/data_bag_item.rb:147:in `from_hash'
from /usr/lib/ruby/gems/1.8/gems/chef-0.9.14/bin/../lib/chef/data_bag_item.rb:183:in `load'
from /var/chef/cache/cookbooks/django/libraries/app.rb:6:in `initialize'
from /var/chef/cache/cookbooks/django/definitions/install.rb:3:in `new'
from /var/chef/cache/cookbooks/django/definitions/install.rb:3:in `from_file'
from /usr/lib/ruby/gems/1.8/gems/chef-0.9.14/bin/../lib/chef/mixin/recipe_definition_dsl_core.rb:50:in `instance_eval'
from /usr/lib/ruby/gems/1.8/gems/chef-0.9.14/bin/../lib/chef/mixin/recipe_definition_dsl_core.rb:50:in `method_missing'
from /var/chef/cache/cookbooks/django/definitions/install.rb:42:in `from_file'
from /usr/lib/ruby/gems/1.8/gems/chef
@timdiggins
timdiggins / chef_solo_patch.rb
Created March 13, 2011 07:54
Monkeypatch for chef solo to do basic data_bag manipulations
# save this in the library folder of a cookbook
# (e.g. ./coookbooks/vagrant/library/chef_solo_patch.rb)
# see also https://gist.github.com/867958 for vagrant patch
# based on http://lists.opscode.com/sympa/arc/chef/2011-02/msg00000.html
if Chef::Config[:solo]
class Chef
module Mixin
module Language
def data_bag(bag)
@timdiggins
timdiggins / Vagrant_monkeypatch.rb
Created March 13, 2011 07:43
Vagrantfile monkeypatch of Vagrant's chef solo provisioner to allow databag folders
CHEF_REPO = "../chef_repo" # path to chef repo,
# because Vagrantfile is normally in application repo, not in chef repo.
# see also https://gist.github.com/867960 for chef_solo patch
module Vagrant
module Provisioners
class ChefSolo
class Config
attr_accessor :data_bag_path
end
@timdiggins
timdiggins / search_for_chef_solo.rb
Created March 8, 2011 06:52
simplistic search implementation for chef solo
# better to use a monkeypatch - see https://gist.github.com/867960