Category / Ruby on Rails

Ruby modules are awesome January 12, 2009 at 5:00 am

If you read my previous blog, well. I told you this was a roller coaster! I’m starting to feel manic.

Ruby, on the other hand, is pretty solid, well documented, and cool.

After fighting with stupid gems all day, I decided to just let people include their own with a simple wrapper. So I started playing with modules.

Modules really rock. They are so freakin simple it’s unbelievable. Modules are sort of like namespaces but you can build upon already loaded ones. Basically, what this means to me is that I can have a base/core module that loads all the files in some module directory, and then figures out dynamically what’s been loaded.

Here’s a practical example (from the previous blog).

Let’s say we want to be able to let a system administrator installing our software decide where users authenticate from. The avilable options *we* thought of were Active Directory, Local Database and Linux PAM. But the reality is not everyone will need all of these options, but someone might need two of them (like local DB and active directory – such that when A.D. is down you can still get into your machine). How do we do this?

First, the individual modules would look something like this:

module MyAuthFramework
  module AuthViaLDAP
    def Login
    # Do login validation here, possibly through a gem
    end
  end
end

You’d save that to a folder somewhere, along with maybe another module, like this:

module MyAuthFramework
  module AuthViaPAM
    def Login
    # Do login validation here, possibly through a gem
    end
  end
end

Note that the two modules share the same method name and base module, but the module namespace in the middle is different. Loading both the above files from the same Ruby script effectively mixes them together, creating this:

module MyAuthFramework
  module AuthViaPAM
    def Login
    # Do login validation here, possibly through a gem
    end
  end

  module AuthViaLDAP
    def Login
    # Do login validation here, possibly through a gem
    end
  end
end

Now, in a base file somewhere, we can use a nifty constants method built into modules to accurately see what’s loaded and cycle through each class, calling it’s login function with some credentials we received. Whoever returns a success could be declared the winner!

module MyAuthFramework
  def Login
    MyAuthFramework.constants.each do |modulename|   # Cycle through all modules
      mod = Object.const_get(modulename)                    # Instantiate the module
      mod.Login()    # Call the method in each module
  end
end

This routine will effectively call the Login() methods in both the included modules, LDAP and PAM.

cool, huh?

Check out the docs for more goodness.

Why Ruby on Rails frustrates me… January 11, 2009 at 11:00 pm

So I am now in week 4 of trying to switch to Ruby on Rails from CakePHP. It is truly a roller coaster ride.

On the upside, I have been very, very impressed with the true object-oriented nature of Ruby. Really, I can’t say enough here. The fact that you can override and extend pretty much anything in the language to your liking is just awesome. Everything is an object – just like Java or Javascript – but without the annoyances of an overwhelming required number of definitions or memory concerns. You can even overload symbols and other operators. The built-in introspection and modularity is just slick.

On the downside, I am continuing to have trouble taking advantage of this “goodness” so many speak of with Rails. I don’t think it’s because of lack of trying. I think it’s due to lack of good documentation.

As a practical example, let’s say I want to do something as simple as create a login generator that can integrate with LDAP as well as a local database. This is a practical scenario I’ve run into in the PHP world – I want to have Windows users use the same login/password on an intranet site as they do for their Windows credentials. But I also want a fallback mechanism so I can login when LDAP is broken, or when I need to create a special account for, say, a contractor who only needs temporary access and should not be allowed onto the Active Directory network. In PHP, you simply go to www.php.net/ldap for the LDAP pieces, or maybe do a search for a PECL library that handles LDAP for you. Then you cobble together a quick model, view and controller using CakePHP’s scaffolding and get the login and logout stuff done. Or you extend the CakePHP authentication modules that are well documented right within the CakePHP manual. Probably about two hours of work.

Now let’s apply this to Rails. Not knowing where to start, and not having a search box on the RubyonRails API page (there isn’t one – how silly), I try some Google-fu to find the equivalent in rails. The first relevant hit is a page that seemed like a match – http://wiki.rubyonrails.org/rails/pages/Authentication . An authentication wiki page on Rails own site. Seems legit.

But then I load the page and the first thing I am greeted with is:

