Tuesday, April 24, 2012

10 Things you didn't know Rails could do - James Edward Gray II

10 42 Things you didn't know Rails could do
4.2 times more awesome than originally planned.

There's going to be a Rails Core panel at 5:30 today, which wasn't on the schedule initially.  That will be interesting, especially since it happens to overlap with the previous scheduled 'dinner' block.  Given the two options...I'll take the Rails Core option.

This is all for Rails 3.2 or higher


Interfaces - ways to give Rails a hug
1. Run a complete Rails app from a Single File
2. TODO, FIXME, OPTIMIZE comments -- there's a rake task to list all of those out: rake notes:todo
3. Sandbox Your Console: rails c --sandbox
4. Run Helper Methods in the Console: helper.number_to_currency(100)
5. Use non-WEBricker server in development
6. Allow you to Tap Into its Configuration: plugins can be configured the same way as the Rails app

Databases
7. Understand Shorthand Migrations on command line: can leave off types, add limits
8. Add Indexes to Migrations on command line
9. Add Associations to a Migration
10. Show you the Status of the Database: rake db:migrate:status
11. Import Your CSV Data
12. Store CSV in your Database:
13. "Pluck" Fields out of your Database - User.pluck(:email), User.uniq.pluck(:email)
14. Count Records in Groups: Event.group(:trigger).count
15. Allow you to override associations:
16. Instantiate Records Without a Database (Somewhere between cool and evil)
17. Use Limitless Strings in PostgreSQL:
18. Use Full Text Search in PostgreSQL: The reason we should all be use Postgres now instead of MySQL. 
19. Use Different Database for each user: in the case where Users will only ever be manipulating their own data.  Could potentially scale very nicely since you don't have just one database that keeps growing, and more than one database 'pipeline' for data to come through.

Ruby Extensions - Updating Ruby's fashion sense
20. Write File Atomically: File.atomic_write()
21. Merge Nested Hashes: .deep_merge FTW
22. Remove Specific Keys From a Hash: params.except(:controller)
23. Add Defaults to Hash: defaults.reverse_merge(params)
24. Answer Questions About Strings: `inquiry` magic

Views - it's all about how we look
25. Hide Comments from your Users (comments in ERB)
26. Understand a Shorter ERB syntax: ERB starts with just a single % at the beginning of the line
27. Use Blocks to Avoid Assignments in Views: assignments in views just looks bad
28. Generate Multiple Tags at Once:
29. Render Any Object: Rails used to ask the Class what partial to render, not it asks the actual Object. 
30. Group Menu Entries: select_tag(:grouped_menu...)
31. Build Forms the way you like them: Build forms the way you want them
32. Inspire Theme Songs about your work: check out @tenderlove 's themesong
33. Route Exceptions to any Rack application
34. Route to Sinatra (you can mount any Rack application in the routes file now)
35. Stream CSV to Users: Make sure to use find_each instead of all, or you've defeated the purpose
36. Do Some Work in the Background: require "thread"
37. Publish a Status Site Using Rails: page cache EVERYTHING, and then just rsync it around.


The full slide deck will be available at: http://speakerdeck.com/u/jeg2

Presenters & Decorators - Mike Moore

Disclaimer: Rails is a big tent.  We have lots of divergent ideas living inside our community.  This presentation covers one particular approach to solving certain problems.

Presenters should ease PAIN: They should be used to solve a problem.  They shouldn't be used early, but to solve a pain-point during development.  As an example he discussed the situation at a previous job where the Views were over architected with Presenters that just led to confusion for new developers.

Won't somebody please think of the new developers?!?  We're not talking about newbie developers, but developers who come onto the project later.  We need to be very cognizant of the trade-offs of advanced techniques versus the benefits.  The benefits need to far outweight the potential costs.

Typical Summary Example: The View code is iterating over model collections to calculate and determine DOM elements from the Models.  The typical solution is to pull the details of those requests into functions in the appropriate model.  But they don't really belong there since it's not domain logic, it's just view related.

A better solution is a Presenter, a completely separate class that is aware of the model.  The Presenter, as a separate entity, can be Unit tested, and it removes lots of unnecessary logic from the views.  All of the behavior should be at the Model or Presenter layer, with as little of it as possible in the View layer.

Draper and ActiveDecorator are two examples of gems for building Presenters.

One great use for them is as an intermediary for the different formats your application should respond to.  For example, if your application needs to serve JSON as an API response, testing that can be rough.  You have to do an integration test: make a request to the controller/action, perform the associated logic, render the view, parse the json, then finally make your asserts.  Right?  Not necessarily. 

