Skip to main content

Mobomo webinars-now on demand! | learn more.

Hey RailsConf goers! You won't want to miss Jerry Cheung, co-author of (the just-released) MacRuby in Action book and Senior Engineer at Intridea present "Evented Ruby vs Node.js" Tuesday afternoon!

While Node.js is the hot new kid on the block, evented libraries like EventMachine for Ruby and Twisted for Python have existed for a long time. When does it make sense to use one over the other? What are the advantages and disadvantages to using node over ruby? In this talk, you will learn how to get the same power of concurrency enjoyed by Node.js while continuing to write in the language you know and love. Topics covered will include pubsub with redis or faye, building evented rack applications, and running evented applications alongside existing Rails apps.

Jerry will be in Salon K at 2:30 pm tomorrow.

Keep it weird, Austin Rails!

Categories
Author

One of the first things that anyone has to do in an application is assign instance variables in controllers for use in views, etc. This pattern, while dead simple, has a number of possible implementations that each have their aesthetic benefits and drawbacks. At RailsConf I had the opportunity to hash this out with none other than DHH and David Chelimsky, so I thought it might be a good opportunity to represent the different sides in a post.

The Repeat Yourself Pattern

This is where things start out many times, as they likely should. You're building out your controller for the first time, and you set the instance variable you need in each action:

class WidgetsController < ApplicationController   def show     @widget = Widget.find(params[:id])   end    def edit     @widget = Widget.find(params[:id])   end end 

This method, while very clear, has the obvious problem of being repetetive. What happens when your code isn't as simple as Model.find? You're going to run into trouble quickly. So let's try a few abstractions.

The Getter Pattern (The David Chelimsky Way)

So the simplest abstraction is to do what we normally do when we're repeating ourselves: define a method!

class WidgetsController < ApplicationController   def show     @widget = get_widget   end    def edit     @widget = get_widget   end    private    def get_widget     @widget = Widget.find(params[:id])   end end 

This pattern has some obvious advantages over repetition: it's pretty clear what's happening while giving us the opportunity to define our finder code in a single location. However, we're still duplicating a line at the top of each action which has a very slight smell.

Note: I'm attributing this to David Chelimsky because I heard him talking about it when I entered the conversation not necessarily because he prefers it to other methods.

The Helper Pattern (The Michael Bleigh Way)

So how can we better abstract this? Well, we're going to be accessing this instance variable primarily in the view, so why are we bothering to explicitly set it in the controller? This is the pattern that I have been using for the last couple of years:

class WidgetsController < ApplicationController   def show; end   def edit; end    private    def widget     @widget ||= Widget.find(params[:id])   end   helper_method :widget end 

This gives us a helper that we can call from our controller or our view that will lazily fetch the widget when we need it and not before. Personally I like this pattern quite a bit: I think it's relatively clear and works well in the various ways I've used it. However:

  1. It's a little weird that you don't see it anywhere above your actions. Might lead to confusion, especially in larger controllers.
  2. Via DHH: "You're calling something in the view that depends on params, which is bad."

The Filter Pattern (The DHH Way)

Finally we get to a pattern that I was aware of, but have avoided because I found it aesthetically displeasing:

class WidgetsController < ApplicationController   before_filter :set_widget, :only => [:show, :edit]    def show; end   def edit; end    private    def set_widget     @widget = Widget.find(params[:id])   end end 

So why didn't I like this? Because before_filter seems like it shouldn't be setting a variable, but modifying things (DHH agreed that before_filter should likely have a different name such as before_action). Also I didn't like the fact that the code is split up into two places.

However, DHH made a single argument that trumped everything else I had considered: "When it comes to setters, you don't care about the implementation, but you care very much that you know it's there." And it's true: the filter isn't doing anything complicated, it's just setting an instance variable and its function is very clear from its name. So when you think of it that way, using a before_filter actually makes all kinds of sense.

Why so many?

There are other ways of achieving this pattern, even gems such as decent_exposure that can automate much of it. So why isn't there a standard solution for this extremely common abstraction "baked in" to Rails? Because at a certain point abstracting behavior that is too simple is more complex than simply not abstracting it at all.

Why did so many patterns evolve for doing the same thing? I lay the blame mostly on both the name and syntax of the before_filter method. When something is called a "filter" you expect it to be manipulating, not setting. In addition (to me at least), the :only and :except syntax doesn't feel right to use in a common case. To me, these keywords ugly up the declaration and should only be used as a last resort.

