-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ja_resource #332
Add ja_resource #332
Conversation
One issue that's unclear to me right now is how to do the |
5634e99
to
efc2462
Compare
Don't yet understand how to use |
Also is escaping me how to do |
Some filters like We'll need to find a way to rework these, either on the Ember side or in the way we're handling the request in the controller. |
Also not sure how to do the slugged route controller. |
Have not done the task controller yet. |
Decent indication of what still needs a good amount of work:
|
I have no idea if changesets are being handled here correctly at all via the |
Also these will help some:
|
I think we could probably cut out between 150-200 LOC more if we figure out some of the above issues. |
|> Map.get("data") | ||
|> assert_result_id(membership.id) | ||
|> assert_role("admin") | ||
conn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Good catch. Didn't notice we weren't using response
.
Looked into the ja_resource code and this is what a create callback looks like: def call(controller, conn) do
merged = JaResource.Attributes.from_params(conn.params)
attributes = controller.permitted_attributes(conn, merged, :create)
conn
|> controller.handle_create(attributes)
|> JaResource.Create.insert(controller)
|> JaResource.Create.respond(conn)
end
For the first two of these, if they return a changeset, then For For analytics, we might be able to create a plug that goes at the end of the line, after ja_resource does it's thing. On success, For authorization, however, I'm not seeing a way to create a plug. Instead, our With some amount of work, we could make this work to a point where a lot of our controllers would have some or all of these: def handle_create(conn, attributes) do
changeset = %Task{} |> Task.create_changeset(attributes)
case conn |> authorize(changeset) do
true -> changeset # ja_resource will continue
false -> conn |> handle_not_authorized # conn will halt
end
end
def handle_update(conn, model, attributes) do
changeset = model |> Task.update_changeset(attributes)
case conn |> authorize(model/changeset) do
true -> changeset # ja_resource will continue
false -> conn |> handle_not_authorized # conn will halt
end
end
def handle_delete(conn, model) do
case conn |> authorize(model) do
true -> model # ja_resource will continue
false -> conn |> handle_not_authorized # conn will halt
end
end For slugs, we would override the def record(conn, _id) do
params = conn |> get_params_we_need_to_find_record
Task |> repo.get_by(params)
end For custom index filters, we override records def records(conn) do
params = conn |> get_index_filter_params_we_need
Task.index_filters(params) |> repo.all
end As I said, for analytics, we might be able to create a plug, but i'm worried that, since I also see you've created an issue in |
I created an issue of my own and the author replied with basically the same suggestion I have: vt-elixir/ja_resource#32 (comment) |
@begedin should we temporarily bring back the six or so places where |
afc5227
to
2a990e3
Compare
@joshsmith I did a spike for analytics that reintroduces the tracking we removed, while keeping ja_resource. I also implemented a method to significantly trim down the amount of code in our Segment module once we completely switch. While doing this, I realised where not really testing tracking in any proper way. Now that controller actions pass through segment, I really think we need some sort of testing for that, but I'm not sure what the best approach is. Ideally, we test the code we have by abstracting the part out of our control (direct segment calls) as much as possible and then somehow injecting our own layer for testing. I'll look into how to do that. |
@joshsmith I went ahead and implemented the new tracking in all controllers we touched on in one commit. Then I moved on to add ja_resource and tracking to all those controllers that have tracking, but we haven't touched them yet, in separate commits. I also, due to reasons above, increased the abstraction of our segment layer, by basically pulling two tiny functions (identify and track) to an outside module. Now our segment module is the one that handles the injection of the newly abstracted api layer, instead of our controllers. Once I did that, several tests started failing due to newly discovered bugs, so I believe it was well worth the trouble. Also, this will allow us to write more tests for our segment layer. What's really left here is just adding ja_resource to the controllers that don't have it yet. These also don't have tracking, so they do not prevent us from merging, and can be done separately. We can wrap up the remaining endpoints in a separate PR, then switch to bodyguard in a whole new PR. We can also make a PR to test our segment layer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some quick notes here.
Also would request that we spend some time doing inline documentation of this module and their functions. I don't think we should be approving any PRs with new functionality – even on our own – that don't have inline docs.
|
||
@actions_without_properties [:updated_profile, :signed_in, :signed_out, :signed_up] | ||
|
||
def track({:ok, record}, action, %Plug.Conn{} = conn) when action in @actions_without_properties do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any way to pattern match on the record
's type at all for extra type safety?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outside of having one function signature for every single record type, I'm not sure there is.
def handle_delete(conn, record) do | ||
record | ||
|> Repo.delete | ||
|> @analytics.track(:deleted, conn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we can just pass a record
along to ja_resource
and it won't try to do anything else with it, is that right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly. That is, it won't try insert it again. It will, however, render it.
Convert category controller Convert comment controller Convert organization controller Change to use filter helpers in some controllers Convert organization membership (except create and update) Convert preview controller Convert project category controller Convert part of project controller Convert project skill controller Convert role skill controller Convert skill except for index Convert user category controller except create Convert user controller except update Convert user role except create Convert user skill controller except create Refactor helpers Switch to analytics tracking alongside ja_resource to places that lost tracking Switch OrganizationMembership update to new tracking, fix some tracking code there Switch parts we already touched to new tracking completely Switch Task controller to ja_resource, except index Switch User controller completely to ja_resource Improve abstraction of segment service. Allows for better testing. Add documentation for analytics, rewrite functions some
fc90b83
to
3e91d5c
Compare
Changes Unknown when pulling 3e91d5c on 331-add-ja_resource into * on develop*. |
Update excoveralls
3e91d5c
to
f6775e0
Compare
Changes Unknown when pulling f6775e0 on 331-add-ja_resource into * on develop*. |
|
Closes #331.
Will need documentation to explain to new contributors what's happening, mostly by linking to the
ja_resource
repo.JaResource
plugLots of little issues listed below in comments.