Live data from Hacker News

On logic in a Rails app, revisited 6 years later

alisnic.github.io

31–33 of 33 posts

Re: On logic in a Rails app, revisited 6 years later

#31
post #20
post #17

Been doing Rails development for 6+ years. The most maintainable codebases I've worked on had some kind of service layer between the controller and the model. We used the "interactor" gem to create individual units of business logic that we could reuse and piece together into larger "flows". Business logic stayed in the interactors, persistence logic in the models. This lead to skinny controllers, skinny models and m…

My team discovered the interactor pattern about 8 months ago and never looked back. It’s worked extremely well and easy to read. We’ve found it necessary to maintain a sensible naming convention for the interactors and to namespace them. The other pattern we’ve found to work decently well is to ensure most operations are idempotent. It makes it easy to ensure the correct state.

What are some of the parts to your naming conventions if there are any prevailing patterns?

Re: On logic in a Rails app, revisited 6 years later

#32
post #20

Earlier quoted context omitted.

My team discovered the interactor pattern about 8 months ago and never looked back. It’s worked extremely well and easy to read. We’ve found it necessary to maintain a sensible naming convention for the interactors and to namespace them. The other pattern we’ve found to work decently well is to ensure most operations are idempotent. It makes it easy to ensure the correct state.

What are some of the parts to your naming conventions if there are any prevailing patterns?

Currently we're experimenting with namespacing per "ownership" of that namespace.

For instance, if the interactor is in charge of onboarding a new account, we have it under something like this:

Account::SetupOnboarding

Note that we also have an Account model here and so anything namespaced under it will be assumed to take place in that context.

Re: On logic in a Rails app, revisited 6 years later

#33
post #29

I've found that a pretty simple technique along the lines of what's shared in the article makes complex Rails apps much more maintainable. Most of this applies to any MVC style app/framework. I follow these rules of thumb: 1. Controllers should only handle converting HTTP to ruby calls. That includes logic that is specific to the request flow, like parsing params or authenticating cookies, but nothing else. 2. Models…

> 3. What Rails calls "views" should be though of as simple html templates with loops and simple if/else, but no complex logic. I rather like how Phoenix makes a clear separation between views and templates. The templates have no logic, but the views can have quite a bit of it as long as it is directly linked to the templates (presentational).

When I first looked at Phoenix ~18 months ago, I was a fairly fresh developer and couldn't figure out the separation of Views and Templates (coming from Rails).

I'm just now going through the Programming Phoenix 1.4 book and it's just been one 'aha' moment after the other, with the separation of logic and views being one of the most significant.

Post reply on HN