So what might we do to the Rails syntax to make this pattern a little easier to understand? Here are a few different options I've been thinking about:

class WidgetsController < Application   # Option A   run :set_widget, before: [:show, :edit]    # Option B   before :show, :edit, set: :widget   before :index, run: :prepare    # Option C   before_action :set_widget, only: [:show, :edit]    private    def set_widget     # ...   end    def prepare     # ...   end end 

Option A adds a new run method to the controller syntax that acts as an "every filter". You could specify :before, :after, or :around options and either supply a list of actions or :all for all actions. Two downsides to this: The syntax is a little longer for the case where you want the method to run before all actions, and I don't have a good analog to :except for this option.

Option B takes a different approach and assumes that the arguments are declaring actions instead of methods to run. Here you declare one or more actions (or none for all actions) and you have two options: you can declare a :set key that will call a method called set_value where value is one or more symbols passed into the option, or you can call the :run option which will simply execute methods named as specified. This may seem like an arbitrary design choice, but Rails is full of choices that don't have specific need but rather guide developers down doing the best practice instead of doing something else. Downsides to this are again a lack of a good :except syntax and it reverses the current Rails assumption of declaring methods with before so would likely have some confusion associated if it were adopted.

Option C Is simply renaming before_filter to before_action. Obviously you would have to alias it (perhaps through the Rails 3.x versions), but eventually before_filter would be deprecated. The word filter simply implies a certain type of functionality rather than accurately describing it as simply a way to execute code before an action.

I'm not saying that any one of these options is better than what's already available, I just think that it's interesting that one of the most common patterns in Rails can still have so much difference of implementation around it. The fact that no one "best practice" has become dominant to me implies that there's some room to explore API changes to point people down a best path. What are your thoughts on the controller setter pattern?

Categories
Author

I had the opportunity to speak at RailsConf 2011 about OmniAuth, outlining some of the reasoning behind it as well as some current and upcoming features of Intridea's own "authenticate with anything" middleware. While the session wasn't video recorded, a little trick I've picked up is to run a screencasting program in the background while I present to generate a "poor man's Confreaks" version of the talk. Well, that's exactly what I've done for OmniAuth: From the Ground Up!

I'm also embedding the slides below for a "click-through" tour of the talk, but there were a few livecoding sections that are only represented in the video form.

I had a great time at my fourth RailsConf and I hope those who had a chance to attend enjoyed my talk. I also want to say thanks to all of OmniAuth's contributors, they've given me the ability to really ratchet up the awesome on the library and I couldn't have done it without them.

Categories
Author

And I’m back from RailsConf 2009! I had a great time and enjoyed meeting and speaking with a number of Rubyists who had previously only been @-names on Twitter. I also had a great time giving my talk, Twitter on Rails. Thanks to everyone who had a chance to make the session and especially everyone who made it to the Twitter BoF on Wednesday night. I got some great feedback and interesting ideas for future work.

I recorded my entire session as a screencast (a last-minute idea that I’m very glad to have had; with 30+ minutes of livecode the slides are only so useful), and I have also posted the slides (also embedded below) and the completed livecode. I’m happy to answer any questions about Twitter app development, TwitterAuth, or anything else since I didn’t have a chance to take questions during the session.

Thanks to everyone at RailsConf for making it a great conference, and hopefully I’ll see many of you at another Ruby event soon!

Categories
Author

I saw the article ’Google’s First Real Threat: Twitter’ pop up in my RSS readers today from a couple sources and didn’t click through and read; I had already learned the power of Twitter Search on multiple occasions (see my previous discussion here). But then something funny happened: I got an e-mail from O’Reilly saying that all three of my RailsConf proposals were accepted. Then I heard from a colleague here at Intridea that his talk was accepted.

This seemed, frankly, too good to be true. So I hit up Twitter Search for RailsConf, and sure enough, everyone seemed to be elated about getting their proposal(s) accepted. This confirmed my suspicion, and I contacted O’Reilly immediately. In fact, I contacted O’Reilly and they weren’t even aware of the problem yet. I may have been the one who alerted them to the issue in the first place. Confirmation came a few minutes later via the RailsConf Twitter account. While I’m a bit disheartened that I’m not necessarily speaking at RailsConf, it was an object lesson in just how powerful Twitter search has become.

