HomeRamblings  ⁄  ProgrammingRuby Language

Ruby Language

Making Commitments, Reaching Out

In a very real sense, today is monumental for me. Today, I committed developer resources (not just my own) to my upcoming mobile app. I don't quite have the specs together for the developer, but I have enough now that I can get him started on building some of the foundational components of the app. We're making room in our collective schedule to get this project formally under way. So far, I have built the user management component of the system that allows a new user to register an account and log into the system. We have a little more work to do on the user onboarding experience, but we've got enough that a solid API scaffold is in place in the budding app and I can start assigning user stories to the developer. I'm… read more


Dynamic Routing in Rails Revisited

Ever since my original post [Creating Dynamic Routes at runtime in Rails 4](/ramblings/creating-dynamic-routes-at-runtime-in-rails-4), I have been digging into Rails routing more and more. I've gotten a lot of feedback on the post itself as well as comments in the IRC #rubyonrails channel with an over abundance of passionate pleas not to do routing this way in Rails. But it seems a lot of people desire do to it this way and I have even been inspired to put a gem together to do it. A few days ago, I introduced a refactored version at 0.1.1 and then two days later, a radically refactored version at 0.2.2. Let's talk about this gem and why it's upsetting some Rails purists. This is a long post and describes a bit of my journey from a good solution to a slightly… read more


Creating Dynamic Routes at runtime in Rails 4

I was working on a fairly simple site that has a content management component to it and wanted to find a way to dynamically generate the routes to the pages that were managed on the back-end by the site authors. This article presents an alternative to the usual approach of creating a catch-all route in the routes.rb by generating specific routes to each and every page. ## Preamble Sometimes, its faster to re-invent the wheel than to figure out thousands of lines of other people's code. Rails is great in that way -- you can look at the ecosystem and find lots of great pre-existing solutions or you can roll your own in very short order when the requirements are clear and precise. Such is the situation I found myself in while trying to get one of the… read more


Gems you should consider for every Rails projects

Ruby on Rails provides a lot of great functionality out of box, but there are a few gems I almost always add to my new Rails projects. Here are some of those gems I practically cannot live without. ## HAML Templates ~~~ text gem 'haml' ~~~ I know many developers who say "HTML is a perfectly good DSL for HTML" and I used to follow in this mantra myself until one day I had to work on a new Rails project that already had HAML in use. There's a lot to be said for "less is more" when the clutter of punctuation and inconsistently indented code is replaced by the stripped down, well-indented code. When you inherit a ERB-based project that is full of conditional flow and Ruby code embedded in the view files, you really start to appreciate the… read more


Making Ruby talk to MSDE

Getting Ruby to talk to Microsoft SQL Server 2005 is one thing. Getting Ruby to talk to Microsoft SQL Server Developer Edition 2000, has one twist that threw me off for hours. Here, I'll show you how to do and hopefully save you lots of frustration and brain damage from banging your head against the wall. I won't cover a complete Ruby and Rails/Ramaze/Sinatra installation. This post assumes you have that much down pat, so we'll pick up the ball with installing FreeTDS, unixODBC, Ruby ODBC If you're using Ubuntu 8.10, you can get away with installing everything from packages with the following: ~~~ bash apt-get install unixodbc freetds-common tdsodbc \ libodbc-ruby1.8 \ libdbi-ruby1.8 libdbd-odbc-ruby \ autoconf make ~~~ **NOTE: I wrote this article and right before publishing, attempted to replicate these results on both mac OS X and CentOS… read more


Implementing Ruby jobs in the background

I needed a way to kick off a background job that was triggered by an end-user on my Ramaze-backed website and could run for almost two hours! This article brings together all the elements to get the job done. ## Preamble (a.k.a. Ramblings) I can hear you exclaiming, "Two hours!? Are you nuts? You really don't want to do that on a web application," and you would be correct; I did not. But alas, my users wanted it and they waved money in front of my face to get it done. What's a poor sap like me to do but to comply? So, what is this crazy task that needs to run so urgently it couldn't wait until a midnight cronjob task to kick it off? Well, the task is basically exporting data from the live production database to a… read more


What makes a top award candidate, anyhow?

In our local Linux user group mailing list, one of the participant asked the group for recommendations for open source software projects that should be considered for a prominent publication's top 100 awards. The ensuing discussion got me to wondering: What is the purpose behind doing the awards? Most of what I see talked about are all well-known and well-used, such as git, mercurial, word press, joomla, django, and so on. Usually, when I look at a list like this, I'm looking to uncover new and undiscovered projects that have real potential to make a real impact in my daily life and thus was somewhat surprised that such mature projects are being so heavily promoted in the discussions. Its not that I don't think these tools are worthy contenders. Most of them, I have used fairly extensively myself over the… read more


One ActiveRecord Model Acting as a List and Tree

Occasionally, Rails can appear to make your life extremely easy while silently throwing you a curve-ball. I needed a model that required a hierarchy while also preserving order of the records. Although fairly straightforward to set up and start immediately using, there are a couple of "gotchas" to watch out for and this article covers those pitfalls and shows how to apply the cool new "dirty attributes" feature in ActiveRecord. ## The Problem I am working on a content management system (CMS) where I want the pages to have a hierarchical structure that turns into a menu with sub-menus. The content manager needs to also be able to order these pages so that the menu structure renders in the desired order. ## The Solution Two plugins jumped to mind almost immediately: **acts_as_tree** and **acts_as_list**. The tree plugin will manage the… read more


