Monday, June 29, 2009

Testing File Uploads in Rails

I was frustrated today trying to figure out why the HTTP functions in Integration::Session were converting my UploadStringIO class parameters to String instances using 'inspect'. Turns out for these specific Rails 2.1.2 performance tests you have to use a special class named ActionController::TestUploadFile. Here is the sample documentation:

# Essentially generates a modified Tempfile object similar to the object
# you'd get from the standard library CGI module in a multipart
# request. This means you can use an ActionController::TestUploadedFile
# object in the params of a test request in order to simulate
# a file upload.
#
# Usage example, within a functional test:
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
#
# Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)

Thursday, June 18, 2009

Migration Helpers To Add Foreign Keys etc...

Rails plugin for adding foreign key constraints in a simple way:
http://github.com/patientslikeme/migration_helpers/tree/master

add_foreign_key 'address', 'user_id','users'

They also have a few simplified utility methods:

update_row 'books', :set => { :price => 4.99 }, :update_all => true
delete_row 'books', :author => 'Tolkien'

Enjoy.

Friday, June 12, 2009

This code sucks

Nat Pryce wrote an interesting article where he comments on "Complaining about Other Peoples Code". Pay attention to what your team members say while reviewing legacy code. Are they passing judgment on the aesthetics of the code or are they actually asserting that the author is a bad person?

Wednesday, June 10, 2009

ruby-prof, script/performance, and vendor/gems

It seems I keep having all of this fun running into issues working with Rails 2. My most recent challenge was unpacking ruby-prof as part of a Rails 2.1.2 application. Over the past few months I have spent some time getting our project to the point that we were able to unpack our required gems. Everything was working great, until I tried to using ActionController::RequestProfiler a.k.a. script/performance/request to profile a controller action. However when I tried to use it our application complained that: `gem install ruby-prof` to use the profiler
Huh? I thought I unpacked the gem in the vendor directory? A quick directory list revealed that it was there: vendor/gems/ruby-prof-0.7.3
What could be wrong? I started to trace the request script to find this little gem call in the Rails source code:
     # File actionpack/lib/action_controller/request_profiler.rb, line 146
146: def load_ruby_prof
147: begin
148: gem 'ruby-prof', '>= 0.6.1'
149: require 'ruby-prof'
150: if mode = options[:measure]
151: RubyProf.measure_mode = RubyProf.const_get(mode.upcase)
152: end
153: rescue LoadError
154: abort '`gem install ruby-prof` to use the profiler'
155: end
156: end
Really? Another Evil monkey patch in my source code:
     # File /lib/core_ext/action_controller/request_profiler.rb
def load_ruby_prof
begin
require 'ruby-prof'
if mode = options[:measure]
RubyProf.measure_mode = RubyProf.const_get(mode.upcase)
end
rescue LoadError
abort '`gem install ruby-prof` to use the profiler'
end
end

#config/environment.rb

Rails::Initializer.run do |config|
config.load_paths += Dir["#{RAILS_ROOT}/vendor/gems/**"].map do |dir|
File.directory?(lib = "#{dir}/lib") ? lib : dir
end
end

# Add pagination requirement
require "#{RAILS_ROOT}/lib/core_ext/action_controller/request_profiler"

Blogs I read