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

iPhone fever … fading?

It feels like trolling, but an article I recently read on The Atlantic Wire describes weakening iPhone demand as reported in the Wall Street Journal. (Aside: I think using the term “fan boy blogs” is just belittling.) But it feels like the real story here is that Apple’s grip on the mindshare of would-be buyers is loosening. I don’t know for sure but there could be truth in this.

A huge weakness for Apple I’ve long felt has been a solid command in web services—or any web services beyond what iTunes scantily provides. I can’t embed or link to Apple maps, link to an Apple-based social feed, or even use Apple-hosted services. What about Keynote presentations or online editing of Pages documents? iMovie or Garage Band galleries or portfolios hosted by Apple would be huge.

Google and Amazon are way out ahead here and because they now pretty much have the backbone of the internet where all the web services live they command the future. Aside from programming in Objective C, I can’t use web APIs to push and pull information, can I? I use APIs and hosted services because I can multiply my developer power by a factor only limited by my access to funds—and outside funding can make that ceiling very very high. These companies are companies I can grow into, not grow out of.

Being a leader in devices is nice, but a saturation point gets reached where everyone has the same phone or the same app and the “cool” factor dictates that a rebel band find a new niche. Is it truly the rebels that define the future? I think there is a strong pull by them at the very least. It’s not just because something is labeled “awesome” that makes an app, a service, or a product a hit. Rather, when eyeballs and creative energy is being applied to something it gets better. And that means the innovation goes there. If innovation is solely left to Apple’s employees they can be hamstrung by layers of management or company directives. Innovation in the wild is unbridled creativity that finds organic applications for technology that a corporate mindmeld can’t achieve.

Does that mean that Apple doesn’t innovate? Of course it does—my iPhone and MacBooks are among the best-built and well-designed devices I’ve ever used on a regular basis. But Apple certainly doesn’t seem to care the web. They are only providing access to it. And if you also believe that the future is in device-agnostic connected information, then you have to look beyond pretty Gorilla Glass and aluminum to see where the data is going—and the data is not going to Apple’s servers.

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.)

Setup Amazon’s (AWS) Simple Email Service (SES) + DKIM with DreamHost’s DNS

This is mostly a note to myself on how to do this. :)

Amazon’s Simple Email Service (SES) seems pretty easy to use but working with the DreamHost DNS management proved slightly tricky because of the large amount of steps and the order of them. Getting the DNS values right was also not completely obvious. Here’s my recipe:

1. Register a Domain

  • Log into your DreamHost Panel
  • Add a domain (Domains -> Registrations)

2. Change the Domain to DNS-only

  • Click the Edit link for your domain (Domains -> Manage Domains -> Edit)
  • Scroll all the way down to the DNS Only section
  • Choose that

3. Turn on Amazon SES

  • Create your Amazon Web Services (AWS) account
  • Go to the console page
  • Click SES to get to your SES dashboard
  • Click Verify New Sender
  • Click Verify a New Email Address
  • Wait until your new email address shows up in the Email Addresses list (or hit Refresh)
  • Click on the new email address to get its Details, you should see Status: pending verification

4. Verify Your Email Address

  • You should have gotten an email with the verification link in it: click it
  • Go back to the SES dashboard and click on the new email

5. Update DNS custom records on DreamHost

  • Return to your DreamHost Panel
  • Click Domains -> Manage Domains
  • Click the DNS link under your domain
  • Scroll down to the “Add a custom DNS record to YOURDOMAIN.com” section
  • Add a TXT record with the full value:
    • Notice: SES might recommend a text record of _amazonses.YOURDOMAIN.com. Do not paste this full value in! You must remove the “.YOURDOMAIN.com” part. That’s right, just enter: _amazonses
  • You might also want to add DKIM to your domain as well.
    • Click the “copy record” link to see the full name and value.
    • Notice: Again, SES might recommend a CNAME record that looks like abcdefghijklmnop1234567890._domainkey.YOURDOMAIN.comYou need to remove “.YOURDOMAIN.com” for DreamHost.

6. Wait…!

  • It takes a while for the DNS records to propagate. You will also get an email notification that says the sender is verified and the DKIM verification is complete.

 

“Omakase” is how DHH puts it

You may have seen this blog post by DHH: “Rails is omakase.” This pretty much encapsulates my thinking about Rails. Rails isn’t for everyone. And it certainly isn’t for every task.

What I think Rails is good for:

  • Scaling up a team of people to work on a web project who have never worked together before
  • Organizing web assets like images, stylesheets, JavaScript
  • Connecting to other APIs
  • Relational databases

If you start to do other things you might wind up just trying to monkey patch the whole system or create your own objects that mimic ActiveModel things. That’s fine, just be prepared to do a lot of work.

I like not doing a lot of work. It’s like how I used to like upgrading and building my own PCs. That time has passed and now I just want to pickup my MacBook Air and be done with it. Yeah, it’s not the fastest or has the largest storage, but it gets the job done and I don’t have to worry about anything else. I’m still a techie, but I like to focus on solving the user experience problem and not worry about the plumbing of a system. I’ll leave that to DHH and the Rails core team.

Update: The tiny change that sparked a huge conversation on Github and mislav’s post summarizing the conversation.

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: