Created
December 11, 2017 18:59
-
-
Save mattraykowski/61fc9289f7231165a4e43bb25021a606 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule Vassal.Game do | |
# ... bunch of stuff .. | |
@doc false | |
def changeset(%Game{} = game, attrs) do | |
game | |
|> cast(attrs, [:name, :year, :players, :length, :series, :description]) | |
|> cast_assoc(:publisher) | |
|> cast_assoc(:era) | |
|> cast_assoc(:topic) | |
|> cast_scale(:scale) | |
|> validate_required([:name, :year, :players, :length, :series, :description]) | |
|> unique_constraint(:name) | |
end | |
def cast_scale(changeset) do | |
scale = case Map.get(changeset.params, "scale", %{}) do | |
%{ "id" => id } -> | |
Vassal.Games.get_scale(id) | |
%{ "name" => name } -> | |
%Scale{ name: name } | |
_ -> | |
%Scale{} | |
end | |
put_assoc(changeset, :scale, scale) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment