Posts tagged grand rapids wifi

Rails 2.3 and Grand Rapids WiFi

I like to always have at least one project on the go that works as a testbed for a variety of new Rails techniques, tools and releases. For a long time that project was Grand Rapids WiFi, a site that lapsed into some neglect after I moved (a long way) away from the town it focussed on, but which I still officially maintain. It was where I first tried Rails 1.1 and 1.2, where I got to grips with various geo tools, and where I first generated RDF and Atom from a Rails app.

With this morning’s announcement that there’s a release candidate of Rails 2.3 out, I decided the time was ripe to dust off the GR WiFi code and move it from Rails 1.2.x to 2.3.0. It turned out to be a nice quick process, even allowing for the time it took to move it all onto github.

  • Tidy up environment.rb to remove some deprecated features, and specify the new gem version
  • Run rake rails:update to rename application.rb to application_controller.rb
  • Remove quite a few defunct plugins that we didn’t need anyway
  • Replace a couple of plugins with relevant calls to config.gem
  • Rip out the use of components (what was I thinking?) and add a new model to help with sidebars
  • Tidy up a few old paths
  • Update my deploy script to get the code from git rather than svn

This site was where I really learned my way around rails’ test/unit-based testing framework and I’m sad to say that the tests now lie rather neglected. If time allows, I’ll kick them back into shape so they can be used to uncover any dark corners of the code that haven’t survived the transition, but for now I just wanted to get the site upgraded quickly and hopefully running a bit more efficiently.

For those who’d like to play with the code, it’s all public now over on github. I make no promises as to the quality of the code, but feel free to take it, use it, path it, etc. If you do end up using it for anything, please do let me know.

Auto Center and Zoom with YM4R

When I blogged last month about abstracting mapping with YM4R I commented:

What I’ve not yet discovered (and may not be implemented) is a way to automatically center and zoom a map. It would be very nice to be able to add a batch of points to a map and have the plugin automatically work out their mid-point. Maybe I need to work on that a little…

What I was missing was the center_zoom_on_bounds_init method. There are various ways to interact with it, but I’ve chosen:

sorted_latitudes = locations.collect(&:latitude).compact.sort
sorted_longitudes = locations.collect(&:longitude).compact.sort
@map.center_zoom_on_bounds_init([
  [sorted_latitudes.first, sorted_longitudes.first], 
  [sorted_latitudes.last, sorted_longitudes.last]])

That mostly replaces the call to

@map.center_zoom_init([latitude, longitude], 14)

that I was previously using, though I’ve actually kept that around if there’s only one point as I was finding Google Maps’ maximum zoom didn’t show quite enough context for my tastes.

While it would be nice if the plugin had a method that performed the calculations based on the points you’ve fed in, it looks like the current implementation stores the points as javascript strings, so that’s not really an option. But with only three lines of code involved, this is a nice simple way to get the desired effect.

Abstracting mapping with YM4R

When Grand Rapids WiFi–then a PHP application–first implemented google map support all the logic for producing those maps came in the templates. The controller passed the view a list of locations for that page, and it wrote out a series of javascript calls that produced the map. It was a little unreliable and very clunky, but it worked. When I moved the app over to rails, that was one aspect that I didn’t change, and it continued to work.

With more and more talk lately of google and lock-in, and some desire to begin to do more with the maps (perhaps invoking the ajax buzzword) I realised that I needed better tools and cleaner abstraction to work with the map. And it just so happened that YM4R_Mapstraction appeared at just the right time.

YM4R_Mapstraction is a rails plugin that provides methods to build map objects in your controller and helpers that will turn that into a map in the view. Controller code looks like:

@map = Mapstraction.new('map', :google)
@map.control_init(:small => true)
@map.center_zoom_init([location.latitude, location.longitude], 11)
@map.marker_init(Marker.new([location.latitude, location.longitude], 
    :label => location.name, :info_bubble => location.description))

and view code would be:

< %= Mapstraction.header(:google) %>
< %= @map.to_html %>

in the header, and

< %= @map.div(:width => 300, :height => 300) %>

where you want your map to appear in the page.

To switch from a google map to a yahoo map, you simply need to replace ‘:google’ with ‘:yahoo’ in the examples above, opening up the possibility of automatically checking for server availability, or of allowing users to choose their preferred map.

One thing I found frustrating was simply having text for the description in the info bubble. That was easily solved by creating a partial called _bubble.rhtml and changing the marker code to:

@map.marker_init(Marker.new([location.latitude, location.longitude], 
    :label => location.name, 
    :info_bubble => render_to_string(:partial => 'bubble')))

So far, using the partials doesn’t seem to slow things down too much, but eventually I hope to cache the partials to reduce any overhead that might introduce.

What I’ve not yet discovered (and may not be implemented) is a way to automatically center and zoom a map. It would be very nice to be able to add a batch of points to a map and have the plugin automatically work out their mid-point. Maybe I need to work on that a little…

Grand Rapids WiFi on Rails

Grand Rapids WiFi relaunched today with a change under the hood to Ruby Rails. The site has been through several iterations since I took it over in September 2004. It often functions as my testbed for new features I’d like to trial, and since most of my custom development is now rails-based, it made sense to make the switch.

Feature-wise, not much has changed. A few URLs have changed slightly (with appropriate redirects provided by rails and lighttpd), there’s some caching in place, and there are a few new “ajax” effects, but otherwise it’s so far a straight port. And on the UI side the changes are also minimal. The use of microformats has increased somewhat, but the long promised redesign will have to wait a little longer.

What the port should allow is for more rapid iterations. The old system was pretty maintainable for a PHP app, but the new codebase is much cleaner. Over the coming months I’ll be doing some refactoring to clean it up further, perhaps taking some steps to combat the spambots that have found the site recently, and if certain parties are still interested, deploying the code for other communities to make use of. Hopefully that redesign will come along too…