You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We want to move away from the ja_resource and canary combo used in most of our controller modules. The central issue for this project is #864
There is a generic process of how to do it in that issue, as well as tracking for the milestone progress.
The procedure is also pasted below. If this specific controller deviates from this standard procedure, feel free to ask for help here, or by opening a PR.
The PR should indicate that it's closing this issue and is progressing #864 further along.
Procedure
pick a controller to switch
view actions this controller should support in lib/code_corps_web/router.ex
add action_fallback CodeCorpsWeb.FallbackController. This makes it so in case a with clause within the controller action fails, a sort of "catch all" function will be called from FallbackController
add plug CodeCorpsWeb.Plug.DataToAttributes. This converts the data hash within the conn params from the json api structure into a flat map convenient for usage with changesets
add plug CodeCorpsWeb.Plug.IdsToIntegers. This casts all id parameters into integers, since this is what our authorization layer and Phoenix in general expects, while ember is sending strings.
for each action
remove that action from any load_resource, load_and_authorize_resource, etc. plugs in that controller
remove any def handle_#{action} functions
write a def #{action}(conn, params) do. For the standard create, updatedelete, show and index actions. See examples below on how these functions should be written, usually
rewrite the policy function for that action to support params instead of a changeset if necessary. See section below for pointers.
That should be the whole procedure. We already have controller tests in place, so if the process has been done correctly, the tests should pass.
Some additional steps might be necessary, however, if there are some specifics to an action. For those, we will be here to provide feedback.
authorize action for current user and parameters (requires rewriting that policy to use a params map instead of a changeset, expect {:ok, :authorized}
perform insert action
render resulting resource
update and delete actions
load resource
authorize action for current user on resource, expect {:ok, :authorized}
perform update/delete
render resource, or no content for delete
Examples in code
-CodeCorpsWeb.CommentController has examples for standard index, show, create and update actions
How to rewrite policies
This consists of several relatively simple steps.
For each action being switch away, we need to move the def can function clause out of the defimpl block in policy.ex into the root namespace of the CodeCorps.Policy module.
We also need to make the clause private instead of public.
If any of the clauses expect a changeset, they should now expect a simple map of params:
in this case, the policy module itself also need to be rewritten to support a params map. Since in most cases, these policies simply look at the changeset.changes map, the rewrite ought to be simpler. If there is trouble, someone will be available to provide pointers.
Examples in code
the rewrite has been done for Policy.Comment.create?
The text was updated successfully, but these errors were encountered:
Progress on #864
Problem
We want to move away from the
ja_resource
andcanary
combo used in most of our controller modules. The central issue for this project is #864There is a generic process of how to do it in that issue, as well as tracking for the milestone progress.
The procedure is also pasted below. If this specific controller deviates from this standard procedure, feel free to ask for help here, or by opening a PR.
The PR should indicate that it's closing this issue and is progressing #864 further along.
Procedure
lib/code_corps_web/router.ex
action_fallback CodeCorpsWeb.FallbackController
. This makes it so in case a with clause within the controller action fails, a sort of "catch all" function will be called fromFallbackController
plug CodeCorpsWeb.Plug.DataToAttributes
. This converts thedata
hash within the conn params from the json api structure into a flat map convenient for usage with changesetsplug CodeCorpsWeb.Plug.IdsToIntegers
. This casts all id parameters into integers, since this is what our authorization layer and Phoenix in general expects, while ember is sending strings.load_resource
,load_and_authorize_resource
, etc. plugs in that controllerdef handle_#{action}
functionsdef #{action}(conn, params) do
. For the standardcreate
,update
delete
,show
and index actions. See examples below on how these functions should be written, usuallyThat should be the whole procedure. We already have controller tests in place, so if the process has been done correctly, the tests should pass.
Some additional steps might be necessary, however, if there are some specifics to an action. For those, we will be here to provide feedback.
How to rewrite controller actions
So to recap, the actions work like
index
actionshow
actioncreate
action{:ok, :authorized}
update
anddelete
actions{:ok, :authorized}
Examples in code
-
CodeCorpsWeb.CommentController
has examples for standardindex
,show
,create
andupdate
actionsHow to rewrite policies
This consists of several relatively simple steps.
For each action being switch away, we need to move the
def can
function clause out of thedefimpl
block inpolicy.ex
into the root namespace of theCodeCorps.Policy
module.We also need to make the clause private instead of public.
If any of the clauses expect a changeset, they should now expect a simple map of params:
becomes
changeset.changes
map, the rewrite ought to be simpler. If there is trouble, someone will be available to provide pointers.Examples in code
Policy.Comment.create?
The text was updated successfully, but these errors were encountered: