bitcrowd

Agile Ruby on Rails and iOS Development from Berlin
Airship-colored

Disable console.log in production

You know the problem – sometimes you just forget one console.log statement in your JavaScript and have console logs in production mode or even worse – the script is completely broken on IE. Here I describe a way how you can be sure that console is defined and quiet in production mode:

Define a HTML5 data-atribute in your applictaion.html.haml:

!!!
%html
  %head
    ...
  %body{data-env: Rails.env}
    ...

Second step is to include this javascript snippet, which checks – after the DOM is loaded – if we are in production mode or if console is undefined. If so, we define console.log as an empty function.

$(function(){
  if ($('body').data('env') == 'production' || typeof console == "undefined"){
    var console = { log: function() {} };
  }
});
glyphicons-sample

Supernice Iconset

Big props go out to Jan Kovařík for designing the amazing icon set glyphicons and releasing it under CC.
Here’s a little snippet:

Great job!

Test PDF output with Cucumber

Want to really test what’s in some PDF output of your Rails application?
If it’s enough for you to test the raw text only, here is a solution:

Add this to your Gemfile (e.g. within your :cucumber group):

gem 'pdf-reader', :git => 'https://github.com/yob/pdf-reader.git'

Put this in a new file in your step definitions folder:

When /^I inspect the generated PDF$/ do  
  pages = Array.new
  reader = PDF::Reader.new(StringIO.new(page.source))
  @pdf = reader.pages.collect(&:text).join("\n")
end

Then /^I should (not )?see "([^\"]*)" in the PDF$/ do |inversion, text|
  if inversion
    @pdf.should_not include(text)
  else
    @pdf.should include(text)
  end
end

Use your new steps:

When I follow "Link to some PDF"
  And I inspect the generated PDF
  Then I should see "Something" in the PDF

Drawback: Whitespaces may be missing in the text output, so you may have to look for “MyText” instead of “My Text” (if you have a suggestion how to fix this, please use the comments).