Created
July 14, 2012 09:02
-
-
Save the-teacher/3110116 to your computer and use it in GitHub Desktop.
EvilMartians First task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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