Using a Presenter (in this case as a Serializer) to take care of any transformations to the data suddenly makes it Unit testable.

Mediator Design Pattern: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly and lets you vary their interaction.

Hearkens back to Martin Fowler's Presentation Model, and suggests that Presenters should be close to that definition. 

Make the view/template as dumb as possible.  If you are a designer, or work with a designer, the more behavior the intelligence is present in the view, the greater the risk of something being changed in unexpected ways. 

Mike was pretty nervous throughout the talk, but I think he did great.  He had a lot of good, concrete examples to illustrate his points, it flowed well, and I am looking forward to learning more.

Ruby Heroes 2012

Greg Pollack talks about the history of the Ruby Hero award, how it started, how they are selected.

1. Konstantine Haase: Contributed a lot to Rack, Rubinius, and recently become the maintainer of Sinatra, co-authored a Sinatra book.

2. Tony Arcieri: Revactor, Cool.io, Celluloid::IO, New IO for Ruby, DCell, Reia, Unlimited Novelty blog

3. Yoko Harado: JRuby, Nokogiri, yokolet's notelets

4. Sven Fuchs: Rails I18n and I18n Rails Guide, Cofounder of Travis CI project

5. Frederick Cheung: Ruby on Rails: Talk mailing list

6. Eric Hodel: maintaining RDoc and RubyGems, Ruby Documentation Project, Mendicant University

Congratulations to them, with a big thank you for their contributions to our community!

Bite-sized RailsConf, Aaron Patterson

As should have been obvious to me after my Day 0 post, there's way too much going on at RailsConf for me to reasonably try to do one post that covers everything I wanted to say about the day.  Yesterday's post just underscored this issue even more.

So I'm going to make smaller, more bite-sized posts throughout the day and see how that goes.

One item of note prior to the first keynote of the morning was the announcement/reminder for the live Ruby Rogues podcast tomorrow.  They'll be taking previously submitted questions to discuss on the panel, but they need to be emailed in today.  So if you have a question for any of them, send it in to panel[at]rubyrogues.com

On to the main event: Aaron Patterson

After some amusing anecdotes about trying to fill out his annual review, we move on to the actual topic: Failure. 

He starts off by discussing Yehuda Katz's recent Kickstarter project, called Tokaido.  Tokaido is a binary distribution/installer for Ruby that also comes packaged with all of the gems necessary to run Rails.

Why do we have ActionMailer? To send email, obviously.  But often our controller code directly calls the email triggers, ignoring any Concurrency concerns this could cause in our applications. So why do we have ActionMailer, but we don't have a Queue?  Turns out we do have a Queue, called ActiveQueue, but there are a few issues.  No one uses it, because it has no documentation, no examples, and technically 0 lines of implementation code. What he would like to see in Rails 4 is for it to be the driving force to create an API for the various Queuing implementations available.  

Fear of Features
"Older developers have a lower tolerance for Technical Debt."  This stems in large part from becoming more familiar with the maintenance costs associated with new features.

"Our Browsers are getting more powerful, and we need to take advantage of them."

For a while now most (or all) of our computations have been run on the server.  Things are changing now, with JavaScript on the client-side becoming more and more important.  And we need to be prepared for that shift.

Monday, April 23, 2012

Rails Conf 2012: Day 1

Today has been pretty amazing.  I've been trying for the last 2 or 3 years to attend RailsConf and it doesn't disappoint.  From a developer perspective, the most valuable thing I am taking away from this entire experience is a renewed excitement and drive in relation to what I do every day.  Getting to spend several days with other developers, discussing topics that matter to us, is just a lot of fun. 

The day started off with the keynote address by David Heinemeier Hansson.  His entire focus was on 'Progress', and that while we all usually say 'Yes, we like progress' in the abstract it often can become more difficult to agree so readily when we get down to specifics.  He brought up several examples that have impacted the Rails community over the years: REST, bundler, the asset pipeline.  These were all things that, at the time of their introduction, received a non-trivial amount of backlash from portions of the community.  But now they're all commonly accepted components of our development environment.  This all basically led up to the declaration that "Rails 4 is coming...".  Once again, there will be changes to our environment that at first we might be tempted to rage against. 

For fear of this post running on forever, I'm only going to cover the first session I attended in detail.

Learning something new, especially when it's really adjustments to something you thought you had already conquered, can be frustrating.  But I think the Rails Core team has a pretty good track record for making the right decisions, so I'm excited to eventually see what they've come up with.

