
(L to R) Mike, Yukihiro Matsumoto (Japan), John Anderson (South Africa), Daya Sharma (Houston). Photo by Nathan Price (Houston)
Last week, I attended the 2nd annual Lone Star Ruby Conference here in Austin. Some of the highlights of the weekend include:
- thoughtbot’s Rails Best Practices. I was fortunate to attend a full-day training course given by thoughtbot’s talented team. Their hands-on approach really helped me improve my testing skills as I have migrated most of the unit tests from my latest project from Test::Unit to shoulda.
- James Edward Gray II is quite an impressive technologist. Throughout the weekend, I enjoyed listening to his nuggets of knowledge and experience about some of the broader aspects of Ruby programming.
- Glenn Vanderburg’s high-level talk on design principles will stay in my mind as I continue to improve my programming skills. He explained how some people just have a natural aptitude to become great programmers, others develop the skill through lots of hard work, and the rest will never have the skill or the interest in the profession. I’d like to think that I’m in the second group.
- I appreciated Yehuda Katz’s demonstration of Merb. Even though I’ve sat in on a few of Ben Burkert’s demonstrations of the framework, I’m finally motivated to try it out for a couple of new projects, including updating this blog.
- The excellence of the work and planning delivered by Jim Freeze and his team at the Lone Star Ruby Foundation cannot be stressed enough.
- Of course, there was nothing more enjoyable than spending time with Ruby developers who made their way to Austin from three continents. How exciting it was to start up spontaneous conversations with some of the most well-known Rubyists like Dave Thomas, fellow Austinites Hal Fulton and Bruce Tate, and Yukihiro Matsumoto himself.
Today I submitted an enhancement ticket for the Rails ActionView::Helpers::ScriptaculousHelper module. This version of drop_receiving_element_js adds two additional options, beforeAjax and afterAjax, that provide a means for javascript code to be executed before and after the creation of the Ajax.Request object. This would allow greater flexibility than just having to create an onDrop function from scratch when needing to do more than making a remote request from within the function. The Rails API doesn’t actually include this method, instead, it’s called from the drop_receiving_element method. You can track the ticket on Lighthouse.
If you’d like to use this enhanced version immediately, create a file named scriptaculous_helper.rb to be placed in config/initializers (for Rails 2.x) or lib (for older versions of Rails). Save the code below into the file, restart your app, and you should be good to go.
toggle line numbers
- require 'action_view/helpers/javascript_helper'
-
- module ActionView
- module Helpers
- module ScriptaculousHelper
- def drop_receiving_element_js(element_id, options = {}) #:nodoc:
- options[:with] ||= "'id=' + encodeURIComponent(element.id)"
- beforeAjax = options[:beforeAjax] ? options[:beforeAjax] + ';' : ''
- afterAjax = options[:afterAjax] ? options[:afterAjax] + ';' : ''
- options.delete(:beforeAjax)
- options.delete(:afterAjax)
- options[:onDrop] ||= "function(draggable,droppable,event){" + beforeAjax + remote_function(options) + ';' + afterAjax + "}"
- options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) }
-
- options[:accept] = array_or_string_for_javascript(options[:accept]) if options[:accept]
- options[:hoverclass] = "'#{options[:hoverclass]}'" if options[:hoverclass]
-
- # Confirmation happens during the onDrop callback, so it can be removed from the options
- options.delete(:confirm) if options[:confirm]
-
- %(Droppables.add(#{element_id.to_json}, #{options_for_javascript(options)});)
- end
- end
- end
- end
Here’s an example call from one of my apps that I used to swap the order of an HTML element and make a remote call to change the affected objects’ attributes on the back end:
toggle line numbers
- drop_receiving_element("field#{@field.id}", :url => field_path(@field),
- :method => :put, :accept => 'fields', :hoverclass => 'droppable_field',
- :beforeAjax => 'droppable.parentNode.insertBefore(draggable,droppable);')
Today I wanted to update my unit test to use instantiated fixtures, and I found this regular expression useful for replacing the non-instantiated fixture syntax fixture(:name) with the instantiated syntax @name.
I used the following pattern to find references to the non-instantiated fixtures:
\w+\(:(\w+)\)
I used the following pattern to replace each match with an instantiated fixture.
@$1