Beware of the Habit Creature
2009-01-23
You know, I write controller tests day in, day out. I’m used to seeing this:
1 should "be valid" 2 assert assigns(:user).valid? 3 end
So, when I write a Rake task and I render some data, I need my assigns:
lib/tasks/ssl.rake
1 namespace :ssl do 2 task :build => :environment do 3 rm_rf "config/sites/" 4 mkdir "config/sites" 5 6 Dir["config/ssl/*"].each do |path| 7 site = File.basename(path) 8 parts = site.split(".") 9 10 puts "Processing #{site}" 11 assigns = { 12 :wildcard_domain => parts[0] == "_", 13 :domain_name => parts[-2..-1].join("."), 14 :ssl_key_file_path => File.expand_path(File.join(Dir.pwd, path, "#{site}.key")), 15 :ssl_certificate_file_path => File.expand_path(File.join(Dir.pwd, path, "#{site}.crt"))} 16 17 next "Skipping #{site}: #{assigns(:ssl_key_file_path)} does not exist or is unreadable" unless File.file?(assigns(:ssl_key_file_path)) 18 next "Skipping #{site}: #{assigns(:ssl_certificate_file_path)} does not exist or is unreadable" unless File.file?(assigns(:ssl_certificate_file_path)) 19 20 view = ActionView::Base.new([File.dirname(__FILE__)], assigns) 21 22 File.open(File.join("config", "sites", "#{site}.ssl.conf"), "w") do |site_config| 23 puts "Writing config/sites/#{site}.ssl.conf" 24 site_config.write view.render("ssl.conf.erb") 25 end 26 end 27 28 view = ActionView::Base.new([File.dirname(__FILE__)], {}) 29 File.open(File.join("config", "sites", "default.conf"), "w") do |site_config| 30 site_config.write view.render("default.conf.erb") 31 end 32 end 33 end
Can you spot the error on lines 17 and 18? Go on, I’ll wait…
You want some help? Here’s the backtrace:
1 ** Invoke ssl:build (first_time) 2 ** Invoke environment (first_time) 3 ** Execute environment 4 ** Execute ssl:build 5 rm -rf config/sites/ 6 mkdir config/sites 7 Processing _.xlsuite.com 8 rake aborted! 9 undefined method `assigns' for #<Object:0x389a0> 10 /Users/francois/Documents/work/xlsuite-stable.svn/lib/tasks/ssl.rake:17
Yes, took me a while too. I was calling a method named assigns, not using the Hash named assigns.
Habit, when you take hold of me…
blog comments powered by Disqus