For the first session of the day, I attended Sarah Mei's presentation on Backbone.js.  The first half of the talk was basically about the evolution of a developer's use of Javascript.

Phase 1: No JavaScript.  You just use the built-in Rails functionality to create any JavaScript that you need.

Phase 2: jQuery Explosion!  Your views are practically littered (mostly at the top) with jQuery.  This eventually ends up looking like spaghetti code.  Not to mention it's difficult (if not impossible) to test.

Phase 3: JavaScript page objects.  It's a distinct improvement over Phase 2.  At the very least you can test it without too much difficulty.  But as the 'product people' begin to request more and more interaction this ultimately breaks down.  There's more and more client-side rendering, and an explosion of events that this eventually collapses under.

Phase 4: And we finally arrive at JavaScript Frameworks, of which Backbone is one of the more recent examples.  On the plus side, Backbone (generally) maps fairly well to Rails.  The conventions for how it is employed aren't strongly established yet, so there is a wide diversity between projects the both employ it. 

Ultimately she suggested there were two primary takeaways from her perspective.  1) Backbone works fine on a greenfield application, but alternatives should be considered first.  There's a lot of boilerplate work that you would still have to do.  2) Backbone's sweet spot is when you're converting an existing application to have more client-side functionality. 


Talking with Andrew Cantino and Zach Brock later on, Andrew brought up another framework called Ember.js.  Zach said that he had worked with a project that had initially been built with Backbone, including a lot of customization that had to be built on for it, when they decided to go with Ember instead the need for almost all of that customization went away.  So I'm definitely going to have to put some time into researching that, and see what I come up with.

The other presentation that I really enjoyed was given by the aforementioned Andrew Cantino and Ryan Stout on Machine Learning and Rails.  I took an Intro to Machine Learning course back in college, but that was a while ago.  It was nice getting a refresher on some of the basic algorithms (Decision Trees, Naive Bayes and Neural Nets) and to see how those techniques can be applied, even in a Rails environment.

The day turns into something of a blur after that.  I got to catch up with several old friend from previous jobs, which led to one of my big revelations.  It's rare to have this kind of opportunity and environment with so many talented and intelligent people.  So don't be so focused on the actual schedule of the Conference.  Definitely find sessions that interest you and get involved in them, but it's also incredibly worthwhile to take some time and just talk with our peers.  It's incredible the things you can pick up in the process.

Addendum

In regards to Raimonds' Ignite presentation yesterday, this is one of the reasons I was laughing so hard:


Sunday, April 22, 2012

RailsConf 2012: Day 0

I admit it.  I've been programming professionally for several years now.  I've specifically been programming in Ruby on Rails for nearly 5 years.  And yet I've never been able to attend any programming-related conferences.  Until this week.

And of course I don't start with a more local conference or anything.  I jump straight into RailsConf 2012, which is pretty much the alien mothership for developers of my ilk.  But I'm getting ahead of myself.

First off, I've never been to Austin before, but so far I quite like the city.  Granted I've seen very VERY little of it so far, but what I have seen has consisted of views like this:


I find it quite pleasant.

Back to the matter at hand.  Event though RailsConf doesn't officially start until tomorrow, my coworker saw there was a pre-conference Ignite session tonight.  I wasn't really sure what to expect, but I was really impressed with how the evening went.  But first, the obligatory picture of the gathering assembly of programmers:



For those unfamiliar with Ignite sessions, the slides the speakers use for their talks transition automatically. I'm guessing every 20-30 seconds or so, but I wasn't actually timing it.  So the speaker has to stay pretty on point and direct.  And once the slides for your talk are over, you're done.  Pretty interesting concept, yet I couldn't help but wonder how much programming-related content you can fit into a format like that.

As it turns out, quite a bit.  I ended up taking notes on my iPhone during the session, so these notes are by no means complete or copious, just the salient points that really stuck out to me:

Noel Rappin (@noelrap): As developers we can and should take better advantage of our programming environments lower the Extrinsic Load our brains have to work under.  Basically if there's something that you generally just have to remember (i.e. the 1st tab of my console is the rails server, the 2nd is the rails console, 3rd is solr, etc), that's energy your brain has to expend to keep that information there.  Either that, or it doesn't and you have to go poking around for what you were looking for.  Instead, we should tailor our environments as much as we can to be as informative as possible.  Make the computer tell you as much as it can, so that you can focus on solving the actual problems you're working on.

Adam Keys (@therealadam): Be positive.  Say 'Yes'.  The way Adam phrased is "Yes, and...", but the core of it is to be a happier, confident person and say 'Yes'.  He introduced the idea as coming from taking an Improv class, where you are presented with a situation and reply with "Yes, and...".  This response simultaneously accepts the information you were given, validates the person to whom you are speaking, and then allows you to build or expound on the topic with additional information.  Even if you disagree with the person, honestly using this response technique instead would lead to finding common ground upon which to build, while "No, because" or other similar variants just start to feel like drawing lines in the sand.   Stay positive.  Be confident.  Say Yes.

Raimonds Simanovskis (@rsim): Raimonds built a tool for analyzing git commits, and presenting some interesting and amusing statistical data gathered by the tool on the Rails repo on github.  I wish I had take pictures of some of the slides, but I was too busy laughing.  It was awesome.  Hopefully I'll be able to find some photos elsewhere.

Andy Maleh (@AndyMaleh): While they have many similar end goals, Software Engineering and Software Craftsmanship differ greatly in their approaches to accomplishing those goals.  This is another talk where I wish I had the slides or some pictures of certain key slides, as I couldn't type on my phone fast enough to capture much.  But very interesting stuff.

Prakash Murthy (@_prakash): Honestly my brain almost (ALMOST) fell asleep for this one.  Prakash talked about running a marathon.  I admit that I probably should start some kind of a fitness regimen, but running is the absolute last thing I would choose to do.  However, one of his slides had this quote on it (or something close enough to it):

"There will be days when you think you can't run a marathon.  There will be a lifetime of knowing that you did."

I almost ignored the fact that he was talking about running a marathon at this point, because that easily applies to almost everything in life.  There are a lot of things that I have thought I couldn't/can't do, that I've been afraid of trying, and I've let that hold me back.  I am working on making that less the case for me, and I'm pretty sure I'll be happier that way.

Jesse Wolgamott (@jwo) - ActiveRecord is like Velveeta.  There are certain unspoken rules about how they should be used.  Velveeta should never be consumed in an un-melted state. ActiveRecord should not be used in such a way where your ENTIRE application knows about it.

Ron Evans (@deadprogram) - Spoke about KidsRuby.com and KidsCodeCamp.com.  This was really timely for me as I've begun thinking it is time to introduce my daughter to programming, but I wasn't really sure where to start with that.  Now at least I have a platform to start with (Yay, Ruby!), and hopefully I'll be able to get some ideas as far as a curriculum goes.

Brian Liles (@bryanl) - I would really like to meet Brian.  I'm a pretty horrible conversationalist, but I think Brian would take care of most of the conversational heavy lifting for me.  His talk was really entertaining.  He started out his talk dealing with at some point losing his 'spark' and realizing that he had allowed it to be stolen making OPR (Other People Rich) by working on OPC (Other People's Code).  Funny and insightful talk about taking a more active role in your career to avoid losing your spark.

Steve Klabnik (@steveklabnik) - Steve's talk mostly boiled down to 'we need more female programmers'.  And he's right.  A couple of years ago I was lucky enough to get to pair program with Sarah Mei (@sarahmei) for a couple of months, and it was great.  I learned a lot while working with her.  Steve did the topic much better justice than I think I am...but yes, I agree with him. 

Richard Schneeman (@schneems) - The work is full of Wicked Problems.  Problems that, due to a combination of a variety of factors, are really hard to solve.  The thing is, that's pretty much what we as programmers do for a living: problem solving.  And we have some pretty sophisticated toolsets to help us do just that.


I also wanted to give props to the other speakers: Chris Holland (@slamgauge) and Kenichi Murahashi (@sanemat).  The specific topic they spoke about didn't hold much interest for me personally, but I respect the fact that they got up there to speak.  Doing that is something I fantasize about but have yet to even attempt.  Well done, to both of you.

So that was Day 0 of my first RailsConf.  I'm really looking forward to tomorrow.  I'm planning on attending these presentations:
  1. "Using Backbone.js with Rails", by the previously mentioned Sarah Mei
  2. "Practical Machine Learning and Rails", by Andrew Cantino & Ryan Stout
  3. "Rails Engine Patterns", by Andy Maleh
    (or maybe "Mobile Rage", by Lori Olson, not entirely sure yet)
  4. Really not sure here, but am leaning towards "Use the Source, Luke: High fidelity data with event sourcing", by Keith Gaddis
  5. "Securing your Site", by Thomas Pomfret
Hopefully I'll get to reconnect with some old friends tomorrow, while also being educated on some really interesting topics.