May 22, 2012

Redis on Rails - RedDotRubyConf 2012

This past weekend I had the pleasure of delivering a wide-ranging talk in Singapore about using Redis as a complement to your main relational database. It was essentially a much enhanced redo of my RailsConf 2012 talk of the same name. The challenge of covering this topic effectively is that Redis is quite popular so at a single-track conference such as RedDotRubyConf, I was sure the audience would include many people that are already using Redis in production. For that reason, I cut down on the introductory material and dove into aspects of Redis, related libraries and programming techniques that I hoped would be of use to as much of the audience as possible. Apparently that strategy worked, because I was happy to get a lot of positive feedback from almost everyone at the conference.

The slides below are not particularly useful without the associated talk material, especially since I spent most of the time inside of the sample app doing live coding and code walkthroughs with explanations. However, the slides do provide you with pointers to a number of different libraries that are of definite use to you if you're using Redis to complement your relational database. I have every intention of expounding on this topic here on this blog since the technique has been very useful to me in development of DueProps. With that said, here are my slides. Enjoy.

May 14, 2012

The Rails Dilemma

This is a guest post by Rusty Zarse CTO of Search Discovery, spurred by a series of email conversations that we had regarding the difficulty of finding experienced Rails talent. Rusty leads the Atlanta iOS Developers meetup and is one of the better known technologists in our city.

Where'd Every RoR Go?

“Rails is hot,” ain’t no headline today. Five years ago, Ruby on Rails was an underdog, the somebody to watch, the next big thing. It's not news that the Rails community continues evolving and growing while its members do a good job protecting the integrity and quality of the platform. Rails 4 is visible on the horizon and looks better than ever. The growth of the Rails community worldwide appears to be relentless, but there's a dark underside to that growth that does not get enough attention: In terms of sheer numbers, there just doesn't appear to be enough Rails developers to properly serve the needs of the market.

Technical organizations (particularly at the CTO and Director level that I inhabit) are now faced with a stark decision of choosing the Rails platform on its virtues or reluctantly giving the nod to another technology that carries less staffing risk.

On the face of it, people might think that this situation is good for Rails developers because it means that they continue commanding astronomical billable rates, higher quality coding practices and standards for their professional ecosystem and the aura of elite superstars. But Rails remains a community-driven platform and so it's up to the community itself to identify the needs of the market and identify what can be done for our overall growth to continue to flourish.

Attracting Rails Talent

Our core product at SearchDiscovery does things that truly push the envelope. There's a lot of work remaining to be done, but we feel that it already does what nothing else in the world can do. We owe a lot to the exceptional genius of our Hashrocket team and the wizardry of the lead developer they provided to us. During my career I've led many projects and never seen a product reach this level of complexity without falling apart due to compromises and shortcuts made along the way. I attribute our success in no small part to the principles and practices that Rails embodies. It is the motivation that I had for choosing Ruby on Rails as our web development platform to begin with and continues to provide a level of quality and productivity that I believe is unmatchable.

Our dilemma is that in over 3 months of arduous searching, we have not been able to find even a single Rubyist in Atlanta interested in joining our team. I'm not talking about having to turn down interested applicants; I'm talking about not having any interested applicants to begin with.

Now we have new products in the pipeline and our CEO is pushing to choose an alternative platform. Some of us know PHP and I personally know .NET. My experience with PHP is that its a great hacky tool to tweak wordpress and everything else just becomes spaghetti soup. Microsoft .NET is the platform of choice for developers who like to write the same oil tanker full of plumbing code over and over again just to show how great they are at doing things "better" than everyone else. Here in Atlanta, a common alternative to Rails is .NET, given the large Microsoft contingent we have in town. In my opinion, .NET doesn't deliver quickly enough and PHP inevitably becomes a maintenance nightmare.

Why on earth would we consider switching off Rails? The painful truth is that we need a team of professionals more than we need the best technology. We don't even need to hire an entire army of engineers immediately. We're specifically looking to fill at least one senior position so that this individual can cultivate Ruby on Rails talent within our walls. Our long-term goal is to become that company everyone in Atlanta wants to work at. We admire companies who posses an organic energy and self-managed excellence within their development team and we're willing to invest the capital needed to get there. I feel  that we have the professional atmosphere and culture that would be attractive to Rails professionals, but where are they?

Rails in Atlanta

