Skip to content

Instantly share code, notes, and snippets.

@biscuitvile
biscuitvile / 01_shell.txt
Created March 20, 2014 18:49
minitest problem on Rails 4.0.4
ruby -v
#=> 2.0.0p353
rails -v
#=> rails 4.0.4
rails new minitest_example && cd minitest_example
@biscuitvile
biscuitvile / validation_as_a_service.md
Last active August 29, 2015 13:57
Validation as a service

This is my take on isolating validations from ActiveRecord models, inspired by Corey Haines' gist here: https://gist.github.com/coreyhaines/3305349

The main desires are putting validation responsibility into their own object, reducing the bloat of god classes, and easing validation concerns from tests which should not care about them, particularly when validations are quite complex.

This method relies upon inclusion of the ValidationService module within an ActiveRecord class. The model will invoke its validator automatically while saving provided there is a validator that follows the appropriate naming convention.

In order to perform tests that don't concern themselves with validation logic that you desired to be unencumbered by validation, you have two options. One is to instantiate with a NilValidator: Account.new(validation_service: NilValidator)

Alternatively you can monkey-patch the validating class at the top of your test file:

@biscuitvile
biscuitvile / form_builder.rb
Created March 6, 2014 16:54
Form builder with inline error handling
class FormBuilder < ActionView::Helpers::FormBuilder
EMAIL_OPTIONS = {autocapitalize: 'off', autocomplete: 'off', autocorrect: 'off'}
def label(method, text = nil, options = {}, &block)
text = (text || method.to_s.humanize.titleize)
unless object.nil?
errors = object.errors[method.to_sym]
if errors.present?
text += " <small>#{errors.is_a?(Array) ? errors.first : errors}</small>"
options[:class] ||= 'error'
@biscuitvile
biscuitvile / controller.rb
Created March 5, 2014 20:14
Errors getting swallowed
class SettingsController < ApplicationController
layout 'main', only: [:more]
def edit
@account = client.account
end
def edit
@form = SettingsUpdate.new(account: client.account)
- flash.keys.each do |key|
- content_for :message do
= render partial: 'shared/forms/message', type: key, message: flash[key]
@biscuitvile
biscuitvile / .vimrc.local
Created February 22, 2014 15:14
Blow up the spot on too-long lines
" handle long lines
set textwidth=80
:match ErrorMsg '\%>80v.\+'
@biscuitvile
biscuitvile / architectures.md
Last active August 29, 2015 13:56
Architecture talks and materials
@biscuitvile
biscuitvile / gary.log
Created February 14, 2014 14:46 — forked from NZKoz/gary.log
Koz-Prostyle-2:store michaelkoziarski$ time /usr/bin/ruby -e ''
real 0m0.117s
user 0m0.031s
sys 0m0.056s
Koz-Prostyle-2:store michaelkoziarski$ time ~/.rbenv/shims/ruby -e ''
real 0m0.096s
user 0m0.057s
sys 0m0.038s
@biscuitvile
biscuitvile / test_helper.rb
Created February 4, 2014 04:00
test_helper for sinatra
require 'rubygems'
require 'test/unit'
require 'rack/test'
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'sinatra/erector'
class Test::Unit::TestCase
include Rack::Test::Methods
class Api::V1::SitesController < Api::V1::BaseController
actions :show
def index
respond_with [ Site.find_by_subdomain(params[:subdomain]) ]
end
end