Thursday, December 6, 2007

I'm bit by the bug

Okay, RoR is pretty cool. In about a couple of hours I have secured my login and activation. I found all of the places to customize and have done so, and I put in some pretty colors. It's amazing because when I was working in WebObjects I thought about how much I wanted to abstract things. I worked hard to make the WO version be as flexible as possible so that changes only were needed in one location and those locations were logically organized. The original developer and those continuing to extend rails obviously think along the same lines. Here's what I've been using so far and like:

Flash. Flash is really cool. You use it to pass messages from the controller to the next page displayed. It works as a hash , so you associate a string with a key. One standard is to use :notice as a key for messages to the user, and :error for error messages. What makes this awesome is that I can place <%= flash[:notice] %> in my page and whatever message gets passed will pop right in there. Wrapping a css class and span around that lets me control the format of these messages on every page. I set this up on my login page for the standard "username/password incorrect" message. Later, when I locked down the user's home page and sent a redirect to login, "you must be logged in to do that" showed up in the same spot, looking exactly the same! I think this is part of the fun that RoR gives a programmer. One of the best feelings of programming is when you what something to happen, you code something to happen, and it does. I know that sounds stupid, but ask a programmer and he will agree. What is even cooler, is when you want something to happen, you don't code it to happen, and it does anyway!

Another thing I like is the inheritance hierarchy. RoR was designed for maximum flexibility, and the inheritance structure provides it. Placing items in the superclasses provides for everyone. You want it to be specific? Put it in the subclasses. My only worry is that this won't be so easy with Models. I will have to see.

I did run into a problem with something I've had an issue with all along. The separation of migrations and model files seems so un-Rails like. I added on to the User class to allow a boolean to hold email preferences. For some reason the boolean wasn't getting set from the form. Eventually, I realized that I also needed to add it to some code in the model file. This is stupid. I should only have to declare my columns and properties in one place. I do not think that the benefit of backtracking migrations outweighs the ease and intelligence of single location editing of models. They're models! What language requires you to define classes in two places (okay, leave C++ out of this)?

Authentication is just about finished for now. Down the road I am going to want roles, but for now a simple Admin boolean will suffice. I should program up some more functional and unit tests, but I'm too lazy and I want to start playing with some Avatars. Mmmm... Character Creation is on the menu!

Wednesday, December 5, 2007

You've got Authentication!

After working a bit with my user authentication/management system, I figured that someone has probably done this before, and done it better. In all likelihood, my system would be highly vulnerable to well-known attacks and never be secure enough nor cool enough. Plug-ins are a double-edged sword. They can be dangerous because the programmer doesn't learn anything by using them, causing them to become more ignorant about the language and technology, and thus requiring more reliance on more plugins. But they can also be helpful. They can save time when the task is something simple, and can provide a novice to the technology (like I am) the ability to see how things should be done. Also, an open-source approach is always more secure because hundreds or thousands of eyes have had a chance to look over things. This was what made me decide to seek some outside help for my authentication system.

At first I looked through the wiki, and found the page on authentication. Many different plugins and approaches are listed there. I first chose Act_as_authenticated since it included everything I wanted and seemed to be the most complete. When trying to install that, I found that it had been replaced by the author by a new version called Restful_authentication. And then I fell down the rabbit hole of RESTful.

RoR development is fun because it is so new. Someone can post an idea and 3 months later everyone is depending on it like God Herself decreed that it was the best way to program. There are mini fads within this fad of a programming technology! RESTful is one of those. Actually, I don't know if its a fad, because it makes sense. It deals with handling access by putting access to data behind a single URI and sorting out based on HTTP protocol. This makes sense. Really, it is pretty much what Rails should have been doing the whole time rather than using the long chain within the URI (and including the id number? Ewww!). I always had a problem with that.

Now I have my plugin, and I know why its cool, next step is to install and test it. I made a separate app for testing to make sure it all worked. I'm glad I did because there was a bunch of setup stuff required, and I feel better getting it right in my test app before putting it all in my app. Once I got everything figured out, I looked at my current version of V and noticed that really all it had was the user stuff that I was going to replace anyway, so I renamed that for historical reasons (future historians are going to love me) and made a new app. Here's what I did after that:
  1. Install the plugin.
    This one's easy. First link to the URL:
    script/plugin install
    Then install:
    script/plugin install restful_authentication

  2. Generate the model.
    Restful_authentication is a generator plug-in, so it works like scaffolding where you make one call and it builds lots of stuff.
    script/generate authenticated user sessions --include-activation
    The first parameter (authenticated) tells the generate script to use restful_authentication's generator. The second parameter is the name of the model and the the third is the controller name for handling log in and out. This call will create a new model called user that will be used to hold all of the user's data. It also creates controllers that handle signup, login, and logout as well as some minimal views for the functions. You also get functions to use as filters for testing the current authentication status. By adding --include-activation on the end, e-mail activation code is included so that the system can e-mail the user and have them activate their account through e-mail. I'm going to use this since I want to make it difficult for people to have lots of accounts, so I want to force each one to have a real e-mail address.

  3. Setup the routes.
    This was a little tricky, even though the docs did give me just about all of the info I need. Here's all of the things I added to routes.rb to get it working. Remember that these go before the map.connect lines:
    # for restful_authentication
    map.resources :users
    map.resource :session, :controller => 'sessions'
    map.signup '/signup', :controller => 'users', :action => 'new'
    map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate'
    map.login '/login', :controller => 'sessions', :action => 'new'
    map.logout '/logout', :controller => 'sessions', :action => 'destroy'

  4. Setup the environment.
    The activation mailer uses an observer to watch for the creation of a new user. Put this into environments.rb in the run method:
    # restful_authentication
    config.active_record.observers = :user_observer

    To send the e-mail, I want to use sendmail since I'm developing just on my mac, and don't happen to have an SMTP server setup. However, this might change when V goes into production, so I'm going to tell my ActionMailer to use sendmail only during development. I put this in environments/development.rb.
    # tell ActionMailer to use sendmail
    ActionMailer::Base.delivery_method = :sendmail

    God I love RoR!!!

  5. Give everyone access to the authentication methods.
    The user_controller and session_controller have AuthenticatedSystem included at the top of them with a note to put this in ApplicationController. This way, everybody gets access to the methods that test for authentication. So take those lines out of user_controller.rb and session_controller.rb and put this at the top of application.rb:
    # Authentication methods for restful_authentication
    include AuthenticatedSystem


That sets things up. Next, make a call to migrate to add the new db stuff and then start up the server and check out /signup. There is a lot more configuration and customization to be done. But it works! And it seems to be pretty robust.

I'm on a kick of extreme programming, so I want to have my authentication system complete before I move on. That means that customizing the interface is next!

Monday, December 3, 2007

V 6.0

And so I start over again... again.

It turns out that the WO stuff for Eclipse wasn't that great after all. The EOModeler replacement had its issues, the SQL code never worked, and I couldn't get Java classes generated from my EO Models. I was facing a re-write of all of my previous work, with a sub-par alternative to sub-sub-par tools, and on a less-than-lean system that I was going to have issues deploying.

With those issues looming, I looked at a change of technology... again.

And so V was reborn. This time it was red and shiny and had thick iron guides. Yes, V has jumped on another fad, this time to Ruby on Rails.

I did plenty of reading before deciding to change. Lack of documentation is one issue that the RoR community is trying to fix. Many of the intro docs go through quick set-ups and overview the creation of mini-projects to show off how quickly you can start working with rails. There are few docs that provide full details of what is going on behind the scenes, or that outline the full set of information. To learn RoR, you have to schlep through these tutorial guides until you learn enough so that you can tackle the API's. That's about all you can do.

I'm still not sure if RoR is the best fit for V. RoR is designed for more casual web applications. Ruby is a tougher language for business logic than Java is and the Rails tools are designed for more strict CRUD (Create, Read, Update, and Delete) access to data. V is a game. Users log in and fight monsters and spend gold on items. It's mostly business logic (yeah, the business of kicking monster ass) and data management isn't as big of a deal. However, there are some really great things that I get with RoR. While Java would be better for the logic programming, Ruby has some awesome OOP stuff including introspection, wacky overriding, and reflection. Unit testing and functionality testing is built-in. While it takes a while to get used to the rails directory and file structure, and you are forced a bit to do things the "rails way" (hence the appropriate title), it uses easy to read pure text files in a standard format. This is such a nice thing to have after WO.

After research, the next step was installation. The steps to install Ruby on Rails on OSX Leopard are as follows:


Did you catch that? It's already installed! Apple chose to include RoR in Leopard. If you have the developer tools installed, you don't have to do a thing.