Obviously, timing is everything. I've tapped the local user group community and will continue to do so, but so far have not seen much interest in the position. When I do connect with a local Atlanta Rails expert, often the professional courteousness is not reciprocated. I don't want to complain about specific people, but c'mon it just isn't necessary to be rude when someone asks if you are available. Clearly I'm trying to recruit someone, but I'm a CTO not a recruiter. Is politeness too much to ask for? Most technical people in Atlanta are nice and as helpful but there are a handful of dudes with serious reputations that apparently forget that we're all in this together. Their dismissive attitudes are toxic to non programmers who just want to run a business.

Worse, these bad attitudes get back to my bosses. I can argue all day about the gazillion benefits that using Rails brings to the table, but I can’t argue against the facts of our recruiting problem!  

A Plea to the Community

I wrote this post at Obie's request when I asked him for advice. I'd failed to consider that this is more than just my problem, more than just a hiring problem: It's a Rails community concern. The Rails platform is not bleeding edge anymore. It's proven, mature, reliable, amazing and a proper choice for just about any web development problem domain you might encounter. I don't know what the solution is, but I do know that we need to ensure that it continues to be the platform of choice for innovation.  We’re committed enough in the concept to put our money where our mouth is.

What do you think? Is this actually a community problem? Let's get a greater dialogue going.

March 20, 2012

Andrei Apologized

Since I got a sincere-sounding apology from Andrei, I have un-published my last blog post and hope this will be the end of the matter. Thanks to everyone that had my back about the way I handled it, and also those that gave me advice and support about it.

 

March 14, 2012

Seeking iOS Expert

I'm so dumb sometimes. I was just in the kitchen making myself a sandwich and it hit me why I have such a mental block about hiring anyone to do remote iOS development for me: I don't know anything about it! But how to get past that? I don't have time to teach myself, it would take too long and my first few projects would be crap. Too risky.

Perhaps I should follow my own advice on the subject? D'oh! What I really need is an expert to come pair with me on building the iOS version of DueProps at my location.

The way I see it that buys me getting:

  • past the initial learning curve of iOS
  • an early version of the actual software that I'm happy with
  • more than a prayer of being able to maintain the thing myself afterwards

Now I just need to find my expert. Please email me if you are an expert iOS developer and willing to take a short-term contract (approx 1 week) onsite with me in Atlanta at DueProps HQ. Travel expenses covered and hourly rate commensurate with your experience level.

Alternatively, I'm also willing to barter time (and still pay for your travel expenses to my location). Meaning, if you are an iOS developer that needs help getting a Rails webapp up and running, I can trade my time for yours in exchange for a full or partial reduction in your rate.

March 07, 2012

How Insignificant Is Internet Explorer These Days?

Maybe Super Tuesday got me in a mood to play with numbers tonight. I was poking around analytics stats for DueProps and noticed that my users are not fans of Internet Explorer in any significant numbers and among them, most use modern versions of IE (8,9) by a huge margin. These visit percentages are across many tens of thousands of visits since we opened our beta last year. 

Iestats

Admittedly these numbers reflect an early adopter crowd, but still, they're much smaller than I would have guessed. Does this information mean we don't need to invest much effort in maintaining compatibility with IE? Does IE9 have enough penetration to be the only one to worry about?

March 02, 2012

Forced to disable Firefox Inspect Element due to muscle memory

It's amazing how much we take for granted certain little tasks that we do so often they become muscle memory. The latest powerful example of that fact for me has been the annoyance provoked by the addition of a native Inspect Element option to the Firefox 10 context menu, exactly where Firebug's Inspect Element used to reside.

Screen shot 2012-03-02 at 11.02.51 AM.png

If you had asked me last week how many times I hit inspect element in a given day, I might have guessed a dozen or so... Ha! Turns out I do it more like hundreds of times a day! And you build serious muscle memory when you do something so often. Yesterday, despite repeatedly making mental notes of the fact that I needed to move my pointer 1 more menu item down the list to the "Inspect Element with Firebug" item, I would just forget. Over, and over, and over again. Sometimes 30 seconds apart. To the point of crying out in frustration.

Of course, you can turn off that menu item using Firefox's about:config screen, which I suspected was possible but had resisted researching based on, well, based on I don't know what, some perveted sense of principle that I should be able to learn something so simple! Still, major props to Jared Habeck:

So the remarkable part of this experience was realizing just how ingrained that particular operation was that no matter how hard I tried, occasionally even sometimes when I was purposely thinking about "ok self, remember to click Inspect Element with Firefox" MY FINGER STILL DID THE WRONG THING!

