Close and Go BackBack to Viget

Creating Resourceful Plugins

Ben Scofield
Ben Scofield, Development Director, September 30, 2008 6

It's been some time since we originally proposed the idea of resourceful plugins—a solution to the persistent problem of integrating distinct Rails applications. Over the past year, we've worked on refining the approach (in the context of our two main resourceful projects, Sandstone and Bloget), and in the course of preparing for the tutorial I gave at Railsconf Europe, we've been able to formalize (somewhat) the process of creating resourceful plugins.

Extraction

Like most good, reusable code, the best resourceful plugins are extracted from an existing app, not created ex nihilo. I generally recommend creating a new plugin only after you've built the same functionality into an application at least twice—that way, you've worked through the same problems at least two times, and should be able to create a much better solution than you'd have been able to at the start.

Once you've built the functionality into an application, the extraction process isn't that complicated. You start with the Rails plugin generator (in the examples that follow, I'll use the Bagpipes plugin):

Continue reading "Creating Resourceful Plugins"

Scala: The Adventure Begins!

Clinton R. Nixon
Clinton R. Nixon, Senior Developer, September 24, 2008 4

I’ve been enthusiastic about the Scala programming language for a few months now, and this week’s been very exciting for the Scala community. A new Scala book from the Pragmatic Programmers was announced today, and Alex Payne from Twitter gave a presentation at C4 that strongly indicates Twitter’s writing part of their platform in Scala. Given this week’s news, I thought I’d explain why I’m using Scala, and what that means for the Lab.

Continue reading "Scala: The Adventure Begins!"

Protip: TimeWithZone, All The Time

David Eisinger
David Eisinger, Web Developer, September 10, 2008 0

If you’ve ever tried to retrieve a list of ActiveRecord objects based on their timestamps, you’ve probably been bitten by the quirky time support in Rails:

>> Goal.create(:description => "Run a mile")
=> #<Goal id: 1, description: "Run a mile",
     created_at: "2008-09-09 19:32:57",
     updated_at: "2008-09-09 19:32:57">
>> Goal.find(:all, :conditions => ['created_at < ?',
     Time.now])
=> []

Huh? Checking the logs, we see that the two commands above correspond to the following queries:

INSERT INTO "goals" ("updated_at", "description",
  "created_at") VALUES('2008-09-09 19:32:57',
  'Run a mile', '2008-09-09 19:32:57')
SELECT * FROM "goals" WHERE created_at < '2008-09-09 15:33:17'

Rails stores created_at relative to Coordinated Universal Time, while Time.now is based on the system clock, running four hours behind. The solution? ActiveSupport’s TimeWithZone:

>> Goal.find(:all, :conditions => ['created_at < ?',
     Time.zone.now])
=> [#<Goal id: 1, description: "Run a mile",
     created_at: "2008-09-09 19:32:57",
     updated_at: "2008-09-09 19:32:57">]

Rule of thumb: always use TimeWithZone in your Rails projects. Date, Time and DateTime simply don’t play well with ActiveRecord. Instantiate it with Time.zone.now and Time.zone.local. To discard the time element, use beginning_of_day.

BONUS TIP

Since it’s a subclass of Time, interpolating a range of TimeWithZone objects fills in every second between the two times — not so useful if you need a date for every day in a month:

>> t = Time.zone.now
=> Tue, 09 Sep 2008 14:26:45 EDT -04:00
>> (t..(t + 1.month)).to_a.size
[9 minutes later]
=> 2592001

Fortunately, the desired behavior is just a monkeypatch away:

class ActiveSupport::TimeWithZone
  def succ
    self + 1.day
  end
end

>> (t..(t + 1.month)).to_a.size
=> 31

For more information about time zones in Rails, Geoff Buesing and Ryan Daigle have good, up-to-date posts.

Railsconf Europe Wrapup

Ben Scofield
Ben Scofield, Development Director, September 08, 2008 1

Finally, back in the office! I spent the last week in Berlin for Railsconf Europe, and though the travel can be painful at times, the conference more than made up for it. A few highlights:

  • Bratwurst on Rails - just like last year, the Berlin Ruby user group grilled thousands of brats for any attendees who happened to be in town Monday night. Great food and conversation.
  • The keynotes - from the Tuesday night panel (with DHH, Jeremy Kemper, and Koz), to DHH's and Jeremy's Wednesday keynotes, to the symposimi/ium on Thursday morning, the plenary sessions were uniformly excellent. DHH on legacy software as product of developer growth (not as a result of code rot), and Jeremy on performance stand out in particular.
  • The content - I went to a number of sessions that had outstanding content, foremost among them Sven Fuchs' on internationalization.
  • The attendees - like every conference, the best part is always the other attendees. I met a number of smart, passionate people in Berlin, and I'm excited both to keep up with them and see what they (and others) will do with the burst of inspiration that conferences like this are famous for.

Continue reading "Railsconf Europe Wrapup"

Unpublished Keyboard Shortcuts on Mac OS X

Tony Pitale
Tony Pitale, Web Developer, September 04, 2008 9

This is just the quickest of tips for everyone using Mac OS X.

Keyboard shortcuts are one of my favorite features on Mac OS X and something that I think few operating systems get right. Not only are there a staggering number of predefined shortcuts (some aren’t even advertised, but we’ll get to that in a moment) but also, for any menu item, a custom shortcut can be defined within System Preferences -> Keyboard & Mouse -> Keyboard Shortcuts (the right-most tab).

I find this to be immensely useful, especially in overriding the shortcut behavior of a variety of third party applications that have the same function. For example, Firefox, Textmate, and almost every other application that has tabbed windows provides a shortcut on the keyboard to switch between tabs (left and right). For Apple’s application, the shortcut is likely to be Command-Option-Arrow Key. By default many other applications use Command-Option-Brace (curly brace, or square bracket?). A quick pair of keyboard shortcuts overrides these behaviors to be standard to your shortcut preference.

If you can’t recall a keyboard shortcut, users of the latest version 10.5 (Leopard), can make instant use of the help menu which, like Spotlight for files, will search for text amongst the seemingly endless sea of menu options. This feature has saved me time, and again, in applications such as any of the Adobe suite of products.

Lastly, I’d like to touch on three very useful but, unadvertised, keyboard shortcuts.
Use Command-Option-Eject Key to set your computer into a blissful sleep state,
Command-Control-Eject Key to reset your Mac immediately, or
combine those two and get Command-Option-Control-Eject Key to shut it down until you need it again.

Overall, the mouse is unbelievably slow for pre-defined activities, in comparison to a keyboard shortcut. Thanks to Apple’s latest OS X, you don’t even have to remember them all.

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Recent Comments

Tony,

I understand and agree that the back-end shouldn’t output code (html code), and only content. The templates (aka views) should do the trick, but instead of having lot’s of if/else conditionals inside the view, you may just output the following content.

No information available

The template would loop in an array and put all the <li>’s inside the <ul>.
I don’t see anything wrong, nor...