Rails 3.0 nearly ready

August 24th, 2010

According the the Riding Rails Blog the final release for Rails 3.0 should be this week. Lots of tweaks and bug fixes, but the things most people (including me!) are excited about are the new methods for handling gems (Bundler) and ARel. Accessing models using relational algebra. Should be much more efficient!

Array dereferencing in PHP

August 3rd, 2010

Just saw this: neat PHP feature.

Many is the time I wanted to just grab something from an array returned by on of the many great core PHP array methods without having to assign it. And now I can.

Built in sanitizing with PHP

July 19th, 2010

A friend of mine just sent me this link about built in validation/sanitizing with php 5.2

This is one of those nice features that veteran php devs have always wanted and have probably missed in the updates.

Zend has always had a pretty nice filter module but this is built into PHP core, so no need to worry about library dependencies.

Check out the list of filters here. Finally, built in IP validation.

action caching and content_for

June 16th, 2010

So as happy as I am with my action caching over on Alternity, there is one problem that’s bugging me.

I’d like each world and category to have a title that reflects the content. I put the content_for :title in my views, threw a yield :title in my layout and as expected, each of my pages had an appropriate title. Committed the change and didn’t think about it too much afterward until I noticed that there was no custom title on my live site. Duh, the content_for method is called when the view is rendered and since my action caching was fetching the pre-rendered *.cache files from my cache, naturally there was no :title for the layout to render. One solution would be to simply include the layout in the action cache, but I have reasons for not wanting to do that. It makes cache invalidation a bear.

I looked up how content_for worked and found that it simply saved the text into an instance variable:
@content_for_{name_of_the_content_block}
@content_for_title in my case.

It seems like the way to fix this would be to have the action cacher check for those instance variables and append them to the bottom of the *.cache files, then check for them when rendering and put them back into an instance variable.

Say, a chunk of text at the bottom of the file:

content_for_title blah blah blah
content_for_nav blah blah blah

Anybody done anything similar? I’m planning it out, but I want to wait until Rails 3 drops to see if there’s changes to the caching.

Amazon adds S3 to AWS console

June 13th, 2010

The AWS Management Console now provides a simple and intuitive web interface for managing your Amazon S3 resources. You can manage your existing Amazon S3 resources, as well as create new buckets and upload objects to your buckets using the console.

The console simplifies managing your Amazon S3 resources by enabling you to:

  • Access your Amazon S3 resources from anywhere using a web-based user interface.
  • Log in using your AWS account name and password. If you’ve enabled AWS Multi-Factor Authentication, the console will prompt you for your devices authentication code. No need to look up and enter your Access Key ID and Secret Access Key.
  • Oversee your AWS resources in a single, convenient location. The AWS Management Console now supports Amazon S3, Amazon EC2, Amazon CloudFront, Amazon Elastic MapReduce, and Amazon RDS.
  • Manage buckets with billions of objects. The console also works with folders and objects created using many popular third party tools.

To start managing your Amazon S3 resources through the AWS Management Console today, go to console.aws.amazon.com/s3. For more information about Amazon S3 go to aws.amazon.com/s3.

This is great news, I use Panic’s Transmit for my day to day management of my S3 buckets, but having a handy web interface is great when I’m on the go.  If you’re not using Amazon’s S3 and CloudFront for your CDN right now, give it a shot.  It’s reduced my image loading time by about a 6th and generally costs less than 5 bucks a month.

Auto-generate a sitemap with a rake task

June 4th, 2010

If you want to hurry up the indexing of the links on your site, it helps to have a sitemap. Google has a handy way to upload one: Webmaster Tools.

The problem can be generating one. There are a number of tools online (most of them cost money beyond a certain link count) or you can hand create the xml and submit, but your best bet is to create a script and auto-generate the sitemap frequently. Since my Alternity site is in Ruby on Rails, I decided to create a quick rake task to do the deed.

So in lib/tasks, I created a file called alternity_sitemap.rb

namespace :alternity do
  desc "Create a google sitemap"
  task :sitemap => :environment do
    include ActionController::UrlWriter
    default_url_options[:host] = 'alternity.jakanapes.com'
 
    #do a bunch of ruby code to generate the xml in the format described on the google webmaster pages
 
   sitemap = File.new("#{RAILS_ROOT}/public/alternity_sitemap.xml",  "w")
 
    sitemap.write(xml)
 
    sitemap.close
  end
end

:sitemap => :environment is so I can pull in my models
include ActionController::UrlWriter pulls in the url helper (i.e. worlds_url)
default_url_options[:host] = ‘alternity.jakanapes.com’ sets the base host of the url

I set up a cron job to run rake alternity:sitemap once a month and pointed the google webmaster tools to that location.

I quickly went from less than 100 indexed links to nearly 2000!

Ruby on Rails mysql gem in Snow Leopard with MacPorts

June 3rd, 2010

Because I have to look it up every time!

If you want to install the ruby mysql gem on a machine running Snow Leopard with mysql installed via mac ports:

sudo env ARCHFLAGS="-arch x86_64" gem install mysql --  --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

Coming soon

June 1st, 2010

Really looking forward to the SQL Antipatterns book. Just reading some of the excerpts on the Pragmatic Bookshelf site has gotten me pretty excited.

SQL Antipatterns

Toggle Action Caching

June 1st, 2010

So if you’ve done any Ruby on Rails work with something that’s dynamically fetched but has pretty static content (like my worlds over on Alternity) then you know the joy of action caching.  Personally, I was seeing a speed increase of around 10x when fetching the cached page.

I got my caching and cache invalidation going, but ran into another problem when I attached admin actions to items.  I wanted to add an edit link to an item when I was logged in as an admin user.  Of course, if the page had been cached, I would never see the link and if it was the first time to view the page, then the edit link would get cached.

My solution was to add an around toggle to those actions that I would be doing editing on:

def toggle_caching
ActionController::Base.perform_caching = !ActionController::Base.perform_caching if session[:logged_in]
 
yield
 
ActionController::Base.perform_caching = !ActionController::Base.perform_caching if session[:logged_in]
end

Works like a champ.  My admin views are a little slower, but since I’m the only one who sees them, I can’t say I care that much.

If anyone knows of a better way, though, I’d love to hear it.

Encrypted folder on Snow Leopard

June 1st, 2010

I recently had a need to create a password protected folder on my Mac.  Did a little digging and found DiskUtility to be the answer.

  1. Open DiskUtility at ~/Applications/Utilities/Disk Utility
  2. File > New > Disk Image From Folder
  3. Select a folder and click the Image button
  4. Image Format -> read/write (if you want to be able to modify the contents)
  5. Encryption -> 128-bit AES
  6. Enter and confirm password
  7. Password protected folder is created

This was great.  My encrypted folder is now mounted at /Volumes/name_of_folder when I open the *.dmg file.  Except that I needed to add files to the folder periodically and the disk image I had created was bound by the original size, give or take a few bytes.

So start over.  Did a little more digging and found sparse images. Sparse Image

  1. Open DiskUtility at ~/Applications/Utilities/Disk Utility
  2. Click New Image icon, making sure that nothing is selected on the left hand side
  3. Enter a name in the Save As field.  This will be the filename of the sparseimage file
  4. Enter a name in the Name field.  This is the name of the disk image that will be opened
  5. Choose a maximum size for the disk image
  6. Choose 128-bit AES encyrption
  7. Choose sparse disk image under Image format
  8. Confirm password
  9. Create your sparseimage file

Done.  Now click on the *.sparseimage and your disk image is mounted as /Volumes/name_of_disk_image.  Add, delete and modify to your heart’s content.