Skip to content

Instantly share code, notes, and snippets.

@the-teacher
Created July 14, 2012 09:02
Show Gist options
  • Save the-teacher/3110116 to your computer and use it in GitHub Desktop.
Save the-teacher/3110116 to your computer and use it in GitHub Desktop.
EvilMartians First task
# encoding: utf-8
class EmailPatternValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value.nil? || !value.match(/\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/)
record.errors[attribute] << "HA HA! Not a valid email address"
end
end
end
class User < ActiveRecord::Base
validates :email, :presence => true, :uniqueness => true, :email_pattern => true
def rating
pokemons.all.inject(0.0) { |sum, pokemon| sum + pokemon.rating }
end
def can_be_charged?(amount)
self.balance >= amount
end
def can_be_charged
unless self.can_be_charged?(100)
errors.add(:user, "не обладает достаточной суммой на балансе")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment