Created
June 27, 2015 19:16
-
-
Save betamatt/e9f0910d3023522fc43d to your computer and use it in GitHub Desktop.
Fully async snell controller action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update | |
user = User.find(1) | |
if user.update_attributes(params[:user]) | |
respond_with(@user) | |
else | |
respond_with(@user.errors, :status => :unprocessable_entity) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func update() -> Promise<Response> { | |
// Swift's current level of suck at type inference and closures makes this more verbose than | |
// would otherwise be expected. It should be solvable in llvm and hopefully the situation improves. | |
return User.find(1) | |
.then({ (user:Repository) -> Promise<Response> in | |
let user = user as! User | |
return user.update_attributes(["name": "Test"]) | |
.then({ (valid:Bool) -> Response in | |
if valid { | |
return Response(status: 200, body: "User updated") | |
} else { | |
return Response(status: 422, body: "Validation errors") | |
} | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks sensible, but yes, verbose.
And I'm wondering how to expose hooks to reduce the boilerplate and handle common failures. For example, implicit in the Rails version is rescuing ActiveRecord::RecordNotFound with a sensible 404 -- we're not even capturing that here.