Skip to content

Instantly share code, notes, and snippets.

@takai
Created July 11, 2011 14:31
Show Gist options
  • Save takai/1075966 to your computer and use it in GitHub Desktop.
Save takai/1075966 to your computer and use it in GitHub Desktop.
# -*- mode: ruby; coding: utf-8-unix -*-
require 'active_model'
class User
include ActiveModel::Validations
attr_accessor :email
attr_accessor :password
attr_reader :password_check_mode
validates :email, :presence => true, :unless => :password_check_mode
validates :password, :presence => true
def password_check
@password_check_mode = true
valid?
ensure
@password_check_mode = false
end
end
u1 = User.new
u1.password = 'passw0rd'
raise if u1.valid?
u2 = User.new
u2.password = 'passw0rd'
raise unless u2.password_check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment