A nice way of not keeping track of retry counter and not using a loop
3.times do
begin
raise "Nope"
rescue
puts "Failed"
else
puts "Success"
break
end
end
A nice way of not keeping track of retry counter and not using a loop
3.times do
begin
raise "Nope"
rescue
puts "Failed"
else
puts "Success"
break
end
end
ActiveRecord loads the xxx_type in your model, making it blow up when doing includes / using the belongs_to on a missing type.
So we make it un-missing.
Usage
class Waldo < MissingType end
Code
class MissingType < ActiveRecord::Base
default_scope :conditions => "1 = 2", :limit => 0
self.table_name = "schema_migrations"
def self.primary_key
"version"
end
def readonly?
true
end
end
Repeatedly entering password is quiet annoying, luckily most vagrant base boxes come with the same insecure ssh key 🙂
curl https://raw.github.com/mitchellh/vagrant/master/keys/vagrant > vagrant.key chmod 600 vagrant.key knife solo cook --ssh-identity vagrant.key vagrant@vagrant
We want to enqueue jobs, but do not want to blow up the app with sidekiq and it’s dependencies.
Usage
RawSidekiq.enqueue("XyzJob", [1,2,3], :namespace => "custom")
Code
# https://grosser.it/2013/01/17/enqueue-into-sidekiq-via-pure-redis-without-loading-sidekiq
require "json"
require "redis"
require "securerandom"
class RawSidekiq
def self.enqueue(queue, klass, args, options={})
payload = {
'class' => klass,
'args' => args,
'jid' => SecureRandom.hex(12),
#'retry' => true
}.to_json
conn = Redis.new
conn.multi do
conn.sadd([options[:namespace], "queues"].compact.join(":"), queue)
conn.lpush([options[:namespace], "queue", queue].compact.join(":"), payload)
end
end
end
Killing observers
Before:
# config/environment.rb
config.observers = [:foo_observer]
# app/observers/foo_observer.rb
class FooObserver < ActiveRecord::Observer
observes :user
def after_save(user)
....
end
end
After:
# app/models/user.rb
class User < ActiveRecord::Base
include FooObserver
end
# app/observers/foo_observer.rb
module FooObserver
class << self
def included(base)
this = self
base.after_save{|user| this.more_descriptive_name(user) }
end
def more_descriptive_name(user)
...
end
end
end