For the past couple of weeks I have been playing with RoR. I made the usual user sign-up and login/logout as a tutorial/trail. However, now I'm experimenting with using a plug-in that will do it all for me (I'll keep you posted with what I find). I'm getting used to some of the things. There are some things I like (strict MVC, easy agile development, standard naming conventions) and some things I hate (migrations vs. models, too many places to define things, standard naming conventions). The one that beats out the rest is the agile development. It's easy to do piecemeal development, which fits hobbyist programming like this. The best part is how comfortable I feel working with one small bit at a time, knowing I don't need to plan my entire object or DB structure ahead of time.

I'm also starting to re-define the purpose of V. I also have been thinking about how I want it to look. I'm imagining the look of an old map. More details on this to follow

Tuesday, October 30, 2007

The big switch

A year ago, when I first started working on V in WebObjects, the WebObjects framework had just gone open source. There was a bunch of results from some talks at a WWDC, and an initial community had been founded. Now, I am finding that this community is starting to flourish, and now it provides some great resources.

One product of this community is a WebObjects plugin for the awesome Eclipse platform. Back then, it was early and buggy and just not right. Now, from what I had been reading, it actually works. A lot of WO things with XCode just never worked right. I thought it was me, and my setup, or the fact that I am using the free stuff. Apparently everyone has had to deal with the bugs of XCode. After this last issue with getting my old code running, I've had it! I'm making the switch!

The plugin is called WOLips and the site and community wiki can be found here:
http://wiki.objectstyle.org/confluence/display/WOL/Home

I installed the latest version of Eclipse w/ J2EE (3.3.0 Released under the name Europa). The WOLips installation tutorial says to first update to 3.3.1, then link to the latest (pre-release) builds. So I did that.

Next step was to make sure that I could do the single-directory deployment into Tomcat. Once again, community to the rescue. I used this tutorial to get the servlet up and running:
http://wiki.objectstyle.org/confluence/display/WONDER/Creating+a+wonder+app+to+deploy+as+a+servlet.

I tried it without the Wonder stuff (another OSS WO framework, to be implemented later) and it worked!

Wednesday, October 24, 2007

More Installs

I think it can be said that behind every good website is a good database. This probably includes both the DBMS and the organization. So my next step was to install a DB, and get it up and running.

Since V will be deployed on the standard cheap-o hosting site, I want to use the most common DBMS out there. This is MySQL. Which is good because it's free. I really like MySQL. I think it has just the right number of features and is very easy to install and maintain. It also has some of the best documentation and I've been using their SQL docs for a long time.

Complete instructions and links to download and install MySQL on Mac OS X can be found here:
http://dev.mysql.com/doc/refman/5.1/en/mac-os-x-installation.html

