SyntaxHighlighter

Tuesday, September 26, 2006

Containing floating elements

A common problem with using XHTML and avoiding tables - containing the floating left and right elements on the page so the parent element completely wraps the children. If you are making a left nav, using a < id="leftnav"> approach, you've probably struggled with this at some point.

Here is a great article by Ed Eliot explaining a few strategies for fixing this.

http://www.ejeliot.com/blog/59

Monday, September 25, 2006

Document.getElementById Bug/Problem

The getElementById method is supposed to return the first element in the document with the specified id. See the spec. There is a bug in IE (perhaps it's intentional), that will return an element with the name, instead of the id.

Try out the following. Get element by ID and display it's ID. You'd think it would be the same id that was just asked for, in this case 'child'...

< type="checkbox" onclick="alert('child = ' + document.getElementById('child').id)">
< type="hidden" name="child" id="hidden_child">
< type="checkbox" id="child">



To work around this - make sure you keep id's unique to each other and to elements that are named. Or be sure the id elements are found first, in document order.

Sunday, September 24, 2006

Ruby file upload

OK - An update. I got a suggestion from a friend (after my upload post) to try file_column. I tried it and it almost worked perfectly. Much nicer for my uses.

The one problem - the files are stored in directory per image_id - one image per directory. This was overkill for my needs, I wanted to store the images in a directory per project_id. To do this, I had to make some manual changes to file_column.rb and my environment.rb files. I'll do my best to post what I did here...

On lines 359 and 361 in file_column.rb, the original code would delete an existing directory and move over the temporary directory to replace it (after a successful download). I modified it to simply move the file over, make the directory if it needs to.

FileUtils.mkpath(@dir) unless File.exists?(@dir)
FileUtils.mv File.join(local_dir, @filename), File.join(@dir, @filename)

On line 395 the code deletes the directory, you must change the delete_files method to be

FileUtils.rm_rf File.join(@dir, @filename)

So only the particular file is deleted.

In my environment.rb file, I overrode the ___ method so the directory name would be based on the user's project.

require 'file_column'
require 'file_column_helper'
require 'rails_file_column'

module FileColumn
class PermanentUploadedFile

def relative_path_prefix
project = Project.find(@instance.project_id)
"#{project.name}"
end

end
end

Thursday, September 14, 2006

Beatstreet

Here's a product idea for you.

Beatstreet - A music player for runners where they can control the tempo

How about a mp3 like player (it would play midi files primarily) that has the ability to change tempo. The midi tunes would play for as long as the runner wants. Ideally, it would change rhythm upon request, but seemlessly integrate it with the current beat.

I call them 'rhythms' as they would be very repetitive and motivating, like a good Moby song or dance beat.

Research shows runners reach their best times listening to music they enjoy. What about music that increases their rhythm?

As Ethan Lowry points out, if Nike's new ipod included this feature, it "would be incredibly cool".

Wednesday, September 13, 2006

Ruby file upload

Uploading files in Ruby took a little time - I was mainly hung up on the File and Directory management. But, after a little trial and error on my part, here's the result. Comments are welcome, as I'd like to improve it...

Some notes - I wanted to store the images in a custom directory under /resources. The resouce model object has a 'path' and a 'title' property.

def self.new_from_upload (upload, directory)
file_name = upload.original_filename
dir_path = "#{RAILS_ROOT}/public/resources/#{directory}"
file_path = "#{dir_path}/#{file_name}"
resource_path = "/resources/#{directory}/#{file_name}"
file_name.chomp!(" ") # remove spaces on the ends


if file_name.length > 0
data = upload.read
# Delete file if it exists

delete_file_if_present file_path

# Write new file
create_dir_if_missing dir_path
File.open(file_path, "wb") do
f f.write(data)
end

resource = Resource.new
resource.title = file_name
resource.path = resource_path
return resource
end
end

private
def self.create_dir_if_missing name
Dir.mkdir(name) unless File.directory?(name)
end

def self.delete_file_if_present file_path
File.delete(file_path) unless !File.exists?(file_path)
end

This creates the directory and the file (if missing). Overwriting the file when it's uploaded again.

One improvement that is coming - I'm going to store the file in a temporary directory until it is uploaded. Once in place - I'll move it over and update the model object.

Saturday, September 02, 2006

Google Code

I've been trying to find a server to host a project I am working on. I didn't want to pay for an ISP and I was willing to make the project Open Source. The project was originally going to be in Java, so I tried to get on tigris.org, or sourceforge.net. Both turned me down (well, tigris didn't reply). I finally got on to java.net (the projects name is 'poster'), but that was more than I needed and CVS was required for source control. I was hoping to use SVN.

I recently discovered Google Code and this is perfect for my needs. It uses SVN, has a very simple issue tracker, and makes it very easy to select an open source license. So far so good.