Hacker News new | ask | show | jobs
by clienthunter 4475 days ago
Not sure I agree with the idea that `Rails == UI`. A lot of the time I'm trying to fight Rails on this as decoupling HTML means refactoring HTML which isn't fun.

I use Rails as a thing that controls boundaries, a glorified CGI really. It's a router, input sanitizer, and general plumbing that spurts out JSON every now and then. It's a mid-level wall in a fortress: server + OS deal with TCP, Rails + Rack handle HTTP, Rails handles raw user input, proxy objects handle Rails, the actual business logic handles proxy objects.

This is the only workflow that allows me and Rails to be long-term friends:

When a request comes in to the API (say, change password) the controller will sanitize and confirm presence of the right params and collect things like the current user. It'll then pass these off to a proxy object e.g. `CustomerPasswordUpdater.update!(user, new_pass)` which will validate length, entropy, etc and perform any necessary crypto. Then that data gets passed off to another proxy object which abstracts over ActiveRecord e.g. `UserPersister`. There's literally nothing inside the model itself. The core `Customer` object is nothing but a decorated window on the relevant data (accessed through unambiguous proxies e.g. `UserReader`, never directly through the model).

This may seem a bit framework-inside-a-framework but the result of this approach is that specific chunks of logic - your logic, the stuff you write on a whiteboard - is clearly identifiable in these small unambiguous classes.

The grand upshot is that I can plan my application on a piece of paper, write all my logic in plain old Ruby with plain old RSpec and plain old gems at breakneck speed, and there's no stupidity like tests hitting the DB or 40 minute test runs in sight.

Once all that's done wiring it up to Rails takes a day or two. At any point I could write a few new proxy objects and drop the whole thing in to Sinatra&Sequel/ Sinatra&Neo4j/ Sinatra&PStore/ Whatever&Whatever. At no point does the logic that defines and encapsulates the actual business problem that needed solved need to be touched.