Rails 4 FlashHash Upgrade Gotcha

We upgraded an application from Rails 3 to Rails 4 this week and came across an interesting gotcha which I haven’t seen documented anywhere.

As we rolled out the Rails 4 version of the app we split the traffic between the upgraded Rails 4 app and our existing Rails 3 app. Some of the requests to Rails 3 app failed with the following exception:

Production NoMethodError: undefined method `sweep' for {"discard"=>[], "flashes"=>{"just_switched"=>true}}:Hash

Error Message: NoMethodError: undefined method `sweep' for {"discard"=>[], "flashes"=>{"just_switched"=>true}}:Hash

Where:
[GEM_ROOT]/gems/actionpack-3.2.19/lib/action_dispatch/middleware/flash.rb, line 239

It turns out that Rails 4 serialise the flash to the cookie differently to Rails 3. When Rails 3 attempts to deserialise it you get the above error. This error can also occur between Rails 3 and Sinatra apps.

To get around the problem whilst in the migration period we patched the Hash class. This would mean of course that the flash methods wouldn’t work, but as they are only used for minor presentation tweaks that seemed a good compromise.

class Hash
def now
Rails.logger.warn "Stubbing now during upgrade"
{}
end
def keep
# stub keep for upgrade purposes
Rails.logger.warn "Stubbing keep during upgrade"
end
def sweep
# stub sweep for upgrade purposes
Rails.logger.warn "Stubbing sweep during upgrade"
end
end

view raw
hash.rb
hosted with ❤ by GitHub

Creating an application form style layout using Prawn

I used the excellent Prawn gem recently to create a PDF. I needed an application form style like layout for the PDF like this:

Simple form layout

Simple form layout

Using Prawn this is straight forward. We just create two bounding boxes one for the left side and one for the right side of the page. Then we render the labels into the left bounding box and the form boxes into the right bounding box.

require "prawn"
Prawn::Document.generate("form.pdf") do
move_down 5
text "Application form", style: :bold, size: 25
bounding_box([0,cursor], width: bounds.width, height: 120) do
fill_color "DCDCDC"
fill_rectangle [0, cursor], bounds.right, bounds.top
fill_color "000000"
transparent(0.5) { stroke_bounds }
move_down 10
float do
bounding_box([5, cursor], width: 250, height: 200) do
["First name(s):", "Last name:", "Address line 1:", "Address line 2:"].each do |label|
pad(5) { text label }
end
end
end
bounding_box([260, cursor], width: 250, height: 200) do
stroke_color "000000"
fill_color "ffffff"
4.times do
fill_and_stroke_rectangle([5, cursor], 240, 18)
move_down 24
end
end
end
end

view raw
form.rb
hosted with ❤ by GitHub

Filling in the fields

filled-in-form

Filled in form

In some case I need to fill in the fields as well so I generalised the code into a method that takes a hash:

require "prawn"
def form(pdf, fields)
pdf.float do
pdf.bounding_box([5, pdf.cursor], width: 250, height: 200) do
fields.keys.each do |label|
pdf.pad(5) { pdf.text label }
end
end
end
pdf.bounding_box([260, pdf.cursor], width: 250, height: 200) do
pdf.stroke_color "000000"
fields.values.each do |value|
pdf.fill_color "ffffff"
pdf.fill_and_stroke_rectangle([5, pdf.cursor], 240, 18)
fill_color "000000"
pdf.text_box value, at: [10, pdf.cursor5]
pdf.move_down 24
end
end
end
Prawn::Document.generate("filledinform.pdf") do
move_down 5
text "Application form", style: :bold, size: 25
bounding_box([0,cursor], width: bounds.width, height: 120) do
fill_color "DCDCDC"
fill_rectangle [0, cursor], bounds.right, bounds.top
fill_color "000000"
transparent(0.5) { stroke_bounds }
move_down 10
form_fields = {
"First name" => "John",
"Last name" => "Doe",
"Address line 1:" => "20 Shrimp lane",
"Address line 2:" => ""
}
form(self, form_fields)
end
end

view raw
filledin_form.rb
hosted with ❤ by GitHub

Programming language stereotypes venn diagram

Inspired by Blame it on the voices’
Religions venn diagram of the day, I put together a programming language stereotypes venn diagram. Stereotypes are supplied by the Google instant search results for the questions: why is language? and why is language so?:
I drew the venn diagram together using a little processing.js script which was pretty straightforward and fun.

  var circleSize = 300;
  void setup()
  {
    background(255)
    size(900, 600)
    textAlign(CENTER);
    stroke(204, 102, 0);
    smooth();
  }

  void draw()
  {
    noFill();
    ellipse(400, 200, circleSize, circleSize)
    ellipse(300, 400, circleSize, circleSize);
    ellipse(500, 400, circleSize, circleSize);

    ellipse(800, 200, 150, 150);

    language = createFont("Helvetica",12,true);
    textFont(language);
    textSize(24);

    fill(0, 0, 0);
    text("Lisp", 800, 100)
    text("Java", 400, 30)
    text("Ruby", 100, 400)
    text("Javascript", 720, 400)

    textSize(20);
    text("Great", 800, 180)
    text("Used for AI", 800, 220)

    text("slow", 400, 330)

    text("hard", 400, 170)
    text("always updating", 400, 210)

    text("popular", 330, 300)
    text("red", 280, 400)

    text("used", 470, 300)

    text("bad", 520, 380)
    text("important", 520, 420)

  }