Aaron Van Bokhoven / aaronvb

I am a Software Developer and a Portrait Film Photographer.
  • I love coding with Ruby and Ruby on Rails, and I love shooting with film.
  • I currently live in Honolulu, Hawaii, but frequent Chicago and California.
  • I believe that programming is a form of art, like painting and photography, where you can express your ideas and logic and see it transform into something real.
Aaron Van Bokhoven
photo: kipkeston
Email me at bokhoven@gmail.com. View my Photography Portfolio and my Photo Blog (tumblr). I'm also on Twitter.

My current work:

Hawaii Photo Rental
http://www.hawaiicamera.com
Buoy Alarm
http://buoyalarm.com
Flux Hawaii Magazine
Summer 2011, Fall 2011, Winter 2011(cover) issues
http://fluxhawaii.com

Open source contributions:

Coffee-Resque (my fork)
https://github.com/aaronvb/coffee-resque
Pow
http://pow.cx (github)
My Gists
http://gist.github.com/aaronvb

You can also find me at:

Github
http://github.com/aaronvb
Photography portfolio
http://aaronvb.com/photo
Junkparty
http://junkparty.com
Flickr
http://flickr.com/photos/aaronvb

Jul 15, 2010

If your PDFKit and wkhtmltopdf is working on your development server, probably in OSX, but not working on your production server, which is probably running linux, it's because you're missing X Server.

There are a few ways around this, some emulate X Server, but that seems hacky. I read through the wkhtmltopdf docs and read that a patched qt framework will allow you to use wkhtmltopdf without using X Server.

Here's what I did on my Ubuntu Box to get this to work (note: the compile time for qt was almost 2 hours, and you'll also need to have git installed):


sudo apt-get build-dep libqt4-gui libqt4-network libqt4-webkit
sudo apt-get install openssl build-essential xorg git-core git-doc libssl-dev

mkdir ~/sources
cd ~/sources
git clone git://gitorious.org/+wkhtml2pdf/qt/wkhtmltopdf-qt.git wkhtmltopdf-qt
cd wkhtmltopdf-qt
./configure -nomake tools,examples,demos,docs,translations -opensource -prefix ../wkqt
make -j3
make install
cd ..

Next install wkhtmltopdf:


git clone git://github.com/antialize/wkhtmltopdf.git wkhtmltopdf
cd wkhtmltopdf
../wkqt/bin/qmake
make -j3
make install

Run 'wkhtmltopdf' in shell and you should see it load correctly instead of seeing the x server error. Running 'wkhtmltopdf-proxy' should do the same, which is what PDFKit uses.


Jul 12, 2010

I've been working on a class that's pulling information from an API and wanted to create dynamic attributes from a hash(json).

I came across this link:

http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object

It works pretty well, but I haven't done much benchmarking. I've read in the past that define_method can be slow and has memory leaks.

Following that post, I read the comments and came across OpenStruct:

http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html

I have it working with what I need but there's still more testing to be done. Just thought I would share.


Mar 10, 2010

Need to pass a Ruby Array to a Javascript Array?
Here's a little gem I found a while ago that I'm reusing in my current project.


>> a_ruby_array = ["one", "two", "three"]
=> ["one", "two", "three"]
>> "['#{a_ruby_array.join('\',\'')}']"
=> "['one','two','three']"


Mar 06, 2010

Here's a little trick I found when handling multiple nested models in a form that require validations for each model.

Spec time. For example, we have a model, Author, which has_many Books and has_many Magazines. Book and Magazine has validations. On a single page, we create a three forms for Book, Magazine, and Author. There is also an option select that a user can choose if the Author has a Book or a Magazine.

Pretty straight forward. Let's do some code.


#models/author.rb
class Author < ActiveRecord::Base
has_many :books
has_many :magazines

accepts_nested_attributes_for :books
accepts_nested_attributes_for :magazines

validates_presence_of :name
end


#models/book.rb
class Book < ActiveRecord::Base
belongs_to :author
validates_presence_of :title,
:genre
end


#models/magazine.rb
class Magazine < ActiveRecord::Base
belongs_to :author
validates_presence_of :title,
:genre
end

Basic model setup with validations and accepts_nested_attributes_for. More info on that can be found in the Ruby on Rails API - Nested Attributes.

Moving on to the form setup..


#views/authors/new.html.erb
<% form_for @author do |f| %>
<%= f.error_messages %>


<%= f.label :name %>

<%= f.text_field :name %>



<%= f.radio_button("media_type", "book" %><%= f.label :media_type_book, 'Book' %> [?]
<%= f.radio_button("media_type", "magazine" %><%= f.label :media_type_magazine, 'Magazine' %> [?]


<% f.fields_for :books do |book| %>


<%= book.label :title %>

<%= book.text_field :title %>



<%= book.label :genre %>

<%= book.text_field :genre %>


<% end %>

<% f.fields_for :magazines do |magazine| %>


<%= magazine.label :title %>

<%= magazine.text_field :title %>



<%= magazine.label :genre %>

<%= magazine.text_field :genre %>


<% end %>
<%= f.submit 'Submit' %>
<% end %>

That sets up the form with the nested model, now for the controller code.


#controllers/authors_controller.rb
class AuthorsController < ApplicationController
def new
@author = Author.new
books = @author.books.build #this builds the nested form in the view
magazines = @author.magazines.build #this builds the nested form in the view
end

def create
param_hash = params[:author]
if params[:author][:media_type] == "book"
param_hash.delete("books_attributes")
elsif params[:author][:media_type] == "magazine"
param_hash.delete("magazines_attributes")
end
@author = Author.new(param_hash)
@author.save
end
end

Instead of passing the params straight to Author.new, it's put into a hash variable. Then the params get checked for a title and genre, if empty delete the key from the hash and pass to Author.new. Activerecord wont see the book param and will skip the validations for it.



© Aaron Van Bokhoven