Crazy. As a sidenote, I've tried the Safari and Chrome's inspector modules and they don't even come close to competing with Firebug. I don't use Firefox for anything else other than development, which has the convenient side effect of letting me CMD-Tab over to Chrome for everything else that you browse for during development, and then back to Firefox when I'm ready to keep working. It's a nice little workflow hack IMO. Anyone else do something similar?

February 28, 2012

Metaprogramming Your ActiveRecord Objects at Runtime

I often store Ruby code in the fields of ActiveRecord object, giving me the ability to morph the system's behavior at runtime. Metaprogramming in the large, if you will. This is one of my favorite techniques in the Ruby on Rails sphere and one of the reasons I love working with dynamic languages. I'm not exactly recommending this technique to anyone, since it's akin to juggling live chainsaws. You especially don't want to architect an entire production system this way, since there are negative implications for peformance and testability. However, the implementations can be simple and a little bit here and there have gone a long way for me over the years.

While there are many ways to leverage this technique, today I'm going to cover an advanced style that involves changing metaclass behavior of a particular object instance using a real example from DueProps.

Screen shot 2012-02-28 at 1.37.51 PM
The central domain object in DueProps is the Prop (aka "Award"). Props are virtual tokens of appreciation that are exchanged amongst users. I'm trying to build a game, not greeting cards, so from the inception of the project I've wanted each Prop to exhibit unique behavior. However, Props are produced by my creative team and uploaded to the app as data: a collection of images with fields for description, points and multiplier values, etc. Programmers don't create individual classes of Props in source code, we have one class called Award with many instances.

Six months into production, we already have over 100 unique Prop instances and plan to have thousands over time. Having a class for each one, or even necessarily for each type of Prop won't scale development-wise. Therefore I devised a configuration system that allows us to program behavior related to Props at runtime, without needing to add new attributes to its database table everytime we come up with a cool new idea. Ruby and Rails already have the building blocks for doing exactly that.

Note that I'm not making much of an attempt to generalize the example code presented here. If you are able to wield this kind of technique then you'll be able to figure out how to apply it to your own problem domain. Here we go...

First I'll add a modifier_expr text field to my Award records.

class AddNominationClassExprToAwards < ActiveRecord::Migration
  def change
    add_column :awards, :modifier_expr, :text
  end
end

Now I'll add an after_initialize callback to my Nomination ActiveRecord class, which is what I want to be modified by Award at runtime.

class Nomination < ActiveRecord::Base

  belongs_to :award

  after_initialize :modify,
    if: lambda { award && award.modifier_expr? }

I only want to run this callback if I have an award instance, and only if that award instance has a non-nil nom_modifier value, hence the if expression.

Now the private modify method, which uses instance_eval to alter this object's metaclass object:

  private

   def modify
    instance_eval award.modifier_expr
  end

end

Altering the object's metaclass means that I can add or override its class methods without affecting any other Award instance with my modifications. For example, under normal circumstances Awards don't allow you to nominate yourself (give yourself a Prop) and that rule is enforced with a validation method:

class Nomination < ActiveRecord::Base

  validate :normal_validations

  def normal_validations
    if users.include?(submitter)
      errors.add(:user_ids, "You can't nominate yourself")
    end
  end

end

Note that I've left out other standard validations for clarity, because the one that matters here is the one that I want to override in the example. I named the method normal_validations to remind myself of its intention.

Let's make it so that Teamwork props can be given to yourself and others, but not only to yourself. We'll add this code snippet as its modifier_expr

def normal_validations
  if users.size == 1 && users.include?(submitter)
    errors.add(:user_ids,
      "You can't give Teamwork Props to only yourself")
  end
end

Now when validations run for a Teamwork nomination, my replacement normal_validations method will run instead of the original. Over time, as I build up a history of interesting validations, I may abstract them out into a module, giving myself an in-app API for the behavior of nominations instead of having to code things out explicitly.

Incidentally, I wrote this code very recently using TDD. Here is a gist of the resulting spec:

February 10, 2012

My last blog post had the primary effect I wanted, which was to get a bunch of iPhone developers familiar with the parameters of my project and to contact me with their proposal for how they might work with me.

By the way, I did get a lot of incoming interest in the opportunity and the reason I haven't gotten back to most of you is because I've been at a small conference (speakerconf) all week. I'll be back to normal operations next week.

