Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'active_model'
class Person
include ActiveModel::Validations
attr_accessor :name, :age, :size
def initialize name: nil, age: nil, size: nil
@name = name
@age = age
@size = size
end
validates :name, presence: true
end
person = Person.new
p "With empty name is valid?: #{person.valid?}"
james = Person.new name: 'James'
p "With filled name is valid?: #{james.valid?}"
incognito = Person.new name: nil
p "Without nil name: #{incognito.valid?}"
p person.errors.messages
p person.errors.any?
p person.errors[:name].any?
p person.errors[:age].any?
p person.errors.details[:name]
puts 'CONFIRMATION'
class Human
include ActiveModel::Validations
attr_accessor :age, :age_confirmation
def initialize age: nil, age_confirmation: nil
@age = age
@age_confirmation = age_confirmation
end
validates :age, confirmation: true
end
human = Human.new age: 12, age_confirmation: 12
p human.valid?
human = Human.new age: 12, age_confirmation: 22
p human.valid?
puts 'EXCLUSION'
class House
include ActiveModel::Validations
attr_accessor :room, :size
def initialize room: nil, size: nil
@room = room
@size = size
end
validates :room, exclusion: { in: [-2, -1, 0], message: "The value: %{value} is reserved." }
validates :size, inclusion: { in: %w(small medium large), message: "The value: %{value} is not permitted." }, allow_nil: true
end
house = House.new room: 2
p house.valid?
house2 = House.new room: 0
p house2.valid?
p house2.errors.messages
puts 'INCLUSION'
house3 = House.new size: 'large'
p house3.valid?
house4 = House.new size: 'mmm'
p house4.valid?
p house4.errors.messages
puts 'FORMAT'
class People
include ActiveModel::Validations
attr_accessor :name, :surname
def initialize name: nil, surname: nil
@name = name
@surname = surname
end
validates :name, format: { with: /\A[a-zA-Z]+\z/, message: "%{value} not allowed. Only allowed letters." }
validates :surname, length: { in: 6..20, message: "the %{value} is too short" }
end
people = People.new name: 'Michael'
p people.valid?
people2 = People.new name: 'pavlik5727'
p people2.valid?
p people2.errors.messages
puts 'LENGTH'
people3 = People.new surname: 'Bratanov'
p people3.valid?
people4 = People.new surname: 'Lee'
p people4.valid?
p people4.errors.messages
puts 'NUMERICALITY'
class Team
include ActiveModel::Validations
attr_accessor :size, :name, :chemp
def initialize size: nil, name: nil, chemp: nil
@size = size
@name = name
@chemp = chemp
end
validates :size, :name, presence: true
validates :size, numericality: { only_integer: true, message: "The '%{value}' is not correct." }
validates :chemp, inclusion: {in: [true, false], message: "the %{value} is not correct"}, allow_nil: true
end
team = Team.new name: 'Football', size: 11
p team.valid?
team2 = Team.new name: 'Volleyball', size: '12a'
p team2.valid?
p team2.errors.messages
team3 = Team.new
p team3.valid?
p team3.errors.messages
team4 = Team.new name: 'Radisne', size: 10, chemp: 'daf'
p team4.valid?
p team4.errors.messages
class Police
include ActiveModel::Validations
attr_accessor :crime
def initialize crime: nil
@crime = crime
end
validates :crime, absence: true
end
punishment = Police.new crime: 12
p punishment.valid?
p punishment.errors.messages
class GoodnessValidator < ActiveModel::Validator
def validate(record)
if record.first_name == 'Evil'
record.errors[:base] << "This person is evil"
end
end
end
class Image
include ActiveModel::Validations
attr_accessor :first_name
def initialize first_name: nil
@first_name = first_name
end
validates_with GoodnessValidator
end
image = Image.new first_name: 'Dimoon'
p image.valid?
image2 = Image.new first_name: 'Evil'
p image2.valid?
p image2.errors.messages
class CrazyValidator < ActiveModel::Validator
def validate(record)
if options[:fields].any?{|field| record.send(field) == "Anfield" }
record.errors[:base] << "You'll never walk alone"
end
end
end
class Stadium
include ActiveModel::Validations
attr_accessor :name, :club
def initialize name: nil, club: nil
@name = name
@club = club
end
validates_with CrazyValidator, fields: [:name, :club]
end
stadium = Stadium.new name: 'San Siro', club: 'Milan'
p stadium.valid?
stadium2 = Stadium.new name: 'Anfield', club: 'Liverpool'
p stadium2.valid?
p stadium2.errors.messages
class CoolValidator < ActiveModel::Validator
def initialize bank
@bank = bank
end
def validate
if some_complex_condition_involving_ivars_and_private_methods?
@bank.errors[:base] << "This person is evil"
end
end
def some_complex_condition_involving_ivars_and_private_methods?
@bank.owner == 'Evil'
end
end
class Bank
include ActiveModel::Validations
attr_accessor :banknote, :owner
def initialize banknote: nil, owner: nil
@banknote = banknote
@owner = owner
end
validate do |bank|
CoolValidator.new(bank).validate
end
end
bank = Bank.new banknote: '1 Dollar', owner: 'Evil'
p bank.valid?
p bank.errors.messages
class Country
include ActiveModel::Validations
attr_accessor :name, :population
def initialize name: nil, population: nil
@name = name
@population = population
end
validates_each :name do |record, attr, value|
record.errors.add(attr, "'#{value}' must start with upper case") if value =~ /\A[[:lower:]]/
end
end
country = Country.new name: 'germany', population: 80_000_000
p country.valid?
p country.errors.messages
class Gamer
include ActiveModel::Validations
attr_accessor :name, :rank
def initialize name: nil, rank: nil
@name = name
@rank = rank
end
validates :rank, numericality: { only_integer: true, message: "The '%{value}' is not correct." }, if: :right_name?
def right_name?
name == 'Bruce'
end
end
gamer = Gamer.new name: 'Vasya', rank: 'aaa'
p gamer.valid?
p gamer.errors.messages
gamer2 = Gamer.new name: 'Bruce', rank: 'bbb'
p gamer2.valid?
p gamer2.errors.messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.