Ramaze and ActiveRecord

Thanks to the great folks on IRC over at Freenode#ramaze, I was able to not only get Ramaze working with Erubis and Activerecord for a pseudo apples to apples comparison of Rails vs. Ramaze performance benching, but I was also able to get Apache 2.2.9 talking to Passenger to Ramaze. This quick post will take you through the basics. ## Environment This post will guide you through setting up the following environment on Ubuntu Intrepid (8.10): * [Apache 2.2.9][1] * [Passenger 2.0.6][2] * [ActiveRecord 2.2.2][3] * [Ramaze 2009.01][4] * [Erubis 2.6.2][5] * [MySql 5.0.67 (assumed installed)][6] To get started: ~~~ bash sudo apt-get install apache2 passenger sudo passenger-install-apache2-module ~~~ Install the Rack, Ramaze, Activerecord, and Erubis gems ~~~ bash sudo gem install rack, ramaze, activerecord, erubis ~~~ ## Establishing Project Now create your first ramaze project with: ~~~ bash ramaze… read more


Initial release of Elemental

I am happy to announce the initial release of Elemental 0.1.1. Elemental provides enumerated collection of elements that allow you to associate ruby symbols to arbitrary "display" values, thus allowing your code to think symbolically and unambiguously while giving you the means to easily display what end-users need to see. Additionally, symbols are associated with ordinal values, allowing easy storage/retrieval to persistent stores (databases, files, marshalling, etc) by saving theĀ  Element#value as appropriate (String by default, Fixnum if you "persist_ordinally"). The primary aim of Elemental is to collect and abstract literals away from your code logic. There's an old programmer's wisdom that you should not encode your logic using literal values, especially those the end-user is exposed to. Complete details provided in README.txt http://github.com/mwlang/elemental/tree/master http://rubyforge.org/projects/elemental/ From Gem: ~~~ bash sudo gem install elemental ~~~ From Source: ~~~ bash gem install… read more


Rails has and belongs to many (habtm) demystified

Every time I have to implement a many-to-many relationship between Rails models, I seem to have to figure out how to do it effectively all over again. Especially as Rails seems to evolve the relational hooks with better support and elegance. Here, I will show a has_and_belongs_to_many strategy that works well for me. Along the way, I'll expose a few other minor tricks, such as adding a custom inflector for pluralizing your model or not adding the ID column on a table declaration. The following Browser Edit form is what we're going for. That is, having a list of Operating Systems to check off while editing a Browser object: ![Editing Browser View][1] First, the models: What I wanted, was a way to declare browsers (Firefox, Explorer, Opera, etc.) and associate them with one or more operating systems (OS X, Windows,… read more


Converting Oddmuse Wiki to Edgewall Trac

Our company began long ago with wiki's, but we chose the Oddmuse wiki way back when. These days, we're heavy users of Trac wiki because of its integrated ticket support system. So what to do with all those old wiki's that folks have stopped using and reading. The Oddmuse wikis still hold valuable data, but they have since become an administrative overhead to keep around, so I decided to convert them all to Edgewall's Trac. To get started, I needed to know how to get the data into Trac. The following links gave me an API I could utilize to create trac pages and content: * [Trac Data Models][1] * [The Data model Python code][2] Great! Except there's one small problem. I don't really know python all that well, even though I did do a bit of Zope development, oh,… read more


Geocoded Zipcodes

You would think that loading up a database of zip codes would be an extremely simple case of finding a public database on www.usps.gov, and then loading up with a database bulk load. It turns out that, while the USPS does offer products for you to purchase, and a rather nice lookup interface for looking up zip codes and so on, there's not really any free data to be had that I could tell. So the hunt was on to find some zip code data, preferably geocoded (latitude/longitude for GIS) zip codes and then load them into MySQL. A bit of hunting around turned up [this list of zip code databases][1]. The first link to CivicSpace database appears to be defunct (I got a Go Daddy "this page is parked" page). So, the next free one, offered by [Popular Data][2]… read more


DRYing your Views

Let me start out by saying that I am finally beginning to understand a bit about that magical Ruby block notion and how implementing methods through block passing can really empower you as a Ruby developer. Thanks to, a most excellent [Ruby tutorial][1], I am definitely feeling a good bit more empowered about getting my Views in Rails all DRY'd up. The problem: I wanted to introduce a property editor metaphor for the website I'm working on where properties could expose edit forms for just about any situation, much like wordpress' widget interface does. That is, there's an area of the current page that shows basic information about the property in a read-only (and preferably compact) form, and user could click an "edit" button and exposed an editable version of the properties. The "edit" link becomes a "cancel" link, which,… read more


A model-free wizard

Maybe I'm taking the whole MVC thing too far, but I've been reading and learning both Ruby and Rails at a fairly fast clip and just when I thought I was getting the hang of what goes in models, views, and controllers respectively, along comes The Advanced Recipe for Rails book with a recipe for implementing a wizard that threw me for a loop here. This implementation just so happens to be based off acts_as_state_machine, which plugs into your model classes. And of course, the acts_as_wizard plugin wasn't too far behind! Well, asking myself whether the wizard logic *should* be allowed to bleed over into the model prompted me to see just how hard it was to build a wizard in a web interface anyhow (this is my first attempt at a multi-step workflow bit on the web). Most of… read more