Skip to content

Instantly share code, notes, and snippets.

Pull Request Checklist

Always

There is no excuse for not following these

  • It solves the problem.
  • It doesn't break Demeter.
  • It follows single responsibility.
  • Naming is declarative.
@slowjud
slowjud / spec_helper.rb
Last active August 29, 2015 14:00
Testing apis
require 'simplecov'
SimpleCov.start 'rails' do
add_filter 'app/controllers/[\w_]*.rb'
add_filter 'app/models/[\w_]*.rb'
add_filter 'app/modules/[\w_]*.rb'
end
SimpleCov.minimum_coverage 100
@slowjud
slowjud / foo.rb
Last active August 29, 2015 14:00
API domain objects
module Api::V1
class foo
include ActiveModel::Serializers::JSON
def create(attrs)
end
def find(id)
@slowjud
slowjud / api_constraints.rb
Created May 1, 2014 06:15
Version Accept headers
class ApiConstraints
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(req)
@default || req.headers['Accept'].include?("application/vnd.betterbills.v#{@version}")
end
end
crontab -l | { cat; echo "@hourly 'for i in {1..20}; do say wub; done'"; } | crontab -
@slowjud
slowjud / Gemfile
Created April 24, 2014 06:34
An empty ruby gem
source "https://rubygems.org"
# Declare your gem's dependencies in service_qualification.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec
# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
@slowjud
slowjud / queries.rb
Last active August 29, 2015 13:57
All the commands and seed data for my mongoid talk at RORO
# Indexes
Bike.where(style: 'FIXIE').explain['indexBounds']
Bike.where(brand_name: 'Genesis').explain['indexBounds']
Bike.where(style: 'FIXIE', brand_name: 'Genesis').explain['indexBounds']
# Nil and unset
chinarello = Bike.first
chinarello.colour = nil
chinarello.save
chinarello.reload
@slowjud
slowjud / bike.rb
Last active August 29, 2015 13:57
Dodgy Bike models for my mogoid talk at RORO
class Bike
include Mongoid::Document
field :brand_name
validates :brand_name, presence: true
STYLES = %w(CARBON_MONSTROSITY DUALIE FIXIE TOURER)
field :style
validates :style, inclusion: STYLES
@slowjud
slowjud / bike.rb
Created March 10, 2014 23:55
Good Bike models for my mongoid talk at RORO
class Bike
include Mongoid::Document
field :brand_name
validates :brand_name, presence: true
STYLES = %w(CARBON_MONSTROSITY DUALIE FIXIE TOURER)
field :style
validates :style, inclusion: STYLES
def alert_log
AlertLog.find(:all, :joins => [:want, :alert],
:conditions => ['wants.user_id = ? OR alerts.user_id = ?', id, id])
end