summaryrefslogtreecommitdiff
path: root/users/app/models/identity.rb
blob: 4dff93a1c83b48b1bb3181f88b7208502f3a0c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Identity < CouchRest::Model::Base

  use_database :identities

  belongs_to :user

  property :address, LocalEmail
  property :destination, Email

  validate :unique_forward
  validate :alias_available

  design do
    view :by_user_id
    view :by_address_and_destination
    view :by_address
  end

  protected

  def unique_forward
    same = Identity.find_by_address_and_destination([address, destination])
    if same && same != self
      errors.add :base, "This alias already exists"
    end
  end

  def alias_available
    same = Identity.find_by_address(address)
    if same && same.user != self.user
      errors.add :base, "This email has already been taken"
    end
  end

end