Skip to content

Instantly share code, notes, and snippets.

View mattyoho's full-sized avatar

Matt Yoho mattyoho

  • Portland, OR, USA
View GitHub Profile

Validations

Data integrity is an underrated part of proper application architecture. Many of the bugs in production systems are triggered by missing or malformed user data. If a user can possibly screw it up or screw with it, they will. Validations in the model can help!

On Syntax

Before we begin, let's talk about syntax. There are two primary syntaxes for writing validations in Rails 3:

validates_presence_of :price
Fabricate(:person) do
ssn { sequence(:ssn, 111111111) }
email { sequence(:email) { |i| "user#{i}@example.com" } }
end
# following is a little benchmark looking a real world example of inserting a
# bunch of records into pg and mongo using a few methods available in ruby.
#
# the code inserst a value with a simple uniq contraint and then looks it up.
#
# this is not meant to be an apples to apples benchmark, rather it's trying to
# see how the various dbs might perform in a real world situation. i know
# mongo does not fsync to disk. i know it's not fair using a transaction with
# the rdbms, but these conditions will exist in real world usage and it's that
# which i'm interested in.
module Padrino
# Minimal version of `decent_exposure`[1], in keeping with Padrino's
# simplicity.
#
# [1] https://github.com/voxdolo/decent_exposure
#
# == Usage
#
# The first step is to register the plugin with your application:
@ryanlecompte
ryanlecompte / gist:891728
Created March 29, 2011 02:52
BUG with Ruby 1.9.2 (MRI)
# The following fails with a failure to call "x=" private method
String.send(:attr_accessor, :x)
s = ""
s.x = 100
# The following works:
class String
self.send(:attr_accessor, :x)
end
s = ""
Username = <%= @var %>
@antirez
antirez / sinatra_redis_cache.rb
Created March 22, 2011 21:06
the simplest Redis caching for Sinatra -- not even tested... consider this code just an hint
# This is just an hint for a simple Redis caching "framework" using Sinatra
# A friend of mine asked for something like that, after checking the first two gems realized there was
# a lot of useless complexity (IMHO). That's the result.
#
# The use_cache parameter is useful if you want to cache only if the user is authenticated or
# something like that, so you can do cache_url(ttl,User.logged_in?, ...)
require 'rubygems'
require 'sinatra'
require 'redis'
diff --git a/ftplugin/ruby.vim b/ftplugin/ruby.vim
index 07358b2..3b40da3 100644
--- a/ftplugin/ruby.vim
+++ b/ftplugin/ruby.vim
@@ -29,8 +29,11 @@ endif
if exists("loaded_matchit") && !exists("b:match_words")
let b:match_ignorecase = 0
+ " Go go gadget match! This ignores if, unless, while, and until if they come
+ " after non-spaces, meaning they're a post condition.
@tmm1
tmm1 / gist:818340
Created February 9, 2011 11:31
working prototype of rbtrace
~/code/rbtrace (master*) % cat server.rb
require 'rbtrace'
while true
Dir.chdir("/tmp") do
sleep rand*0.5
end
end
~/code/rbtrace (master*) % ruby server.rb &
class Notification
property :name, String
belongs_to :user
end
class User
has n, :notifications
end
# Find all users with no notifications