While the command-line stuff is always a blast, I'm a visual person and I need a visual way to manage my data. Another staple of the cheap-o hosting site is phpMyAdmin, a php tool that provides a great browser-based interface to an ODBC database. But before I can use phpMyAdmin, I need to get PHP. There are two options:
  1. Download the latest tar of PHP, make and install it (or get some binary package

  2. Enable the PHP that is installed in Apache in Mac OS X.

While the second option gets me an older version, it is a lot easier. All I want to do is run phpMyAdmin, and the version pre-installed is supported, so I took that route.

The instructions to enable the pre-installed PHP are here:
http://the.taoofmac.com/space/HOWTO/Enable%20PHP%20on%20Mac%20OS%20X
UPDATE: To enable PHP on 10.5 Leopard use this link:
http://clickontyler.com/blog/2007/10/how-to-enable-php5-in-mac-os-x-leopard/

Next step is to get phpMyAdmin. The download link for phpMyAdmin is here:
http://www.phpmyadmin.net/home_page/downloads.php

But of course, that's not all! phpMyAdmin must also be configured! Included with phpMyAdmin is a neat wizard script that sets up the configuration file for you. The first time I tried to run it, it wouldn't work at all, and I had to do some fiddling. I put in the sample config file, and added some random string to the blowfish thing, and that got it going. There was a bit more fiddling with the setup util before it finally worked. Eventually, I realized that the less stuff I entered, the better. So all I did was give it a super user's name and password, pointed it to the database, and it worked!

With all of the support systems in place (Tomcat, db server, db management), it was time to try and get my old V project running. I still had the xcode project, and all of the files, but I was now on a different machine, and xcode is very particular about keeping everything in place. Needless to say, things did not go very well.

I had over six hours of flying time to get my old project up and running. Just about when the stewardess was saying "turn off all electronics in preparation for landing", I finally got the old Main component to show up in the browser. Gosh XCode sucks.

Hmm.... Maybe I'll use some thing else. (hint, hint)

Thursday, October 18, 2007

Tomcat

Since I am starting over again with my application, the first thing I need to do is install software. My goal is to deploy my WebObjects application on a non-WebObjects server. This means that the program will be compiled and built into Java servlets. To test these servlets, I need Apache Tomcat.

When I first started working with WebObjects a year ago (see last post -Ed.) it seemed like there were very few resources for installing Tomcat on OS X. Most of the instructions I found were for out-of-date versions of Tomcat. Finally, I mashed together the directions from a couple of websites and somehow got the thing installed.

This time, things were different. I started with the usual Google search of "Tomcat OS X." The first results were the same, out-of-date versions or horrible directions. I almost gave up, but then I found the diamond in the rough on the second page. This thing is up to date (almost), clear, fast, and easy. Thank God for this German guy.

The best instructions for installing Tomcat on OS X:
http://wolfpaulus.com/journal/mac-tips/tc6013.html

So now I have Tomcat, up next is MySQL and the connection to WebObjects.

Chapter 1: I am born

This is the second time I have started development of the World of V. Actually, it's more like the fifth. While this blog will chronicle my efforts setting up and programming V, I thought I should start by tracing the history of this project.

I first started to work on a text-based online RPG around 2004. I had played a few online RPGs (Vampires! was my first, and of course inspired the V interface) and thought about making my own. In the summer of 2004 (I think) I took a course in Software Engineering. The term project was an online student grading system and my team and I built it with PHP and an Oracle database. This gave me some experience with PHP, and I realized that was how many of these games were programmed. I started playing with PHP and started making my own game to hone my skillz. I think I got as far as user registration, login, and moving around on the map. For some reason I always get stuck right around the map.

The following semester I took a course in relational databases (working for a company that makes post-relational dbs gives you a greater appreciation for the difference). I learned how I should have been setting up my project all along, and did a little normalization work. But still, nothing more than running around.

And then V was shrouded in darkness....

Spring of 2006. Graduate school. The course is Concurrent Programming. I am introduced to the coolness that is Java servlets. V is reborn. and reborn. and reborn.

V w/ servlets started as a normal servlet app. I think I combo'd JSP and the standard doGet routines methods. I think I only did user registration on that version. Then, the following conversation took place with a friend:
friend: "If you are using servlets, why don't you use AJAX?"
me: "AJAX? What the hell is AJAX?"
friend: "You've never heard of AJAX? Oh, it's awesome. It lets you update pages without reloading a page. Google uses it in gmail. Look it up."

a little later

me: "Wow, this is awesome. You can make some really cool stuff with this. "
friend: "Yeah, not many people know about it. You would think more sites would use it."
me: "Yeah, I'm surprised it isn't more popular"

And so V was reborn... again. I read all of the crap that's out there, wrote my own damn XML interpreter, and re-wrote V as an AJAX app. I did a lot of work on it. I made user registration and login. And you could walk around on the map. And then the XML started to crap out on me and I read an article on AJAX with JSON.

And so V was reborn.... again. This time I was passing back JSON stuff, so there wasn't much that had to change, just strip out the XML and throw in the Javascript crap. And then, stuff happened (long story, don't want to talk about it), and I found myself bored and with a lot of time on my hands (not that I had nothing to do, I had plenty to do, but I just wasn't going to do it). So I decided to learn WebObjects. I had always heard about it. I had certainly seen it on Apple's developer website, and it seemed like to good of a deal to pass up ($50,000 off, this week only!)

And so V was reborn... again. I worked on it a lot. I moved to New England. I got user registration to work, and you could sign in and walk around on the map. You could even battle monsters and enter buildings. And then I got a job and moved and had no time.

Less than a year later, I'm moved in and am bored at work. And so V is reborn again. But this time I'm documenting things. I know V will be a landmark game, one that historians will pin as the game that changed the world, and so as a gift, I am documenting all development in this blog. Plus, my old computer drown so I have to re-install everything again, and I really wish I had written down all of the steps I took while installing things.

So here goes nothing!