Is Twitter a replacement for Google? No. But Twitter provides an instantaneous connection to what is happening to people right now, and in some (many) circumstances it can give you answers that Google never would, even after they re-index the web. This also to me serves as a lesson in how to truly compete with Google. Don’t try to “out-Google Google” like Cuil did. Instead find a way to provide a search that Google can’t touch, that can’t be created simply by crawling the web endlessly looking for new content.

Pay attention to where Twitter search goes in the coming months and years, because it’s no joke: real time search is a big deal.

Categories
Author

Last week, along with a few of my Intridea colleagues, I had the opportunity to experience two entirely different Ruby-related conferences. The first was RailsConf Europe, an international Ruby on Rails conference in Berlin Germany. After giving our RCE talks, Pradeep and I got on planes and flew to Austin, TX for the Lone Star Ruby Conference, where Intridea presented a full-day training session. Both conferences had their own unique feel, and it was interesting to compare the community eco-systems inherent in each.

At RailsConf Europe, Intridea had two talks accepted “ Michael and Pradeep gave a tutorial on Rails Internals, and Michael and I presented a talk and coding session on Mid-End Rails Development. For our talk, the crowd was attentive, but very quiet. Generally when speaking in a packed room, there will be a quiet rumble as attendees comment on the ongoing presentation. The entire event had more of a formal feeling to it. This could perhaps be due to the language barrier brought on by attendees from all over Europe, the US, and the world coming together “ but it was a departure from my other presentation experiences.

Likely related to the varied backgrounds, the RailsConf Europe crowd did not seem to be very cohesive “ there appeared to be fewer hallway hack sessions and in-depth discussion that you often find at similar events. The venue also contributed to lack of cohesion. The hotel did not offer breakfast, so attendees didn't have that early morning mingling that usually occurs. Being held in a hotel in downtown Berlin, there were also many potential distractions. A quick nap in the room, a run out to the street corner bratwurst vendor, a wide variety of local pubs – there were many opportunities to be drawn away from the conference.

Because of the variety of backgrounds, along with the number of distractions, the community feel seemed to be a bit lacking.

The Lone Star Ruby Conference was an entirely different experience, I think a large part due to the venue. At LSRC, the venue was not attached to a hotel, all meals were provided, events were scheduled from morning until night, and there was not a great deal to do within walking distance of the venue “ not to mention it was almost too hot to go outside! All of these contributed to a more cohesive group that grew even stronger as the event went on.

Adam, Pradeep, and I presented our full-day training “ Rails Refactoring: Triage, Prevention, and Performance “ to a great group of developers. Having just come from RCE, where people were from all over, it was different to have a class full of people mostly from the same geographic area (Texas). Perhaps the nature of a training vs a session, but there was much more communication going on between attendees and instructors. The vibe was definitely more laid back.

That vibe extended to the rest of the event “ there were lots of hallway hacking sessions, people helping other developers with code problems, and many in-depth technical discussions outside of the actual talks.

Overall, both events offered some great technical talks and the opportunity to meet some great Ruby/Rails minds “ but for a community feel, Lone Star wins hands-down.

Categories
Author

Intridea will be speaking at two Ruby/Rails conferences this week. If you're attending either conference, make sure you come by and say hi!

RailsConf Europe

Pradeep Elankumaran and Michael Bleigh will be presenting their RailsConf Europe tutorial: The Renegade's Guide to Hacking Rails Internals, on September 2nd.

This far-reaching tutorial will give Intermediate and Advanced Rails developers a crash course in hacking and extending Rails internals. The topics covered will include: Ruby Meta-programming Techniques; Rails Abstractions, Idioms & Mixins; Rails Structure & Initialization; A Tour of the Rails Class Loader; Plugin Locators and Loaders; Codebase Modularization using Rails Plugins; Extending ActionView Form Builders; Site-wide Settings; and much more¦

Michael Bleigh and Chris Selmer will be presenting a RailsConf Europe talk: Hacking the Mid-End: Unobtrusive Scripting and Advanced UI Techniques in Rails, on September 3rd. Here's the talk overview:

As web application development advances beyond the static page, a whole new field of development is emerging. In the Javascript behavior layer and markup abstracting helpers lie the 'Mid-End': advanced user interface problems that don't fit traditional ˜back-end' and ˜front-end' models. Explore this new field with case studies and real code such as usage of Lowpro Javascript behaviors to keep the behavior separate from the markup. Learn how to give back-end developers the tools to create simple, repeatable, quality markup through block-accepting helpers. Discuss the methods that allow for rapid development of complex interactions in new and exciting ways and see real examples. Finally, look into the future of the Mid-End and what lies ahead for user interface development.

