Skip to content

Instantly share code, notes, and snippets.

# BAD: opening up the original class and defining/redefining methods
class ActiveRecord::Base
def my_monkey_patch
end
end
# GOOD: extracting custom logic and *injecting* into core class
module MyCustomLogic
extend ActiveSupport::Concern
user = User.new
# execute instance method in background
user.backgrounded.do_stuff
# execute class method in background
User.backgrounded.do_something_else
# perform work on a custom queue
user.backgrounded(:queue => 'custom').do_something_else
# VERSIONING
# all production gems should use STRICT version dependencies ex: "1.0.0"
# development and test gems can you LOOSE version dependencies ex: "~> 1.0.0"
source 'http://rubygems.org'
# list gems for *all* environments/groups here
# ex: rails, mysql2, rails-console-tweaks
# list gems for frontend app servers here
class UuidValidator < ActiveModel::EachValidator
VALID_UUID_REGEX = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "is invalid") unless UuidValidator.valid_uuid?(value)
end
def self.valid_uuid?(value)
!!VALID_UUID_REGEX.match(value.to_s)
end
end
# utility for performing multiple fetches on hashes
# ex:
# h = {:foo => {:bar => :baz}}
# h.deep_fetch :foo, :bar
class Hash
def deep_fetch(*keys, last_key)
scope = self
keys.each do |key|
scope = scope.fetch key, {}
end
#!/usr/bin/env ruby
require 'rubygems'
require 'thor'
module Backgrounded
class CLI < Thor
desc 'enqueue', 'enqueue a clazz.method invocation for resque backgrounded workers'
method_option :queue, :aliases => "-q", :desc => "resque queue to enqueue the operation to", :default => 'backgrounded'
method_option :rails_env, :aliases => '-e', :desc => 'control which rails env used to load the redis config', :default => 'production'
# config/application.rb
config.devise_oauth2_providable.access_token_expires_in = 1.second # 15.minute default
config.devise_oauth2_providable.refresh_token_expires_in = 1.minute # 1.month default
config.devise_oauth2_providable.authorization_token_expires_in = 5.seconds # 1.minute default
IT
basic out of the box example definition.
it { should be_true }
SPECIFY
alias for IT. use to make your example more readable
specify { @user.should be_true }
# spec/support/factory_girl_with.rb
# replace instance variables and before blocks with memoized factory instances
module FactoryGirl
module With
def with(name, options = {})
let(name) { ::Factory.create(name, options) }
end
end
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'paperclip/matchers'
require 'webmock/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}