February 03, 2012

Can I Find an Agile, Design-Savvy iPhone Developer?

UPDATE: I realized that my approach to this problem was all wrong so I deleted this post. See my new post Seeking iOS Expert for more information.

January 30, 2012

Rendering Collections of Heterogeneous Objects in Rails 3.2

This commit by Grant Hutchins & Peter Jaros back in July 2011 gives ActiveModels the ability to define their own partial paths by implementing a to_partial_path method. (You can find a great explanation of this killer new feature on José Valim's blog post about it. Just scroll down to #4 Custom Partial Paths.)

I'm blogging about it here, because it impacts a technique that I described in May 2011 as Rendering Heterogeneous Collections of Objects in Rails. If you happen to be using that technique or anything like it involving the use of ActiveModel's partial_path method, you'll get deprecation warnings like this when upgrading to Rails 3.2:

DEPRECATION WARNING: partial_path is deprecated and will be removed from Rails 3.2 (ActiveModel::Name#partial_path is deprecated. Call #to_partial_path on model instances directly instead.). (called from partial_path at .../config/initializers/partial_path.rb:12)

In this blog post I describe how to update my technique and get rid of the deprecation warning.

The heterogeneous aspect of my technique had to do with rendering collections of STI objects that all inherited from a single Event base class. For organizational reasons, I wanted to put all my disparate event partials in a single app/view/events directory. Without the hack, they'd need to be in their own view/foo_events/_foo_event.html.erb partials. Ugh.

In Rails 3.2, instead of having to monkey patch the framework to fix that crap, I can just implement to_partial_path on the Event base class like this:

class Event

  def to_partial_path
    "events/#{self.class.name.underscore}"
  end 

end

Ahh, that's much cleaner. Unfortunately, there's still a catch, but only if you use controller namespaces.

Rails 3.2 keeps the annoying merge of the namespace prefix as part of the generated partial path even if you override it using to_partial_path. The good news is that it is easier to get rid of it in Rails 3.2 than before. The monkeypatch in my earlier blog post now becomes:

ActionView::PartialRenderer.class_eval do

  private

  def merge_prefix_into_object_path(pfx, path)
    path
  end 

end

What this little bit of duck punching does is to ensure that when you try to render a collection of objects, it will always return the same partial path regardless of whether you are inside a controller namespace or not. I feel that behavior should be default or at least optional (which is why I describe the feature as annoying.) Let me know if you think it's worth submitting a patch.

Incidentally, in his blog post mentioned above Jose mentions needing to add an as: parameter to your call to render, like this:

render :partial => @activities, :as => :activity

In my testing and examination of the underlying Rails code, it is clear that the :as option is not necessary. The PartialRenderer object always checks for the existence of a to_partial_path method on the object being rendered and calls it if present. 

One more thing. Currently the documentation states the following (italics mine):

Instead of explicitly naming the location of a partial, you can also let PartialRenderer do the work and pick the proper path by checking `to_proper_path` method. If the object passed to render is a collection, all objects must return the same path.

I'm not quite sure why it says that all objects must return the same path, or why that would even be considered desirable behavior (it's really not!) The good news is that I can verify for you that it does work to return different partial paths within the collection, in other words, you can still do the rendering heterogeneous objects trick. In fact, depending on your naming convention, you may even get correctly name local variables in your partial for free.

The following method determines what to call your partial's automatic local variable corresponding to the current item of the collection being rendered:

def retrieve_variable(path)
  variable =
    @options[:as].try(:to_sym) || path[%r'_?(\w+)(\.\w+)*$', 1].to_sym

  variable_counter = :"#{variable}_counter" if @collection
  [variable, variable_counter]
end

That regex is a little tricky, but whether it's the intentional behavior or not, it's going to grab whatever is between the last underscore of your partial name and the dot to use as the variable name. In the case of my own Event hierarchy, that means each partial will get its own event local variable, even though the partials are named _user_created_event, _nomination_event, etc.

Perfect. Now to hope someone doesn't fuck it up in a future release thinking it's a bug :)

Important Update: Check out my buddy Lar's take on this stuff in Rails 3 using view resolver classes so that you don't have to monkeypatch anything.

My Companies

My Latest Book

My Book Series

My Conference

Bizconf is the only training event specifically crafted for owners and managers of small to mid-sized web design and development firms.

Dates for 2011 TBA