Skip to content

Instantly share code, notes, and snippets.

@pikender
Last active April 11, 2016 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pikender/f58b2208ae8951c7b13214bf320e8ec1 to your computer and use it in GitHub Desktop.
Save pikender/f58b2208ae8951c7b13214bf320e8ec1 to your computer and use it in GitHub Desktop.
Ecto Model Schema Extension
defmodule Nectar.ExtendProduct do
Module.register_attribute(__MODULE__, :schema_changes, accumulate: true)
add_to_schema = fn(schema_change) -> Module.put_attribute(__MODULE__, :schema_changes, schema_change) end
add_to_schema.(quote do: (field :special, :boolean, virtual: true))
defmacro extensions do
@schema_changes
end
end
defmodule Nectar.Product do
import Nectar.ExtendProduct
schema do
extensions
end
## Add #special boolean to get casted
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, ~w(special))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment