Created
July 24, 2015 17:44
-
-
Save joseluistorres/7b5cccda1eba5ef0a92b to your computer and use it in GitHub Desktop.
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
defmodule Ph_14Version.Field do | |
use Ph_14Version.Web, :model | |
use Ecto.Model.Callbacks | |
before_insert :set_map_coords | |
schema "fields" do | |
field :name, :string | |
field :map_coords, Geo.MultiPolygon | |
timestamps | |
end | |
@required_fields ~w(name map_coords) | |
@optional_fields ~w() | |
@doc """ | |
Creates a changeset based on the `model` and `params`. | |
If no params are provided, an invalid changeset is returned | |
with no validation performed. | |
""" | |
def changeset(model, params \\ :empty) do | |
model | |
|> cast(params, @required_fields, @optional_fields) | |
end | |
def set_map_coords(changeset) do | |
map_coords = Ecto.Changeset.get_field(changeset, :map_coords) | |
change(changeset, %{map_coords: Geo.JSON.decode(map_coords)}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need to parse this to insert a record
This will decode it to be a Geo.MultiPolygon
Geo.JSON.decode(map_coords)