<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: validations code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 06:08:56 GMT</pubDate>
    <description>DZone Snippets: validations code</description>
    <item>
      <title>Custom validation with rails: words with more than 26 characters</title>
      <link>http://snippets.dzone.com/posts/show/5266</link>
      <description>This goes in whatever model you're validating.  "Subject" can be any of the symbols in your model and doesn't need the : in front of it.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def validate&lt;br /&gt;	if subject.split.any?{|w| w.length &gt; 26}&lt;br /&gt;		errors.add(:subject, "cannot have words more than 26 consecutive characters")&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 21 Mar 2008 19:40:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5266</guid>
      <author>ioda006 ()</author>
    </item>
    <item>
      <title>Rspec my validations for models</title>
      <link>http://snippets.dzone.com/posts/show/4508</link>
      <description>// This is the plain-vanilla way that I've been using spec for basic model validations.  &lt;br /&gt;// There is also a bit at the bottom that checks that I have the right association&lt;br /&gt;// setup.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require File.dirname(__FILE__) + '/../spec_helper'&lt;br /&gt;&lt;br /&gt;module CommentSpecHelper&lt;br /&gt;  def valid_comment_attributes&lt;br /&gt;    { :body =&gt; 'Some text',&lt;br /&gt;      :commentable_type =&gt; 'School',&lt;br /&gt;      :commentable_id =&gt; 1 }&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;describe Comment do&lt;br /&gt;&lt;br /&gt;  include CommentSpecHelper&lt;br /&gt;&lt;br /&gt;  before(:each) do&lt;br /&gt;    @comment = Comment.new&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  it "should be valid" do&lt;br /&gt;    @comment.attributes = valid_comment_attributes&lt;br /&gt;    @comment.should be_valid&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  it "should should not be valid without something to attach to" do&lt;br /&gt;    c = valid_comment_attributes&lt;br /&gt;    c.delete :commentable_id&lt;br /&gt;    c.delete :commentable_type&lt;br /&gt;    @comment.attributes = c&lt;br /&gt;    @comment.should_not be_valid&lt;br /&gt;    @comment.errors.on(:commentable_id).should eql("can't be blank")&lt;br /&gt;    @comment.commentable_id = 1&lt;br /&gt;    @comment.should_not be_valid&lt;br /&gt;    @comment.errors.on(:commentable_type).should eql("can't be blank")&lt;br /&gt;    @comment.commentable_type = "School"&lt;br /&gt;    @comment.should be_valid&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  it "should require body" do&lt;br /&gt;    @comment.attributes = valid_comment_attributes.except(:body)&lt;br /&gt;    @comment.should_not be_valid&lt;br /&gt;    @comment.errors.on(:body).should eql("can't be blank")&lt;br /&gt;    @comment.body = "Some text"&lt;br /&gt;    @comment.should be_valid&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  it "should relate to commentable" do&lt;br /&gt;    Comment.reflect_on_association(:commentable).should_not be_nil&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  it "should relate to user" do&lt;br /&gt;    Comment.reflect_on_association(:user).should_not be_nil&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 07 Sep 2007 14:48:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4508</guid>
      <author>drichards (David Richards)</author>
    </item>
    <item>
      <title>URL/URI Validations in Rails</title>
      <link>http://snippets.dzone.com/posts/show/2563</link>
      <description>Here is a quick/basic URI validation library for your Rails app: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;&lt;br /&gt;# Original credits: http://blog.inquirylabs.com/2006/04/13/simple-uri-validation/&lt;br /&gt;# HTTP Codes: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html&lt;br /&gt;&lt;br /&gt;class ActiveRecord::Base&lt;br /&gt;  def self.validates_uri_existence_of(*attr_names)&lt;br /&gt;    configuration = { :message =&gt; "is not valid or not responding", :on =&gt; :save, :with =&gt; nil }&lt;br /&gt;    configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;    &lt;br /&gt;    raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp)&lt;br /&gt;    &lt;br /&gt;    validates_each(attr_names, configuration) do |r, a, v|&lt;br /&gt;        if v.to_s =~ configuration[:with] # check RegExp&lt;br /&gt;              begin # check header response&lt;br /&gt;                  case Net::HTTP.get_response(URI.parse(v))&lt;br /&gt;                    when Net::HTTPSuccess then true&lt;br /&gt;                    else r.errors.add(a, configuration[:message]) and false&lt;br /&gt;                  end &lt;br /&gt;              rescue # Recover on DNS failures.. &lt;br /&gt;                  r.errors.add(a, configuration[:message]) and false&lt;br /&gt;              end&lt;br /&gt;        else&lt;br /&gt;          r.errors.add(a, configuration[:message]) and false&lt;br /&gt;        end &lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Save the code above into a file in your 'lib' directory. ex: validates_uri_existence_of.rb. Include the file in your environment.rb (ex: require 'validates_uri_existence_of'). &lt;br /&gt;&lt;br /&gt;And now you can use the validator in any model by calling:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;validates_uri_existence_of :url, :with =&gt;&lt;br /&gt;        /(^$)|(^(http|https)://[a-z0-9] ([-.]{1}[a-z0-9] )*.[a-z]{2,5}(([0-9]{1,5})?/.*)?$)/ix&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Above code validates the URI/URL against a regular expression first, and then does a HEAD query to confirm that the page is alive (HTTPSuccess code is returned). You could easily modify the code to extend the functionality as you wish. &lt;br /&gt;&lt;br /&gt;More info in &lt;a href="http://www.igvita.com/blog/2006/09/07/validating-url-in-ruby-on-rails/"&gt; this post.&lt;/a&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 08 Sep 2006 07:56:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2563</guid>
      <author>igrigorik (Ilya Grigorik)</author>
    </item>
    <item>
      <title>Date Validator</title>
      <link>http://snippets.dzone.com/posts/show/1548</link>
      <description>The date_validator.rb snippet adds a validates_dates date validator to Rails that accepts a range of user friendly date formats.&lt;br /&gt;&lt;br /&gt;Dates are particularly vexing in a global web environment as there are many acceptable abbreviated and locale dependent formats. Rather than try to support multiple locale dependent formats I side-stepped the issue and choose two unambiguous formats along with abbreviations and date intervals.&lt;br /&gt;&lt;br /&gt;These formats coupled with a date picker calendar control result in fairly painless date input.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# This code replumbs a few classes so that ActiveRecord accepts a wider range&lt;br /&gt;# of user friendly date formats. Change Date.parse_date if you need different&lt;br /&gt;# date input formats. To use: drop this file into ./lib, require it into&lt;br /&gt;# your model and then validate dates with the validates_dates validator.&lt;br /&gt;#&lt;br /&gt;# Written by Stuart Rackham &lt;srackham@methods.co.nz&gt;&lt;br /&gt;# Current version tested on Rails 1.0.0&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;# Add date string parser class method to Date class.&lt;br /&gt;#&lt;br /&gt;class Date&lt;br /&gt;&lt;br /&gt;  # Parse date string with one of the following formats:&lt;br /&gt;  #&lt;br /&gt;  # * ISO date format: yyyy-mm-dd, for example '2001-12-25'&lt;br /&gt;  # * d[ mmm[ yy[yy]]]: examples: '22', '22 feb', '22 feb 2003',&lt;br /&gt;  #   '22 feb 03', '22 February 2003'&lt;br /&gt;  # * +n[units] or -n[units]: Date from today: examples: '+22', '+22 days',&lt;br /&gt;  #   '+22d', '-4 weeks', '-4w', '-4week', '+6 months', '+6m', '+6month',&lt;br /&gt;  #   '-2 years', '-2y'&lt;br /&gt;  #&lt;br /&gt;  # The string argument is first converted to a string with #to_s.&lt;br /&gt;  # Returns nil if passed nil or an empty string.&lt;br /&gt;  # Raises ArgumentError if string can't be parsed.&lt;br /&gt;  #&lt;br /&gt;  def self.parse_date(string)&lt;br /&gt;    string = string.to_s.strip.downcase&lt;br /&gt;    return nil if string.empty?&lt;br /&gt;    today = Date.today&lt;br /&gt;    if string =~ /^(\d{4})-(\d{2})-(\d{2})$/&lt;br /&gt;      # ISO date format.&lt;br /&gt;      date_array = ParseDate.parsedate(string, true)&lt;br /&gt;      begin&lt;br /&gt;        result = Date.new(date_array[0], date_array[1], date_array[2])&lt;br /&gt;      rescue&lt;br /&gt;        raise ArgumentError&lt;br /&gt;      end&lt;br /&gt;    elsif string =~ /^(\d{1,2})(?:(?:\s+|-)([a-zA-Z]{3,8})(?:(?:\s+|-)(\d{2}(?:\d{2})?))?)?$/&lt;br /&gt;      # 'd mmmm yyyy' format and abbreviations.&lt;br /&gt;      day = $1&lt;br /&gt;      month = $2 || Date::ABBR_MONTHNAMES[today.month]&lt;br /&gt;      year = $3 || today.year&lt;br /&gt;      date_array = ParseDate.parsedate("#{day} #{month} #{year}", true)&lt;br /&gt;      begin&lt;br /&gt;        result = Date.new(date_array[0], date_array[1], date_array[2])&lt;br /&gt;      rescue&lt;br /&gt;        raise ArgumentError&lt;br /&gt;      end&lt;br /&gt;    elsif string =~ /^([+-]\d+)(?:\s*(d|days?|w|weeks?|m|months?|y|years?))?$/&lt;br /&gt;      # Date intervals.&lt;br /&gt;      n = $1.to_i&lt;br /&gt;      units = $2 || 'days'&lt;br /&gt;      case units.first&lt;br /&gt;      when 'd'&lt;br /&gt;        result = today + n&lt;br /&gt;      when 'w'&lt;br /&gt;        result = today + n*7&lt;br /&gt;      when 'm'&lt;br /&gt;        sign = n &lt;=&gt; 0&lt;br /&gt;        month = today.month + sign * (n.abs % 12)&lt;br /&gt;        year = today.year + sign * (n.abs / 12)&lt;br /&gt;        if month &lt;1&lt;br /&gt;          month += 12&lt;br /&gt;          year -= 1&lt;br /&gt;        elsif month &gt; 12&lt;br /&gt;          month -= 12&lt;br /&gt;          year += 1&lt;br /&gt;        end&lt;br /&gt;        result = Date.new(year, month, today.day)&lt;br /&gt;      when 'y'&lt;br /&gt;        result = Date.new(today.year + n, today.month, today.day)&lt;br /&gt;      end&lt;br /&gt;    else&lt;br /&gt;      raise ArgumentError&lt;br /&gt;    end&lt;br /&gt;    result&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;ActiveRecord::Validations::ClassMethods.class_eval do&lt;br /&gt;&lt;br /&gt;  # Validates date values, these can be dates or any formats accepted by&lt;br /&gt;  # Date.parse_date.&lt;br /&gt;  # &lt;br /&gt;  # For example:&lt;br /&gt;  #&lt;br /&gt;  #   class Person &lt; ActiveRecord::Base&lt;br /&gt;  #     require_dependency 'date_validator'&lt;br /&gt;  #     validates_dates :birthday,&lt;br /&gt;  #                     :from =&gt; '1 Jan 1920',&lt;br /&gt;  #                     :to =&gt; Date.today,&lt;br /&gt;  #                     :allow_nil =&gt; true&lt;br /&gt;  #   end&lt;br /&gt;  #&lt;br /&gt;  # Options:&lt;br /&gt;  # * from - Minium allowed date. May be a date or a string recognized&lt;br /&gt;  #   by Date.parse_date.&lt;br /&gt;  # * to - Maxumum allowed date. May be a date or a string recognized&lt;br /&gt;  #   by Date.parse_date.&lt;br /&gt;  # * allow_nil - Attribute may be nil; skip validation.&lt;br /&gt;  #&lt;br /&gt;  def validates_dates(*attr_names)&lt;br /&gt;    configuration =&lt;br /&gt;      { :message =&gt; 'is an invalid date ' \&lt;br /&gt;                    '(here are some valid examples: 23, 23 feb, 23 feb 06, ' \&lt;br /&gt;                    '6 feb 2006, 2006-02-23, +6days, +6d, +6, +2w, -6m, +1y)',&lt;br /&gt;        :on =&gt; :save,&lt;br /&gt;      }&lt;br /&gt;    configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;    # Don't let validates_each handle allow_nils, it checks the cast value.&lt;br /&gt;    allow_nil = configuration.delete(:allow_nil)&lt;br /&gt;    from = Date.parse_date(configuration.delete(:from))&lt;br /&gt;    to = Date.parse_date(configuration.delete(:to))&lt;br /&gt;    validates_each(attr_names, configuration) do |record, attr_name, value|&lt;br /&gt;      before_cast = record.send("#{attr_name}_before_type_cast")&lt;br /&gt;      next if allow_nil and (before_cast.nil? or before_cast == '')&lt;br /&gt;      begin&lt;br /&gt;        date = Date.parse_date(before_cast)&lt;br /&gt;      rescue&lt;br /&gt;        record.errors.add(attr_name, configuration[:message])&lt;br /&gt;      else&lt;br /&gt;        if from and date &lt; from&lt;br /&gt;          record.errors.add(attr_name,&lt;br /&gt;                            "cannot be less than #{from.strftime('%e-%b-%Y')}")&lt;br /&gt;        end&lt;br /&gt;        if to and date &gt; to&lt;br /&gt;          record.errors.add(attr_name,&lt;br /&gt;                            "cannot be greater than #{to.strftime('%e-%b-%Y')}")&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Override default date type cast class method to handle Date.parse_date&lt;br /&gt;# formats(the default implementation returns nil if passed an unrecognized date&lt;br /&gt;# format).&lt;br /&gt;#&lt;br /&gt;class ActiveRecord::ConnectionAdapters::Column&lt;br /&gt;  def self.string_to_date(string)&lt;br /&gt;    return string unless string.is_a?(String)&lt;br /&gt;    Date.parse_date(string) rescue nil&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 22 Feb 2006 08:13:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1548</guid>
      <author>stuart.rackham (Stuart Rackham)</author>
    </item>
    <item>
      <title>Testing Rails Validations</title>
      <link>http://snippets.dzone.com/posts/show/1170</link>
      <description>&lt;code&gt;&lt;br /&gt;#&lt;br /&gt;# Useful for testing Rails ActiveRecord validations. For more information see:&lt;br /&gt;# http://wiseheartdesign.com/articles/2006/01/16/testing-rails-validations/&lt;br /&gt;#&lt;br /&gt;module ValidationTestHelper&lt;br /&gt;  def assert_valid(field, message, *values)&lt;br /&gt;    __model_check__&lt;br /&gt;    values.each do |value|&lt;br /&gt;      o = __setup_model__(field, value)&lt;br /&gt;      if o.valid?&lt;br /&gt;        assert_block { true }&lt;br /&gt;      else&lt;br /&gt;        messages = [o.errors[field]].flatten&lt;br /&gt;        assert_block("unexpected invalid field &lt;#{o.class}##{field}&gt;, value: &lt;#{value.inspect}&gt;, errors: &lt;#{o.errors[field].inspect}&gt;.") { false }&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def assert_invalid(field, message, *values)&lt;br /&gt;    __model_check__&lt;br /&gt;    values.each do |value|&lt;br /&gt;      o = __setup_model__(field, value)&lt;br /&gt;      if o.valid?&lt;br /&gt;        assert_block("field &lt;#{o.class}##{field}&gt; should be invalid for value &lt;#{value.inspect}&gt; with message &lt;#{message.inspect}&gt;") { false }&lt;br /&gt;      else&lt;br /&gt;        messages = [o.errors[field]].flatten&lt;br /&gt;        assert_block("field &lt;#{o.class}##{field}&gt; with value &lt;#{value.inspect}&gt; expected validation error &lt;#{message.inspect}&gt;, but got errors &lt;#{messages.inspect}&gt;") { messages.include?(message) }&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def __model_check__&lt;br /&gt;    raise "@model must be assigned in order to use validation assertions" if @model.nil?&lt;br /&gt;    &lt;br /&gt;    o = @model.dup&lt;br /&gt;    raise "@model must be valid before calling a validation assertion, instead @model contained the following errors #{o.errors.instance_variable_get('@errors').inspect}" unless o.valid?&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def __setup_model__(field, value)&lt;br /&gt;    o = @model.dup&lt;br /&gt;    attributes = o.instance_variable_get('@attributes')&lt;br /&gt;    o.instance_variable_set('@attributes', attributes.dup)&lt;br /&gt;    o.send("#{field}=", value)&lt;br /&gt;    o&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 17 Jan 2006 20:25:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1170</guid>
      <author>jlong (John W. Long)</author>
    </item>
    <item>
      <title>validates italian CF</title>
      <link>http://snippets.dzone.com/posts/show/1136</link>
      <description>This function validates italian CF (fiscal code) by:&lt;br /&gt; - checking for valid characters&lt;br /&gt; - verifying checksum&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def valid_cf?(cf)&lt;br /&gt;  def ascii_displacement(ch)&lt;br /&gt;   (?0..?9) === ch[0] ? (ch[0] - ?0) : (ch[0] - ?A)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  return false if cf !~ %r{^[a-z]{6}\d{2}[abcdehlmprst]\d{2}[a-z]\d{3}[a-z]$}i&lt;br /&gt;  sum = 0&lt;br /&gt;  cf[0..-2].upcase.scan(/(.)(.)?/) do |a, b|&lt;br /&gt;    sum += [ 1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20,&lt;br /&gt;            11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23&lt;br /&gt;           ][ascii_displacement(a)]&lt;br /&gt;    sum += ascii_displacement(b) unless b.nil?&lt;br /&gt;  end&lt;br /&gt;  (sum % 26 + ?A) == cf[-1,1].upcase[0]&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 11 Jan 2006 15:07:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1136</guid>
      <author>marcenuc (Marcello Nuccio)</author>
    </item>
    <item>
      <title>Validations for Non-ActiveRecord Objects</title>
      <link>http://snippets.dzone.com/posts/show/1055</link>
      <description>I needed to find a way to use active record validations on non active record items so i went searching and found this blog which is useful for me..&lt;br /&gt;Totally ripped off Peter Donald's blog :P&lt;br /&gt;http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects&lt;br /&gt;&lt;br /&gt;To get this working grab the active_form.rb file and place it in the app/models directory. You can then make your model objects extend ActiveForm and use them like regular ActiveRecord objects.&lt;br /&gt;&lt;br /&gt;active_form.rb&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Note ".valid?" method  must occur on object for validates_associated&lt;br /&gt;class ActiveForm&lt;br /&gt;  &lt;br /&gt;  def initialize(attributes = nil)&lt;br /&gt;    if attributes&lt;br /&gt;      attributes.each do |key,value|&lt;br /&gt;        send(key.to_s + '=', value)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    yield self if block_given?&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def [](key)&lt;br /&gt;    instance_variable_get("@#{key}")&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def method_missing( method_id, *args )&lt;br /&gt;    if md = /_before_type_cast$/.match(method_id.to_s)&lt;br /&gt;      attr_name = md.pre_match&lt;br /&gt;      return self[attr_name] if self.respond_to?(attr_name)&lt;br /&gt;    end&lt;br /&gt;    super&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;protected &lt;br /&gt;  def raise_not_implemented_error(*params)&lt;br /&gt;    ValidatingModel.raise_not_implemented_error(*params)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def self.human_attribute_name(attribute_key_name)&lt;br /&gt;    attribute_key_name.humanize&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def new_record?&lt;br /&gt;    true&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  # these methods must be defined before include&lt;br /&gt;  alias save raise_not_implemented_error&lt;br /&gt;  alias update_attribute raise_not_implemented_error&lt;br /&gt;&lt;br /&gt;public&lt;br /&gt;  include ActiveRecord::Validations&lt;br /&gt;&lt;br /&gt;protected &lt;br /&gt;&lt;br /&gt;  # the following methods must be defined after include so that they overide&lt;br /&gt;  # methods previously included&lt;br /&gt;  alias save! raise_not_implemented_error&lt;br /&gt;&lt;br /&gt;  class &lt;&lt; self&lt;br /&gt;    def raise_not_implemented_error(*params)&lt;br /&gt;      raise NotImplementedError&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    alias validates_uniqueness_of raise_not_implemented_error&lt;br /&gt;    alias create! raise_not_implemented_error&lt;br /&gt;    alias validate_on_create raise_not_implemented_error&lt;br /&gt;    alias validate_on_update raise_not_implemented_error&lt;br /&gt;    alias save_with_validation raise_not_implemented_error    &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;require 'dispatcher'&lt;br /&gt;class Dispatcher&lt;br /&gt;  class &lt;&lt; self&lt;br /&gt;    if ! method_defined?(:form_original_reset_application!) &lt;br /&gt;      alias :form_original_reset_application! :reset_application!&lt;br /&gt;      def reset_application!&lt;br /&gt;        form_original_reset_application!&lt;br /&gt;        Dependencies.remove_subclasses_for(ActiveForm) if defined?(ActiveForm)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jan 2006 02:01:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1055</guid>
      <author>Namor ()</author>
    </item>
    <item>
      <title>Defining a custom validates in rails</title>
      <link>http://snippets.dzone.com/posts/show/822</link>
      <description>in the model:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class User &lt; ActiveRecord::Base&lt;br /&gt;  require 'validations'&lt;br /&gt;&lt;br /&gt;  validates_positive_or_zero :number&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;in /lib/validations.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def validates_positive_or_zero(*attr_names)&lt;br /&gt;  configuration = { :message =&gt; "Cannot be negative" }&lt;br /&gt;  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;  validates_each attr_names do |m, a, v| m.errors.add(a, configuration[:message]) if v&lt;0 end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 19 Oct 2005 21:47:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/822</guid>
      <author>zzzrByte (Tal Ater)</author>
    </item>
  </channel>
</rss>
