Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active April 5, 2016 21:54
Show Gist options
  • Select an option

  • Save jendiamond/d9ce31dc1275eb5e137c9083851d13eb to your computer and use it in GitHub Desktop.

Select an option

Save jendiamond/d9ce31dc1275eb5e137c9083851d13eb to your computer and use it in GitHub Desktop.

http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html

*validate(args, &block)
Adds a validation method or block to the class. This is useful when overriding the validate instance method becomes too unwieldy and you're looking for more descriptive declaration of your validations.

This can be done with a symbol pointing to a method:

class Comment
  include ActiveModel::Validations

  validate :must_be_friends

  def must_be_friends
    errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
  end
end

With a block which is passed with the current record to be validated:

class Comment
  include ActiveModel::Validations

  validate do |comment|
    comment.must_be_friends
  end

  def must_be_friends
    errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
  end
end

Or with a block where self points to the current record to be validated:

class Comment
  include ActiveModel::Validations

  validate do
    errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
  end
end

activemodel/lib/active_model/validations/absence.rb
activemodel/lib/active_model/validations/acceptance.rb
activemodel/lib/active_model/validations/confirmation.rb
activemodel/lib/active_model/validations/exclusion.rb
activemodel/lib/active_model/validations/format.rb
activemodel/lib/active_model/validations/inclusion.rb
activemodel/lib/active_model/validations/length.rb
activemodel/lib/active_model/validations/numericality.rb
activemodel/lib/active_model/validations/presence.rb
activemodel/lib/active_model/validations/with.rb

validates_absence_of, validates_acceptance_of, validates_confirmation_of, validates_exclusion_of, validates_format_of, validates_inclusion_of, validates_length_of, validates_numericality_of, validates_presence_of, validates_size_of

validates_acceptance_of
`:accepted` (“must be accepted”)

validates_associated
`:invalid` (“is invalid”)

validates_confirmation_of
`:confirmation` (“doesn’t match confirmation”)

validates_exclusion_of
`:exclusion` (“is reserved”)

validates_format_of
`:invalid` (“is invalid”)

validates_inclusion_of
`:inclusion`(“is not included in the list”)

validates_length_of
`:too_short` (“is too short (minimum is {{count}} characters)”)
`:too_long` (“is too long (maximum is {{count}} characters)”)

validates_length_of (with :is option)
`:wrong_length` (“is the wrong length (should be {{count}} characters)”)

validates_numericality_of
`:not_a_number` (“is not a number”)

validates_numericality_of (with :odd option)
`:odd` (“must be odd”)

validates_numericality_of (with :even option)
`:even` (“must be even”)

validates_numericality_of (with :greater_than option)
`:greater_than` (“must be greater than {{count}}”)

validates_numericality_of (with :greater_than_or_equal_to option)
`:greater_than_or_equal_to` (“must be greater than or equal to {{count}}”)

validates_numericality_of (with :equal_to option)
`:equal_to` (“must be equal to {{count}}”)

validates_numericality_of (with :less_than option)
`:less_than` (“must be less than {{count}}”)

validates_numericality_of (with :less_than_or_equal_to option)
`:less_than_or_equal_to` (“must be less than or equal to {{count}}”)

validates_presence_of
`:blank` (“can’t be blank”)

validates_uniqueness_of
`:taken` (“has already been taken”)

Errors

Rails errors is handling by ActiveModel::Errors, which generate error messages with attribute name and error type.

Create custom error pages dynamically in ruby on rails 4

https://mattbrictson.com/dynamic-rails-error-pages https://salayhin.wordpress.com/2015/04/17/create-custom-error-pages-dynamically-in-ruby-on-rails-4/


For Gems

I have seen many times that you define exceptions in this way:

gem_dir/lib/gem_name/exceptions.rb and defined as:

module GemName

  class AuthenticationError < StandardError; end
  class InvalidUsername < AuthenticationError; end

end

an example of this would be something like this in httparty

For Ruby on Rails

Put them in your lib/ folder under a file called exceptions.rb, which would look something like this:

module Exceptions
  class AuthenticationError < StandardError; end
  class InvalidUsername < AuthenticationError; end
end

and you would use it like this:

raise Exceptions::InvalidUsername

http://stackoverflow.com/questions/5200842/where-to-define-custom-error-types-in-ruby-and-or-rails

This is how I setup errors in a recent project:

In lib/app_name/error/base.rb

module AppName
    module Error
        class Base < StandardError; end
    end
end

and in subsequent custom errors, like in lib/app_name/error/bad_stuff.rb

module AppName
    module Error
        class BadStuff < ::AppName::Error::Base; end
    end
end

You should then be able to call your errors via:

raise AppName::Error::BadStuff.new("Bad stuff just happened")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment