Sep 29, 2015

React on Rails with webpack - React is not defined

I'm using the react on rails gem in a project I've been working on. It's great for using of React components in a Rails project and supports both server side and client side rendering.

If you've got webpack set up to work as part of your rails stack (example) the gem is a breeze to set up.

I did run into 1 problem recently though. I was getting an error when I deployed to heroku that I wasn't getting in dev mode.

Exception in rendering!
ReferenceError: React is not defined

To fix this, make sure you are exposing react in your webpack config file.

module.exports = {
  module: {
    loaders: [
      // Make sure you have this line in your config!
      {test: require.resolve('react'), loader: 'expose?React'},
Apr 30, 2015

Remapping Keypad on a Kinesis Advantage Keyboard

If you're a kinesis advantage user, you'll know it has an embedded number pad and directional pad in the right key well. I want to quickly toggle this on and off.

The keypad button is in the top right though. That is too far away for my liking. Unfortunately, the keypad button cannot be remapped to another location.

If you have the foot switch accessory, the default action for it is to toggle the keypad. The foot switch CAN be remapped, but ONLY if you have one.

OR you can use a staple to mimic the foot switch so that you can remap it (and hence a keypad toggle) to a key on the keyboard. Here's how:

  1. Press and hold 'Program' and 'F12/Remap'. The green lights should be blinking fast.
  2. Locate the RJ-11 connecter on the back of your keyboard. (This is where the foot switch would connect). Use a staple or wire to connect the middle two pins (green and red) to mimic a foot switch press as a source key (thanks for the tip). The green lights should be blinking slower.
  3. Press the button you want to become your keypad button (destination key). The green lights should be blinking fast again.
  4. Press and hold 'Program' and 'F12/Remap' to exit remapping mode. The green lights should stop blinking.

IMPORTANT: Depending where you map the key to, you ay need to map it twice (with both the keypad on and off) to make sure your key can both toggle it on AND off.

Jan 18, 2015

An exception occurred: 'PG::ConnectionBad: PQconsumeInput() SSL connection has been closed unexpectedly' on Heroku

tl;dr: Always use at least a standard database plan for production apps on Heroku.

Recently I came across some unexpected exceptions in an app running on Heroku. Others have had a similar experience.

# It starts with the connection being unexpectedly closed
An exception occurred: 'PG::ConnectionBad: PQconsumeInput() SSL connection has been closed unexpectedly'
# Followed by a bunch of these
An exception occurred: 'PG::ConnectionBad: PQsocket() can't get socket descriptor'

It turns out that the hobby tier for databases on Heroku have "Unannounced maintenances and database upgrades". Heroku responded basically saying: `The hobby plans have their connections closed sometimes. ActiveRecord should deal with it via a connection pool. If not, you'll have to reconnect.`. The exceptions I saw were on a long running background job (in this case a RabbitMQ consumer), where ActiveRecord was not handling reconnecting. :-(

If you get these exceptions, the first thing to do is to restart your server, which you can do by running this command.

#assuming your git remote is named production
heroku restart -r production 

The best long term fix is to upgrade you Heroku database to at least the standard-0 plan (currently $50/mth). This will prevent connections from being unexpectedly closed. It will also help performance.

If you really don't want to upgrade, you can try reconnecting with your code

  # Do the action you want to do here.
rescue ActiveRecord::StatementInvalid => e
  # Make sure someone finds out!
  # ExceptionNotifier.notify_exception(e)
  # Rails.logger.error { "[insert details here] : {e}" }

  # Now, try to reconnect
  # Don't forget - you need to make sure to retry the original action somehow.

On a final note - there is no way to tell when this might happen. For this particular app, there is both a production and a staging server that are configured in the same way. These exceptions have occurred multiple times on different days on the production server, but have never happened on staging.

Oct 17, 2014

Case insensitive LIKE in postgres (or why it's important to use the same DB for test and prod)

I recently ran into a production bug where a simple 'like' query wasn't working properly, even though I had passing tests which explicitly tested this case.

The problem, of course, is that I was using different databases: sqlite on my dev machine, and postgres on the production machine.

# case insensitive on sqlite
# BUT case sensitive on postgres
where(["description LIKE ?", like_value])

In postgres you need to use 'ILIKE' for case insensitive queries.

# case insensitive on postgres
where(["description ILIKE ?", like_value])

The more important takeaway, however, is to make sure that you use the same database in your test env as do in production!

Oct 2, 2014

Active Admin : Undefined Method `per' for #<ActiveRecord::Relation:> with will_paginate

If you are using both will_paginate and activeadmin, you may see an error like this:

Undefined Method `per' for #<ActiveRecord::Relation:0x.....>

This is because Active Admin uses kaminari for pagination. To fix this error make sure will_paginate and kaminari don't collide, as per : using active admin with will_paginate by adding some config to kaminari:

# create config/initializers/kaminari.rb
Kaminari.configure do |config|
  config.page_method_name = :per_page_kaminari

Google is likely to lead you to a github discussion that tells you to use a very specific version of will_paginate, but obviously the solution above is better.

Sep 4, 2014

Focusing your Guard

Guard is great for getting rapid feedback when you'd doing tdd/bdd.  By default, the guard plugins for spec and cucumber watch and run a lot.  Recently I've found myself wanting to just have guard only run the tests which I am focusing on.

After setting up guard-rspec and guard-cucumber I updated my Guardfile to:

def watch_cucumber
  watch(%r{^features/support/.+$})          { 'features' }
  watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }

def watch_rspec
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }

  # Rails example
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$})          { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }
  watch('spec/rails_helper.rb')                       { "spec" }

  # Capybara features specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$})     { |m| "spec/features/#{m[1]}_spec.rb" }

  # Turnip features and steps
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }

group :tests do
  guard :cucumber, cmd: 'spring cucumber' do

  guard :rspec, cmd: 'spring rspec' do

# Run guard -g focus
# It will watch all the reguslar rspec and cucumber watches files
# but will only run:
# cucumber tests tagged with @focus
# rspec tests with :focus => true
group :focus do
  logger level: :warn
  logger template: ':message'

  guard :rspec, cmd: 'spring rspec --tag focus' do

  guard 'cucumber', command_prefix: 'spring', bundler: false, cli: '--profile focus' do

This creates 2 groups ('focus' and 'tests'). The focus group only runs the tests that I have tagged as a focus. ('@focus' in cucumber and 'focus: true' in spec). The focus:cucumber guard file definition tells cucumber to use the profile 'focus'. Alternatively I could have used the focus_on option but a profile feels cleaner. In order to use a profile you need to set it up in cucumber.yml like so:

focus: --format progress --strict --tags @focus

Then, when I want guard to automatically run only the tests I've tagged with focus, I run:

guard -g focus

I find this really helps me get into the flow when writing code. If I'm doing something that could affect a to of areas, I can still run guard -g tests to fall back on the default guard behaviour.

Sep 5, 2012

Restarting iPhone with broken lock button

I ran into a situation where I needed to restart my iPhone, but unfortunately the lock button was broken.  Normally a restart requires holding the lock button and home button for 8 seconds.

Fortunately - you can use AssistiveTouch to get on screen versions of those buttons.

  1. Open the Settings app
  2. Tap "General"
  3. Tap "Accessibility"
  4. In the "Physical & Motor" section tap on "AssistiveTouch"
  5. Turn "AssistiveTouch" on
  6. A white circle will appear in the bottom right. Tap it.
  7. Tap on "Device".
  8. Hold the on screen "Lock Screen" icon and the real home button at the same time, for 8 seconds.
  9. Voila!

After your iphone restarts you'll want to go back an turn AssistiveTouch off.

Mar 31, 2012

On Techcrunch

Today I tweeted a screenshot of Tweetdeck when it was letting me access other people’s accounts.  Techcrunch (and now CNN, CNET, WSJ, MSNBC) picked up the story.  It’s nice to get on Techcrunch, I just wish it had been for Little Big Thinkers or MomentVille :-S

To be clear – I DID NOT HACK Tweetdeck (I’m looking at you WSJ).  All I did was log in and I saw and could post from the other accounts.

Mar 29, 2012

Rocket Speller is What’s Hot

We’re very pleased that Rocket Speller has been featured in the New and Noteworthy and What’s Hot sections of the iTunes App Store for the Education, Educational Games, and Kids Games sections.

We also hit a big milestone recently with over 1,000,000 words spelled!  If you haven’t got it yet, go get it now. It’s a FREE!