We have been using presenters much more often in our Rails code, with the goal of having views only use “Mustache-level” logic (conditionals, loops, and interpolation). We’ve been happy with the results so far; it really cleans up the views, and moves logic to easily testable objects. In fact, most tests for presenters can be fast specs and still test well.
I’ve gotten the impression that not everyone does presenters the same way, so before getting to the code I’ll clarify. Our presenters are decorator-like, with a restriction that they only transform values, not make any lasting changes to them. Methods are typically “opted-in” for use in the presenter using def_delegators from Forwardable. For a simple example, PersonPresenter might have a method called full_name that combines first_name and last_name; the Person class only manages the persistance of first_name and last_name. That is, presenters wrap an object with methods for presentation to a human.
Not all of Rails makes this pattern easy to use. We ran into an issue recently when calling dom_id() in a view. This was our code, roughly:
# File: app/presenters/foo_presenter.rb
@foo = foo
# File: app/controllers/foos_controller.rb
class FoosController < ApplicationController
foo = Foo.find(params[:id])
# It's nice to only have to expose a single object to the view.
@foo_presenter = FooPresenter.new(foo)
The obvious fix would be to add a to_key method that just delegates to the model, but that quickly became a rabbit hole. Many more methods were needed, and I quickly gave up.
It turns out that dom_id() follows a protocol in that it will call to_model on whatever’s passed in. We can use that to solve our problem. (ActiveRecord objects just return self, as you might imagine.) Here’s the fix:
# File: app/presenters/foo_presenter.rb
# ... (previous content)
Now the presenter can be used with dom_id(), delegating to the presented model. It’s a surprisingly simple solution. :)
Our long distance move last year followed these posts pretty closely, although this was my first time reading them. We had moved using a rented truck previously, and knew we didn’t want to spend that much again just to move non-essentials.
Although I can’t say it will be easy, I can recommend paring your belongings down before moving 1000 miles. It’s funny how quickly new things can accumulate in the year after the move, though… :)
Edit: To clarify, no we aren’t moving a long distance again. This was just some reflection on our move for others. Sorry for any confusion!
This bug, on the other hand, is likely to cause all sorts of issues as it becomes more well known. Any application that uses Apple’s CoreText will crash if it sees a certain string of Arabic characters. There aren’t currently any security implications, and I admittedly thought of sending the text to Mac-using friends. However, if your browser crashes every time you view a certain site or text message, this is why. For some reason, this bug reminds me of the ping of death (e.g., in early versions of Windows).
I used to use Instapaper often, but was turned off by the price for mobile apps and (former?) incompatibility with Android. Their web app isn’t very mobile friendly either. Over time, I started using Instapaper less and less… Pocket seemed like a nice alternative, but I couldn’t really bring myself to switch.
Looking for a self-hosted alternative to other “read later” apps? I’ve been pretty happy with Poche (PHP, demo) so far. I even deleted my Instapaper account and switched to using Poche instead. It’s nice and lightweight, with a responsive design that works on phones, tablets, and desktops.
I haven’t gotten an offline version working (there’s an Android app though, and an export to ePub feature seems likely). My only complaint so far? A lot of the user community only seems to discuss Poche in French (“poche” means “pocket” in French) — certainly something I’m not accustomed to in a largely Anglophone open source community.
Looks pretty awesome, but I’d have a really hard time justifying spending $150 on a keyboard to myself. Highlights: backlit, good media keys, detachable micro USB cord (might work well with an Android tablet with USB OTG), and no Windows logo. :-)
That’s the Onion telling CNN how to be a respectable news organization. The Onion, the fake news site. What’s happened to journalism? If the only thing you care about is metrics like page views, you can easily be led astray. This really shouldn’t have been bigger news than Syria or the anniversary of the “I Have a Dream” speech. This isn’t the first time CNN has done something like this, either.
I’ve been a big fan of Tiny Tiny RSS since switching from the now defunct Google Reader. (It’s still hard for me to believe Google discontinued it.) I had a small Flattr balance from users of my open source software and readers of this blog, so I decided to pay it forward by flattring them. I still love the Flattr concept, and hope it will continue to spread.
Seems pretty interesting; I’ve been curious about it when I walk by. It’s interesting that Teddy’s Bigger Burgers is only in a few places: Hawaii, Japan, Washington, and now Iowa. I skimmed through their menu, and was happy to see they have vegetarian friendly fare alongside their kailua style and chili cheeseburgers. Sounds worth a try. :)