Error installing Ruby 2.0.0-p0 with Homebrew, OpenSSL 1.0.1e errors

This has been driving me crazy all evening. It turns out when you use Homebrew, the installation directories for OpenSSL and Readline libraries are different. Fortunately, there’s a compilation override as mentioned on the ruby-build Issues page:

RUBY_CONFIGURE_OPTS=”–with-openssl-dir=`brew –prefix openssl` –with-readline-dir=`brew –prefix readline`” rbenv install 2.0.0-p0

You can of course change which Ruby version to install, like 1.9.3-p392.

Stubbing Devise helpers to get RSpec to pass

I have literally been bashing my head into the wall this past week trying to figure out how to get RSpec view tests to pass when using Devise. I finally figured it out after a lot of trial and error because there is a particular combination of things you have to have in place to make things work.

First, the what: I added some navigation to a menu bar that only shows up if the user is logged in. It was just:

<nav>
  <% if user_logged_in? %>
    <%= link_to ...blah... %>
  <% end %>
</nav>

The trouble is, I kept getting this error:

Failure/Error: render
ActionView::Template::Error:
  undefined method `authenticate' for nil:NilClass

The first problem was figuring out why Devise’s helpers weren’t being loaded. According to the Devise documentation you should create a spec/support/devise.rb file like:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

But what about running view tests? The missing line is:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
  config.include Devise::TestHelpers, :type => :view
end

Great! But that only solves the missing helpers. What about getting current_user to actually return a user? I tried all sorts of things and spent days searching for the answer and it comes down to stubbing out the helpers … but which object? The answer is on the view itself:

module ApplicationRspecHelpers
  def stub_user
    @user = double('user')
    @user.stub(:id => 100)
    view.stub(:user_signed_in? => true)
    view.stub(:current_user => @user)
  end
end

That works exactly as I want it. So now I can write my view tests like:

require 'spec_helper'

describe 'layouts/application' do
  include ApplicationRspecHelpers

  describe 'authentication nav' do
    context 'when authenticated' do
      it 'should have Log Out' do
        stub_user
        render
        rendered.should have_selector('nav a', text: 'Log Out')
      end
    end
  end
end

Hide the iPhone address bar (navigation bar) without appearing to move the page

The popular trick most people apparently do to remove that iPhone address bar in mobile Safari is to scroll the window up by 1 pixel:

window.scrollTo(0, 1);

My problem with this is it shifts the canvas up thereby losing 1 pixel. It turns out you can specify a fraction of a pixel and the trick works just fine still:

Note: I added a 100ms delay to it because it made it work better. I tried listening to the window “load” event it just wasn’t as reliable.

Setup Rails gem Devise to work with Amazon’s Simple Email Service (SES)

This post is mostly for me to remember how to do this. :)

It turns out that getting Devise (or the built-in mailer for that matter) to send email through Amazon Web Services’ (AWS) Simple Email Service (SES) is stupid easy. Most of the work is just getting SES set up properly. After that:

1. Verify some email addresses

  • If you just set up SES you are probably still in sandbox mode. You need to add new recipients on the Verified Senders dashboard first.

2. Generate your SES SMTP Credentials

  • You must generate a new SMTP User and SMTP Password to connect ActionMailer to AWS SES. These are NOT your AWS login credentials!
  • Go to the SMTP Settings dashboard.
  • Click the button “Create My SMTP Credentials”.
  • Copy the information out—it will not be displayed again!

3. Edit your ActionMailer settings in the production.rb file

That should do it! Send yourself a test message.

Remember: you can only send to verified email addresses while in Sandbox Mode. You must request Production Mode access to send to any unverified email address. Otherwise you will probably see the Rails log error: Net::SMTPFatalError (554 Message rejected: Email address is not verified.)

Lessons learned: Ruby’s local variable scoping behavior

I was looking over some code that I thought should have generated an error:

def something
  # ... blah blah blah

  if some_condition_never_run
    foobar = some_method_making_a_value
  else

  call_other_method foobar  
end

In this case I thought foobar would have been undefined at the point where call_other_method() is run, but no! The value nil was being passed in. I somehow expected instead the error: undefined local variable or method `foobar’. Why didn’t this make an exception? Scoping.

Check this:

Even though the stuff in the if block isn’t executed the fact is that foobar is assigned at least once in that localized scope. Therefore when Ruby runs call_other_method() it knows it’s a variable (not a method) and can safely return nil. In the above example, function c‘s proc creates that local variable context within c() and so c‘s foobar doesn’t know about the proc‘s foobar.

It makes sense when you think about it but I still find it awkward.

See also:

Underscore.js’s debounce() is a great way to kill that double-click

Your mission: make sure your users don’t double-click a form button, but also don’t forever deny them future clicks.

This is a throttling problem really. And recently I was working on a UI element that was just being clicked way too often, my suspicion being the users were double-clicking it. I was going to do what I normally do: set a timeout on the click handler that would prevent more than 1 click being fired during a time period. But that just ends up making me write more code—I’d rather use a library!

Turns out that I’m using Underscore.js a little bit more these days and there is a built-in utility called debounce. It pretty much does exactly what I want it to do. As a comparison, there’s a similar function called throttle, but that still fires at least one more click event. Check this example out:

HTML5 bonus: data-abc-def attributes surfaced as .dataset.abcDef

TIL that the HTML5 spec has a special treatment for data attributes on tags. Sure, you could get at any attribute before like so:

<sometag id="yeah" foo="123" bar="def" data-something="456" data-something-else="789">

var el = document.getElementById('yeah');
alert( el.getAttribute('data-something') );

What I did not know was the new HTML5 feature called dataset that lets you get at the data members:

<sometag id="yeah" foo="123" bar="def" data-something="456" data-something-else="789">

var el = document.getElementById('yeah');
alert( el.dataset.something );
alert( el.dataset.somethingElse );

Yep. The dataset attribute on an element does special things to data-* attributes. It makes them Hash-like and also transforms the dash-delimited attribute names into camelcased hash keys!

This is so much win.

Using the googleon/off HTML hints to tell Google not to use parts of the page for its snippet

This morning I was tickled to learn that there are ways to add some fine-granularity control to Google’s page indexing behavior. If you add some hints to your page in the form of HTML comments you can tell Google’s search engine to ignore the text for all purposes, for the search result snippet, and more.

For instance, if you search for “granularity seqmedia” you should come up with a hit to this very post. But the snippet text should not be the first sentence in this post but rather the preceeding sentence. (I’m using the googleoff:snippet hint.)