I hit a bit of a problem. I’m writing a quotation system for my company.
I have an entity called EstimateType – it is the driver for the rest of the setup. Basically it’s a name and it’s used to group the pieces of the estimate together, so you have, say, small business, or sole trader, and they each may have the same parts of the quote but the details will be different (for example sole traders are generally taught one to one and we charge a flat fee not per delegate).
I built a prototype front end in Ember and used the mirage library.
Using Mirage I just sent the EstimateTypeId to the app and it worked.
The back end’s in Phoenix and I’m using a library called ja_serializer that’s supposed to support Ember out of the box. Having done some experimenting with hand building stuff that can talk to Ember I think this is a good idea and will save some time.
The code generated by this library puts the parent up away in a different part of the JSON from the main data, in a place called relationships. This would be fine (I suppose) but the ID doesn’t end up getting saved either by the generated controllers, or by the generated change sets (I had to add it in).
I’m really not convinced this is right.
10 Apr
The generator doesn’t do parent child properly. It essentially generates the same set of models, tests and controllers that you would get if there were no parent. This is a bit useless and is what got me confused.
I added in some more methods to the tests that create the parent entity and then put it into the queries and structs used by the wrapper methods in the Estimates module (which is the main one for this part of the app).
I’m still a bit meh about having to put things into a module for different parts of the app, which I think came in with 1.3. It’s nice, but often those decisions at the beginning of a development or design run will seem not quite right, and then you get into the problem of asking yourself if it’s worth moving things around. I’d far rather have the problem of deciding if it was worth pushing things into domains and/or namespaces because my app had become overcomplex. It feels like adding an extra layer of indirection for its own sake, and I’ve had enough of that from the days I used to write lots of Java.
Now I have a set of tests that run, and controllers that do parent child correctly.
I did get somewhat hampered where my CI system was failing and not deploying when the tests were running locally. Have since worked out that this was because
MIX_ENV=test mix ecto.reset
Has running the seeds built into the aliases. I’ve since added these aliases to my mix.exs file:
defp aliases do [ "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], "ecto.setup-noseed": ["ecto.create", "ecto.migrate"], "ecto.reset-test": ["ecto.drop", "ecto.setup-noseed"], "test": ["ecto.create --quiet", "ecto.migrate", "test"], ] end
And now I do ecto.reset-test if I want to trash the test db. I still haven’t worked out how to tell mix to always run this with the test environment, but not worrying about that now.
I’ve also added
{:mix_test_watch, "~> 0.5", only: :test}, {:ex_unit_notifier, "~> 0.1", only: :test},
To my deps, so that I can run the equivalent of guard. Test watch autosets the environment to test but I added only: test because I didn’t want the dep in my production setup. It does mean I need to put MIX_ENV=test onto the command line or it won’t compile and run, but it’s no great hardship.
Later the same day
I must have used the wrong generator commands because this now seems to at least attempt to create the parent records
mix ja_serializer.gen.phx_api Estimates Estimate estimates company_name:string aspiration:string prepared_for:string email:string body:map estimate_type_id:references:estimate_types
The estimates tests now contain
estimate_type = Repo.insert!(%RosieEstimatesServer.Estimates.EstimateType{})
In the controller tests. The tests are still all busted, but at least there’s a starter for 10 there now where there wasn’t before. I still had to set up an alias for Repo, though.
And another thing
The autogenerated change sets don’t have the parent id in them – maybe they’re supposed to be used differently – but in the absence of any decent examples it’s a bit hard to get to the bottom of.
In all cases I’ve had to add estimate_type_id to the cast and validate_required clauses in the model files.
In addition
|> foreign_key_constraint(:estimate_type_id)
Wasn’t put in automatically, which seems a bit weird.
Meh
In order to get errors to be returned in a format Ember likes I needed to change views/changeset_view.ex so that it returned the errors in a compatible list
def render("error.json", %{changeset: changeset}) do %{errors: errors_to_json_format(translate_errors(changeset))} end defp errors_to_json_format(errors) do errors |> Enum.map(fn {k, v} -> %{detail: List.first(v), source: %{ pointer: "data/attributes/#{k}"}} end) end
As in the old format isn’t supported any more. This code needs a bit more refactoring but right now it works. Thanks to this guy for the tip.
Also Ember pluralises the entity name so the controller methods needed to be changed
- def create(conn, %{"data" => _data = %{"type" => "estimate", "attributes" => estimate_params}}) do + def create(conn, %{"data" => _data = %{"type" => "estimates", "attributes" => estimate_params}}) d
As in, pluralise the type.
Happy days.
And …
import { underscore } from '@ember/string'; ... keyForAttribute(attr) { return underscore(attr); }
In the serializer config – because elixir inbound wants underscores and I lost patience with JSON API sez X pedantry 🙂