Closed johnmosesman closed 7 years ago
I noticed that the Record docs show it using the model()
function. Does the README have a typo?
This works for me:
defmodule Hunting.Api.OutfitterController do
use Hunting.Web, :controller
use JaResource
plug JaResource
def model, do: Hunting.Outfitter
def record(query, slug_as_id) do
model()
|> Hunting.Repo.get_by(slug: slug_as_id)
end
def records(%Plug.Conn{}) do
raise "hi" # this never gets called
end
end
I am still confused why records/1
isn't called. Based on the docs description ("record/2
receives the results of records/1
") I would think that hook gets fired before passing its result to records/2
. Is that incorrect?
Hi @johnmosesman you are totally right, the readme is completely wrong. Sorry about that!
All callbacks except model/0
and repo/0
get the %Plug.Conn{}
as the first argument. The default implementation is really:
def model, do: MyModel
def records(_conn), do: model()
def record(conn, id), do: conn |> records |> repo.get(id)
So in your case you should be able to only override record with:
def record(conn, slug), do: conn |> records |> repo.get_by(slug: slug)
I will leave this issue open to remind myself to update the README, thanks for reporting it and sorry for any inconvenience!
No worries! A little doc diving never hurt anyone :)
Would you want me to submit a PR for the change?
A PR would be very welcome!
Hi there,
I am trying to use the
record/2
hook described in the README. When I inspect thequery
I see it's a%Plug.Conn{}
, which errors when piping it to theRepo
with the error:Here's my controller:
Am I making a mistake somewhere? I also noticed that the
records/1
function is not called either.Full error: