Saturday, May 21, 2011

A little late-night coding

I wish I didn't have to get up at 7 am during the week. I've always felt most productive and focused when working on projects late at night.

I've been reading the archives at railstips.org lately. I'm using some liberty when I say "reading the archives," because I really only got to this article on good Rails tutorials.

On a side note: I've linked to a 5 year old article which is a link to another article which is a collection of Rails tutorials. Is that ridiculous?

ANYWAY! My friend Eli encouraged me to try out some of these tutorials, despite their age. Most of them use bizarre, antiquated syntax. Luckily, I found an updated version of the first tutorial, which walks you through creating a recipe database. It's a simple application, allowing a user to create recipes and categories, then assign recipes to categories. I can think of several ways to expand functionality for this application: allowing users to search by ingredients, sorting recipes by categories, allowing users to create meals consisting of multiple recipes and so on.

I try to think of these things when I'm doing a tutorial or working on a personal project. I always ask "What else could be done? Where else could this go?" Luckily, Rails not only makes it easy to build something functional very quickly, it also makes adding functionality easy. I'm not sure if this is true for most other languages, but I highly doubt I'd have gotten interested in Rails if it wasn't so simple to pick up the basics.

Not that I think I'm an expert, or even knowledgable! I still get discouraged, especially when I can't figure out how to do something that SHOULD be easy, but I'm getting better every day. I'm planning to work through some of the tutorials on that list, hoping to learn some techniques that I can apply to Manticore and other projects.

A couple of interesting things I learned tonight:

Linking back to the main page of an application:

I got on irc and asked about this. It turns out the trick is this:

<%= link_to 'Home', :root %>

I'd tried linking to various forms of home, index, homes_path and index_path with no luck. This nice little trick saves a bit of time when navigating, though in a real application I'd want to use breadcrumbs instead. I'm assuming that would be built in much the same way?

Pulling information from one model and displaying it in the form of a second model:

In this application, I have two models: Recipe and Category. Each Recipe will belong to a Category, and to build this information in the form for a new Recipe, here's the code:

<div class="field">
  <%= f.label :category %><br />
  <%= select "recipe", "category_id", Category.find(:all).collect {|p| [ p.name, p.id ] }, {:include_blank => true} %>
</div>

It feels good to make progress, and I think I should be talking more specifically about what I've learned. I feel like my learning has been all over the board, but maybe that's how everyone learns to code.

Thursday, May 19, 2011

WTF is an attribute

So I've run into another wall in my work on Manticore. Basically, here's where I'm at right now:

I have the Character model, with several models with a has_one association: Statistic, Fortitude, Reflex, Will, Armor, Hp and Initiative. These are all displayed on the Character show page.

What I want to do is create a horizontal menu bar that link to other pages with further information. The first one I want to do is an Items page, with various items such as Weapons, Armor, Magic Items, Gear and Treasure. I'm seeing these as separate models that belong_to the Character model, but I'm unsure how to go about building this.

Look, I made some visual cues (mostly because I am awful at explaining what I'm trying to do)


Here's a rough idea of how the final, styled application might look. At the top, you'll see "Items." This will be a link that takes a user to a new page, like so:


So now for more questions!

When I asked about this on Stack Overflow, several people said the models I've created for Character (Hp, Armor, Statistic, etc) should really be attributes. I'm not clear on how an attribute differs from a model. Can a user edit an attribute from the view the way a user can edit the model, the way I've built it? Am I creating too much complexity here? My thinking was that things that change (relatively) often, such as Items, Spells, Statistics, should be Models for ease of editing, styling and displaying, whereas things that don't change, such as a character's name, class, race, etc, would be created as columns in the Character model.

Should these be attributes instead of columns? Are columns attributes? I'm not entirely sure what attributes are, and it's hard to find concrete information.

In other news, I've been reading through the archives of railstips.org. I found an article about 12 great rails tutorials, but the problem is the article it linked to is 6 years old. What! So if you've got any great tutorials that are more recent, lay them on me.

Onward!

Sunday, May 15, 2011

Triumph of the heart

So Blogger was down for a day or two this week, which sucks because I really wanted to talk about the progress I've made with Manticore. Thanks to a ton of friendly advice from more experienced coders (UnixMonkey, jqr and a ton of guys on Stack Overflow) I finally fixed the problems I was having with the has_one relationship.

For people who are just tuning in, Manticore is the Dungeons and Dragons character database application I'm working on. I had created a Character and a Statistic model, where Character has_one Statistic and Statistic belongs_to Character. However, I had some issues with displaying the Statistic model on the Character page, and also ensuring there was only one instance of Statistic in use at any given time. But no more!

I was able to take the code David (UnixMonkey) showed me on github and extrapolate that to create new models that belong_to the Character model with a has_one relationship.

So far, I've got a working Character model, with models for character statistics, saving throws, armor class, hit points and speed. I'm beginning to see how I want all this information displayed. Eventually I want it to be a page with 5 tabs: Character, Equipment, Skills and Feats, Spells and Background. I see the Character tab as being a quick sheet with all the relevant information for playing a game, and the other tabs having more detailed information.

I'm also thinking about other things I can do with this app. For example, Armor Class is composed of several different numbers: values for armor worn, a shield, a character's dexterity modifier and more. So it would be pretty easy to ensure the total AC value is equal to all of the separate values and display an error message if this is not true.

I mentioned that I see the Character tab as being a quick page with combat information at the ready. Eventually, I'd like this page to contain some tools to make playing a game easy from that view: stand alone dice rollers and specialized dice rollers that a player can create and save. For example, your character has a short sword and you want to save an attack roll with the short sword. So this would create a button that generates a random number from 1 to 20 (the attack roll) then adds in any relevant modifiers (attack bonuses, weapon enhancements, etc) and spits out a total number.

I've also been thinking about tools specifically for Dungeon Masters. Things like a database of plot hooks to generate ideas for campaigns or short adventures, an NPC personality and description generator and so on.

A lot of Rubyists go on and on about how Ruby on Rails makes programming fun. While I'm beginning to see why that's true (since you spend most of your time building things and not trying to get the tools you need to build things to work) I really think it really opens you up to what's possible. Now that I've got some ideas of HOW to build things, I'm getting lots of inspiration for things TO build.