“This article is part of the confusing world of Authentication in Rails. Feel free to help: AuthenticationNeedsHelp.”

ehh

Then I start examining the list of available plug-ins, gems and solutions to authentication that people have listed. Almost all of them are labeled either deprecated, incomplete or “good for beginning Ruby on Rails user.” Fine, I think, maybe one will work and I just have to find which one. So I start clicking into each page.

LoginGenerator seems relevant.

But scrolling through that page, the text and comments suggest that it no longer works for versions past 2.0.2. But don’t worry – it links to ANOTHER site that swears to be the real solution I am looking for! That’s here – http://wiki.rubyonrails.com/rails/pages/Acts_as_authenticated .

Woohoo! This page starts by proclaiming “Yay! (read why)” and then explains that THIS is the right place for an authentication system generator. As if the page already knew that all those OTHER pages one might stumble upon prior were total crap. But before I get too excited, I click on the link which states that “…you really want to see the official Acts As Authenticated Github.” So I bite – I click the link, and am whisked away to the project page, which states right at the top:

“Please note that acts_as_authenticated, is no longer developed/maintained”

WTF?!?!

So we go back to the drawing board – all the way back to the Wiki page we started with – and look for more. Restful_authentication seems to be the top item, so maybe I should have started there. Again, I click it, and the first comment mentions that some material links to the wrong source. ugh. It then lists four locations to get information. I start with the first one – the official plug-in homepage. It says it’s for rails 1.2. But I’m on Rails 2.2.2. [Sigh] Do I try it? Or go back to the drawing board?

Maybe I am missing something, but one strong part of PHP was the manual and the comments – the manual matched the methods and classes that were actually available almost 98% of the time, and were never incomplete in terms of broken. Does such a resource exist for Rails where I can go to a webpage to find plug-ins and they reliably work and are available? This has to be my #1 frustration with Rails at this point.

If you have comments, please add them. I’d love to hear solutions to how to better manage rails plug-ins, gems and other “goodies” that seem to just be scattered everywhere.

Ruby on Rails installation on CentOS 5.2 – zlib and other errors January 6, 2009 at 9:59 pm

I thought it would be good to share my experience with installing Ruby on Rails w/ CentOS 5.2.

It didn’t get off to a very good start, namely because the person before me had already tried to install rails unsuccessfully. The issue appears to stem from the configure mechanism in Ruby which links against zlib libraries that may or may not be correct. Undoing this was non-trivial.

Specifically I was receiving this error when doing anything with gem:

/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require': no such file to load -- zlib (LoadError)

Installing zlib and zlib-devel libraries on CentOS did not fix the problem because the linking had already occurred back in the ruby build (I think, anyway). I decided the easiest thing was to do a “rip and replace” fix by ripping out all of ruby and the associate gem software and re-installing. I did this as follows:

1. Remove all packages that were installed via yum

rpm -qa | grep ruby
yum remove [insert results from output above here]

2. Go look for remnants that may have been installed by gem or by a manual compile of ruby and remove them, too. A few places I had to look:

rm -rf /usr/local/lib/ruby
rm -rf /usr/lib/ruby
rm -f /usr/local/bin/ruby
rm -f /usr/bin/ruby
rm -f /usr/local/bin/irb
rm -f /usr/bin/irb
rm -f /usr/local/bin/gem
rm -f /usr/bin/gem

3. Download the latest ruby source and rubygems source to /usr/local/src/ and extract them. At the time of this writing, those commands were:

cd /usr/local/src
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz
tar xzf ruby-1.8.7-p72.tar.gz
wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz
tar xzf rubygems-1.3.1.tgz

4. Go into the Ruby directory and compile it, like so:
cd ruby-1.8.7-p72
./configure
make

NOTE: This was the key part to watch. This time around, you should see compile messages stating that zlib was compiled successfully at the end of the log on your screen.

5. Now install Ruby, if all went well:

make install

6. Now go setup RubyGems, like so:

cd ../rubygems-1.3.1
ruby setup.rb

7. Update the gem system for good measure

gem update --system

7. Install Rails

gem install rails

Presto, a nice clean CentOS 5.2 install.