Posts Tagged ‘DBI’

Adapter specific date handling in Rails

Thursday, May 15th, 2008

Lately I’ve had to endure working on a Rails project where the requirements dictate the use of Microsoft SQL Server as the choice database. In addition, we’ve had to test environments consisting of Microsoft Windows/Apache 2.2/Mongrel and Red Hat/Apache 2.2/Mongrel with various adapter(odbc, oledb) and tabular data stream translators. When testing several operating systems, adapters, manipulators, etc there were inevitable issues between the different setups. One of the most common issues was the adapter specific handling of dates. Depending on the adapter’s TDS(tabular data stream), dates can be returned as strings or date objects. This issue made our project less flexible as we were having to modify view code here and there. To solve the issue we extended the rails core and created a DBI override class in the lib directory of our project.

To do this, add a core_ext folder inside of your project’s lib directory. Next, add a ruby file called core_ext.rb; contents shown below.

#Include each of the files in the core_ext directory
Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].each{ |file| require(file) }

Once your core_ext folder and file are setup in lib you’ll need to tell the environment.rb file to include your core extenders. Append the following line to your environment.rb file.

require 'core_ext'

Once our extensions are visible in the environment we can add our date time handler for dates where DBI is the project adapter. Add a file called DBI.rb in the core_ext directory. Contents shown below.

require "date"
module DBI
  class Date
      def to_friendly_str(options = {})
       t = self.to_time
       t.to_friendly_str(options) rescue self
      end

      def strftime(options = {})
        t = self.to_time
        t.strftime(options) rescue self
      end
   end
end

 

You’ll notice a non-standard method called “to_friendly_str”. You can replace this with stftime or create a custom date string handler like we did. I’ve added to_friendly_str below if you want to add it to the core_ext directory in addition to the DBI module or you can place it in your application controller. Enjoy!

def to_friendly_str(options = {})
# Attempt to init a new time object
# from this string then call
# to friendly_str on it.
    t = Time.parse(self)
    t.to_friendly_str(options) rescue self
end

FreeTDS Compiler for Mac

Sunday, February 10th, 2008

Ok, so this is my first post of the year…..deal with it. Not much to report right now. I’m almost ready to switch my blog over to a custom Rails CMS in the near future and I’m gearing up this week to do some training on the latest version of Rails, fun. I always like walking people through a method line by line only to find they weren’t paying attention because they’re too busy being right. That piss anyone else off? I do love REST integration in form objects in 2.0.Yesterday I started compiling a cocoa installer for FreeTDS on Leopard. I’m tired of configuring developer’s machines by hand whenever they need to hit an MS Sql box from their MacBook, so that’s my motivation….and it’d been pretty nice to have. I’ll post to Google Code as soon as it’s tested. With the current work load it’ll probably end up being June.That’s really all for now. Trying to get a major enterprise Rails app out the door this month so it’s likely I won’t have any updates until March or April.