Skip to content

Instantly share code, notes, and snippets.

@madsheep
madsheep / membership.rb
Last active August 29, 2015 14:00
Example 1 - simple model.
class Membership
belongs_to :user
belongs_to :group
after_save :send_email_notification, if: ->(m) { m.accepted_changed? && m.accepted? }
def send_email_nofication
NotificationMailer.accepted(self, user).deliver!
end
@madsheep
madsheep / sessions.rb
Last active August 29, 2015 13:57
log-in service example
# session controller
# receives callbacks with oauth
class SessionsController < ApplicationController
skip_before_filter :login_required
def create
login.run!
redirect_to root_path
end
@madsheep
madsheep / discipline.rb
Last active December 27, 2015 14:09
Ultimate solution for long running specs. It's not that rails are slow, or rspec is slow, or ruby is slow. It's your code that is slow and/or poorly designed. The solution to slow tests problem is not optimizing them once the are too slow - it's about not letting them get slugish in the first place.
# in your spec_helper.rb
RSpec.configure do |config|
config.around(:each) do |example|
timeout(0.2) { example.run }
end
end
@madsheep
madsheep / circle.yml
Created October 31, 2013 13:20
Circle configuration for using DNS apply script.
machine:
environment:
AWS_ACCESS_KEY_ID:
AWS_SECRET_ACCESS_KEY:
dependencies:
pre:
- pip install cli53 --user
test:
override:
- "rake"
@madsheep
madsheep / apply.rb
Last active December 27, 2015 01:59
Quick and a bit dirty solution for sync'ing all your DNS BIND files to route53
cli53 = '/home/ubuntu/.local/bin/cli53'
zones_on_aws = `#{cli53} list`.lines.select{|e| e =~ /Name/ }.map{|e| e.split(":").last.strip.sub(/\.$/, '') }
Dir.glob('zones/*.bind').each do |zone|
zone_name = File.basename(zone, '.bind')
unless zones_on_aws.include?(zone_name)
system("#{cli53} create #{zone_name}")
puts "#{zone_name} created."
end
abort("Can't sync #{zone_name}...") unless system("#{cli53} import #{zone_name} --file #{zone} --replace")
@madsheep
madsheep / kitten_mailer.rb
Created September 10, 2013 14:58
Any mailer inherited from this one will have random cat included as an attachment.
class KittenMailer < ActionMailer::Base
def mail *args
attachments['cat.jpeg'] = open("http://placekitten.com/#{rand(300)+300}/#{rand(300)+300}").read
super
end
end
@madsheep
madsheep / application_controller.rb
Last active December 11, 2015 00:58
Decent exposure decreases coupling in your controller and views. Draper helps you stop writing those messy helpers. I find forcing myself to decorate every object a good practice - we call this a 'decent decoration'
class ApplicationController < ActionController::Base
extend DecentDecorate
end
@madsheep
madsheep / syntax_spec.rb
Created April 20, 2012 09:26
Syntax tests
feature "correct syntax in project" do
scenario "validate coffeescript files for errors" do
Dir["#{Rails.root}/**/*.coffee"].each do |file|
%x[coffee --lint -p #{file} ]
$?.success?.should be_true
end
end
scenario "validate haml files" do
@madsheep
madsheep / package.json
Created April 20, 2012 09:24
npm package file
{
"name": "some_project",
"version": "0.0.1",
"dependencies": {
"coffee-script": "1.1.1"
}
}
@madsheep
madsheep / hash.rb
Created December 14, 2011 10:22
Hardcore deep hash
class Hash
define_method '[]_with_try_on', do |*args|
if args.size == 1 and (args.is_a?(Symbol) or args.is_a?(String))
self.send(:'[]_with_try_on', args.first)
else
args.inject(self.dup){|memo, e| memo.dup.delete(e) if memo.is_a?(Hash) }
end
end