summaryrefslogtreecommitdiff
path: root/lib/aggiedit/rooms/room.ex
blob: 2a23555368cf2a36bada843c56048547b715c95a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
defmodule Aggiedit.Rooms.Room do
  use Ecto.Schema
  import Ecto.Changeset

  schema "rooms" do
    field :domain, :string

    has_many :users, Aggiedit.Accounts.User
    has_many :posts, Aggiedit.Rooms.Post

    timestamps()
  end

  @doc false
  def changeset(room, attrs) do
    room
    |> cast(attrs, [:domain])
    |> validate_required([:domain])
    |> validate_length(:domain, max: 160)
    |> validate_format(:domain, ~r/^[^\s\.]+\.[^\.\s]+$/, message: "Domain cannot be a subdomain, and cannot have spaces")
    |> unique_constraint(:domain) 
  end
end