elixir-lang/ecto — pull request #156 by TanYewWei (2014-01-14 23:07:22+00:00)
Title: Riak @queryable_defaults 20140115

When building my [riak-adapter](https://github.com/TanYewWei/ecto/tree/riak-adapter), I ran into a scenario whereby I wanted to define entity fields specific to Riak in order to manage migration and conflict resolution.

I did not want users to have to constantly specify these fields in their model definitions, but also wanted to give them the ability to override fields if needed.

For example, you need to set the `:riak_version` field on any entity persisted to Riak. This should default to version 0, but the user should have the ability to define an incremented version for dynamic migrations.

I used this in the [`Ecto.RiakModel` module](https://github.com/TanYewWei/ecto/blob/riak-adapter/lib/ecto/adapters/riak/model.ex#L22) to define 3 custom fields which are used to track versioning and conflict resolution.

The changes made include:

1. A `:default_fields` option to `@queryable_defaults` to specify fields to set by default on an entity. This will be a list of 3-tuples `{ field_name :: atom, field_type :: atom, options :: Keyword.t }`

2. An `:overridable?` entity option to allow modules which use a particular `@queryable_defaults` spec to override already defined fields. This is a boolean value.

The snippet from `test/ecto/model/queryable_test.exs` below should show the intended usage pretty well.

Let me know if the changes make sense for Ecto. Thanks!