A Feature All PVR's Should Totally Have
by Blake Watters.
Posted in Personal. Tagged with technology, television.
I was just ripping through the channels on my TV and caught a commercial for a show I was interested in watching, but was featured on a different network at a time 15 hours from now. Even worse, the show is available in HD, but to get there I have to jump 20-50 channels to find it.
There should be a "record the show featured on this commercial" button available to set up recording of any show being advertised, in HD if available, across channels.
This button has interesting possibilities across the many TV advertising contexts.
Hide comments
This weekend I will be attending and volunteering at BarCamp RDU this Saturday @ Red Hat Headquarters in Raleigh. I hope the turnout is big and the event is a big success!
Come out and join us!
I somehow disallowed anonymous commenting over the last few months… Sorry!
So a good friend of mine reads a lot of patents for work. He hates reading them on the computer and prefers to archive them with hard copies. The process was tedious, because the Patent Office website generally blows and does not offer an archived download of all the images. So he was manually control + click, save as downloading each image and then organizing them. What a waste of time, right?
Ruby to the rescue! I grabbed Hpricot, enabled Firebug, and headed to the Google Patents web site and started hacking. Thirty minutes and ~fifty lines of code later I had completed work on )).
Maybe someone else will find it useful or a good reference on how to work with Hpricot. Enjoy!
Dead Simple Rails Dependency Management with install_gems
by Blake Watters.
Posted in Software Development. Tagged with capistrano, rails, ruby.
- Grant your deployment user rights to run gem via sudo. This can be accomplished by adding the following to /etc/sudoers on Unix platforms:
deploymentuser ALL=(ALL) NOPASSWD: /usr/bin/gem - Create two Gem configuration files under the config directory of your Rails root. These files should be named gems.yml and developer_gems.yml. The gems.yml file contains a YAML formatted Array of Arrays corresponding to a particular Gem and a GemConfigure compatible versioning string. Here is an example RAILS_ROOT/config/gems.yml file:
[‘rails’, ’= 1.1.6’][‘facets’, ‘>= 1.7.46’][‘mysql’, ‘>= 2.7’][‘rmagick’, ‘>= 1.14.1’][‘chronic’, ‘>= 0.1.5’]
The developer_gems.yml file contains a similarly formatted list of Gems that are only required for developers of the app. This may include mocking libraries, deployment tools, etc. Here’s a sample RAILS_ROT/config/developer_gems.yml file:
[‘production_log_analyzer’, ‘>= 1.3.0’][‘action_profiler’, ‘>= 1.0.0’][‘sql_dep_graph’, ‘>= 1.0.0’][‘capistrano’, ‘>= 1.2.0’][‘flexmock’, ‘>= 0.4.3’] - Next, download install_gems (328 Bytes) and place it in your RAILS_ROOT/script/ directory and make it executable.
- Then add the following lines to your environment.rb file:
- Load our dependent gems
require ‘gemconfigure’
gem_config = open("#{RAILS_ROOT}/config/gems.yml") { |f| YAML.load(f.read) }
Gem.configure(gem_config) - Finally, configure Capistrano to run install_gems when deployments occur. Here’s an example task:
task :after_update_code, :roles => :app do run "#{release_path}/script/install_gems
end
Congratulations and welcome to dependency management nirvana! You can now upgrade your local Gems by running script/install_gems (use the -d switch to load developer Gems as well). When you change the YAML file, your servers will be automagically brought up to date on the next deployment. All dependent libraries will be automatically loaded when your Rails processes start. Pretty slick, eh?
I’ve been meaning to bundle this up into a Rails plugin and will do so when the mood strikes me or there’s enough popular demand.
Enjoy!
I’ve fixed some bugs and added model class whitelisting to the excellent show_model script from the thoughtbot guys. For the uninitiated, show_model is a slick little script that will show you a ton of information about the models in your Rails applications. Here’s an example run:
aria:~/near-time/near-time.net-trunk blake$ ./script/show_model ControllerPermission
ControllerPermission < Permission < ActiveRecord::Base Class Methods: controller_class_for Associations: has_many Roles DB Columns: action (string) controller_class (string) created_at (datetime) id (integer) model_class (string) tab_title (string) type (string) verb (string)
My updated version will not ‘hide’ models that are loaded by your environment and avoids some nil object errors I ran into.
This is an invaluable tool, I highly recommend adding it to all your Rails projects!
Force MacPorts to Install Ruby 1.8.4
by Blake Watters.
Posted in Software Development. Tagged with rails, raleigh.rb, ruby.
Attached is a simple script and associated files that will allow you to force MacPorts to install Ruby 1.8.4 instead of 1.8.5. Why they don’t support previous versions is beyond me and its by no means a trivial exercise to figure out how to do it.
Usage
$ tar jxvf macports-ruby_1.8.4.tar.bz2 $ cp -R macports-ruby_1.8.4/* /path/to/my/rails/app/script $ cd /path/to/my/rails/app $ ./script/hack_ruby_portfile.sh $ sudo port install ruby
Today I discovered the excellent RubyOSA library. It bridges Mac OS X’s Open Scripting Architecture. It allows you to simply script many Cocoa applications. The coolest part is that it can generate RDoc documentation against any OSA application on your system. I generated documentation for Adium, my chat client of choice. Five minutes later I had connected Adium’s status message to my Subversion repository’s trunk revision number. Here’s the code (with repository randomized):
require ‘rubygems’
require ‘rbosa’
require ‘rexml/document’
app = OSA.app(‘Adium’)
doc = REXML::Document.new(`svn—xml info https://fake.near-time.subversion-server.com/svn/near-time/trunk`)
revision = doc.root.elements["//commit"].attributes[‘revision’]
app.adium_controller.my_status_message = "Current Near-Time Revision: #{revision}"
How’s that for return on lines of code? Ruby and OS X are amazing!
I just moved up to a new Core 2 Duo MacBook Pro from a PowerBook. These new machines are insanely great. Here’s a snapshot from Photo Booth:

Weirdness in assert_redirected_to
by Blake Watters.
Posted in Software Development, Near-Time. Tagged with rails.
Earlier today I began having an infuriating problem coming from assert_redirected_to. The tests were failing an error like the following:
test_index_user_not_login(Admin::ConfigurationControllerTest) [./test/ functional/admin/configuration_controller_test.rb:20]: response is not a redirection to all of the options supplied (redirection is <{:action=>"login", :controller=>"user"}>), difference: <{}>
Hrm I said. Difference is {} ? Shouldn’t that mean that there is no difference? I hit the web and found some references to this sort of problem, mostly involving absolute controller paths. I don’t use absolute controller paths very often, but tried the fix anyway. No dice. So I rolled up my sleeves and set up some breakpoints and got to debugging. The problem was in the following portion of assert_redirected_to:
assert_block(msg) do if options.is_a?(Symbol) @response.redirected_to options else options.keys.all? do |k| if k :controller then options[k] ActionController::Routing.controller_relative_to(@response.redirected_to[k], @controller.class.controller_path) else options[k] (@response.redirected_to[k].respond_to?(:to_param) ? @response.redirected_to[k].to_param : @response.redirected_to[k] unless @response.redirected_to[k].nil?) end end end
end
The error was being raised in options.keys.all? block. After breakpoint hackery, I found that there was a mismatch between :id => 1 and :id => ‘1’. The Fixnum was being coerced to a string by the to_param method in this line:
else options[k] == (@response.redirected_to[k].respond_to?(:to_param) ? @response.redirected_to[k].to_param : @response.redirected_to[k] unless @response.redirected_to[k].nil?)
So I have added a modified version of assert_redirected_to to my test_helper.rb file that coerces both the redirect and asserted parameters via to_param before comparing them. This made my problem go away. So here’s the newly modified method:
def assert_redirected_to(options = {}, message=nil) clean_backtrace do assert_response(:redirect, message)
if options.is_a?(String)
msg = build_message(message, "expected a redirect to <?>, found one to <?>", options, @response.redirect_url)
url_regexp = %r{^(\w+://.?(/|$|\?))(.)$}
eurl, epath, url, path = [options, @response.redirect_url].collect do |url|
u, p = (url_regexp =~ url) ? [$1, $3] : nil, url == '/') ? p : '/' + p]
end.flatten
end
end
assert_equal(eurl, url, msg) if eurl && url
assert_equal(epath, path, msg) if epath && path
else
@response_diff = options.diff(@response.redirected_to) if options.is_a?(Hash) && @response.redirected_to.is_a?(Hash)
msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)#{', difference: <?>' if @response_diff}",
@response.redirected_to || @response.redirect_url, @response_diff)
assert_block(msg) do
if options.is_a?(Symbol)
@response.redirected_to == options
else
options.keys.all? do |k|
value = (options[k].respond_to?(:to_param) ? options[k].to_param : options[k])
if k :controller then value ActionController::Routing.controller_relative_to(@response.redirected_to[k], @controller.class.controller_path)
else value == (@response.redirected_to[k].respond_to?(:to_param) ? @response.redirected_to[k].to_param : @response.redirected_to[k] unless @response.redirected_to[k].nil?)
end
end
end
end
end
Disabling Observers in Tests
by Blake Watters.
Posted in Software Development, Near-Time. Tagged with rails, ruby.
At Near-Time, our models have recently suffered an onslaught of observers that handle all sorts of wonderful stuff. Sometimes, however, those wonderful Observers can start to get in the way of my testing efforts by introducing all sorts of external dependencies and behavior that is quite outside of what I want to test. So tonight I took up the cause of writing a simple test helper that is capable of disabling a given set of Observers for the duration of a test. Here’s the code:
def disable_observers(observers)
@observers ||= []
observers.each do |observer|
next if @observers.include?(observer)
instance_methods = {}
[:after_create, :after_destroy, :after_save, :after_update, :after_validation, :after_validation_on_create,
:after_validation_on_update, :before_create, :before_destroy, :before_save, :before_update, :before_validation,
:before_validation_on_create, :before_validation_on_update].each do |callback|
next unless observer.send(:method_defined?, callback)
method = observer.send(:instance_method, callback)
instance_methods[callback] = method
observer.send(:remove_method, callback)
observer.send(:define_method, callback) { true }
end
@observers << [observer => instance_methods]
end
end
def restore_observers
return true unless @observers
@observers.each do |observers_info|
observers_info.each do |info|
observer = info.keys.first
instance_methods_info = info.values.first
instance_methods_info.each_pair do |callback, method|
observer.send(:remove_method, callback)
observer.send(:define_method, callback, method)
end
end
end
@observers = nil
end
Usage:
class FooTest < Test::Unit::TestCase def setup disable_observers(SomeObserver, EntityObserver) end
def teardown
restore_observers
end
end
UPDATE*: I missed some callbacks methods in my original post. The code now contains all available callbacks.
Skipping Active Record Callbacks
by Blake Watters.
Posted in Software Development. Tagged with rails.
The following code will allow you to skip an ActiveRecord callback for the duration of the block. This can be useful if you are, for instance, building a model security layer and need to sidestep your enforcement callbacks temporarily.
def skip_callback(callback, &block)
method = instance_method(callback)
remove_method(callback) if respond_to?(callback)
define_method(callback) { true }
yield
remove_method(callback)
define_method(callback, method)
end
Usage:
class User < ActiveRecord::Base
before_save :raise_error
def raise_error
raise 'You wont see me!'
end
end
User.skip_callback(:raise_error) do
user.save!
end

RSS
Comments








