initial commit.
[reserve_usernames.git] / lib / reserve_usernames / validate.rb
1 module ReserveUsernames::Validate
2   def self.included(base)
3     base.class_eval do
4
5       validate :check_if_address_is_taken
6
7       protected
8
9       #
10       # Only query ReservedUsername if this is a new record.
11       # Otherwise, validation will always fail because the address
12       # has been taken... by this identity.
13       #
14       def check_if_address_is_taken
15         if new_record? && address
16           username = address.split('@').first
17           if ReservedUsername.find(username)
18             Rails.logger.info("ReserveUsernames - #{username} is taken.")
19             errors.add :address, :taken
20           end
21         end
22       rescue ActiveResource::ResourceNotFound
23       end
24
25     end
26   end
27 end