The Lone Star Ruby Conference

Adam Bair, Pradeep Elankumaran, and Chris Selmer will be leading a training on the first day of the LSRC. The training is titled: Rails Refactoring: Triage, Prevention, and Performance. And the overview:

Maybe you inherited a mess of a Rails project “ or perhaps your own codebase is poorly-tested, not very DRY, or just generally confusing. Worse yet, maybe your Rails site has slowed down to a crawl or even stopped working entirely. Whatever the reason, it's time to consider refactoring these rough spots and boosting your site's performance.

In the first half of the day we'll go through real-life examples of (shameful) code we've written and refactored, give tips on how and when to start, and show you how to avoid the need for a future refactor. In the second half, we'll introduce common Rails performance pitfalls, how to diagnose them, and how to solve them. We'll also talk about other ways to speed up your app.

Categories
Author

We had a great time this year at RailsConf 2008 in Portland, Oregon. If you were there, we hope you had a chance to try out Yoga on Rails and hopefully you had a chance to stop by our booth to meet some of us. At our booth we were showing off some of our products, such as Acts As Community, CrowdSound, SocialSpring, Scalr, and MediaPlug. We even handed out a bunch of awesome Intridea t-shirts, which no swag-bag was complete without.

Day Before RailsConf 2008   RailsConf 2008
photo.jpg   RailsConf 2008

The Intridea team participated in the Community Code Drive on Thursday and worked on improving some of our open source plugins, such as Acts As Taggable On, SMS Fu, and Fu Fu. The rest of the conference was filled with informative talks from prominent members of the Rails and Ruby community. Even our very own Chris Selmer gave a talk on ˜How to Build an Rails App in 48 Hours' with Josh Owens of the Web 2.0 Show.

RailsConf 2008    RailsConf 2008
RailsConf 2008   RailsConf 2008
RailsConf 2008

Thanks goes out to everybody who stopped by our booth and everyone we met throughout the event. RailsConf 2008 was a blast and we can't wait until next year when we get to do it all over again.

Categories
Author

Intridea is going to be actively involved throughout Railsconf 2008. We're sending our entire team, so you're sure to see us around if you're attending.

Official Sponsors - Come Visit Us!

Intridea is an official Silver Sponsor of Railsconf this year. Come check our booth during lunch and the designated expo times (10:00-10:45am, 12:30-2:00pm, 3:30-4:30pm on Friday and Saturday). We'll be giving away t-shirts, will have product demos, and will be announcing the release of two products. Also, be sure to sign up on the Acts As Community.

Intridea Presents Yoga on Rails

Intridea will kick off each morning of RailsConf 2008 by sponsoring a yoga session aimed at refreshing developers' bodies and minds before a day of sessions. Kate Sanderson of Portland's Yoga Bhoga will be leading the session. Yoga on Rails will be an easy beginner's yoga session “ so don't be shy about coming out even if this will be your first yoga experience. We hope to see you there!

Community Code Drive

Intridea will be actively involved in the Community Code Drive on the first day of RailsConf 2008. The Community Code Drive is a chance for Rails developers to give back to the Rails community by teaching others and contributing code to open source projects. The entire Intridea team will be on hand to help support this effort.

Chris and Josh Speaking on Sunday at 1:50pm

Want to build a Rails application quickly? Competitors in the Rails Rumble had 48 hours to design, develop, and deploy a complete application. Chris Selmer and Josh Owens will take attendees through the development processes of the winning four-man Tasty Planner team, and compare it with those of the one-man Your Pet Records team. The discussion will include techniques, short-cuts, helpers, and Rails plug-ins that helped speed development. Join Chris and Josh in the Portland Ballroom 256 at 1:50pm on Sunday.

Categories
Author

RailsConf 2008Intridea will be represented at Railsconf 2008 by Josh Owens and Chris Selmer. They will be speaking about their experience rapidly building web applications during the first Rails Rumble, a 48-hour contest for developing a Rails site from concept to completion.

Railsconf 2008 will be held in Portland, OR on May 29th - June 1st. Stay tuned for scheduling details.

Categories
Author
1
Subscribe to Railsconf