cnp_studio (cnp studio) - An interactive division of Clark/Nikdel/Powell

A unique mix of technology, creativity and human interaction makes cnp_studio a Web development firm focused on connecting people. Creating simple, useful Web sites is what we do. Learn more about us.

cnp_studio Blog

cnp_studio Blog

Archive for the ‘Rails’ Category


Missing acts_as_conference

Friday, February 8th, 2008

Halo 3

Well. It's 8:30, and I'm sittin' here at my office instead of attending acts_as_conference. Why? 4 projects are comin' in for a landing next week, and in our business, the "billable" hour is gospel. I'm not mad or anything, you just have to prioritize this stuff and getting to do the "fun stuff" has to come after the bottom line.

I've known about acts_as_conference for months, and planned as best as I could, but I have got 2 projects (both launching next week) that walked in the door last week. What's a guy to do?

In a way, it's kind of like college. One attends college to go to class, not sit around and play Halo 3 all day.

Although, if I truly believed that, I'd be at the conference.

Rails 2.0

Friday, December 7th, 2007

After months of waiting, Rails 2.0 is finally here!. If you haven't already, you can get Rails 2.0 with:

$> sudo gem update rails --include-dependencies \
--source=http://gems.rubyonrails.org

note: I had to use the '--source=http://gems.rubyonrails.org' after a few 'gem not found' errors.

Obviously, if all your applications are still running 1.2.x, there are a few caveats with just upgrading to 2.0, so be sure not to run 'gem cleanup'. If you do, you can always re-install that removed gem with:

$> sudo gem install rails --include-dependencies \
--version 1.2.x

So, let's say that umm... I were to update some existing production applications to Rails 2.0, without testing… what will break?

Where to begin? For starters, you hopefully wouldn't do that. Second, if you write good tests, run 'em. You should have plenty of output letting you know what's different. You may need to tweak a few things before your tests will even run. From the get go, there were only a few things that affected my older development applications, but most of what I've been working on lately has been release candidate copies and "preview" releases of Rails 2.0.

With my older applications, the only major issue I ran into when trying to run tests was the undefined method `server_settings=' for ActionMailer::Base:Class message. The method has only been renamed. Adjust the appropriate setting to read:

ActionMailer::Base.smtp_settings

A few of my favorite new features have been some of the updates made to routing. The semi-colon has been dropped from generated routes so /admin/pages/31;edit is now /admin/pages/31/edit. No more ;action_name. As trite as it sounds, that slash just looks better, and to me anyways, is much more RESTful than the semi-colon.

Route “namespaces” are definitely a powerful addition to ActionPack. Where you might have implemented a named route like:


ActionController::Routing::Routes.draw do |map|
  map.resources :articles,
    :path_prefix => "admin",
    :name_prefix => "admin_",
    :controller => "admin/articles"
end

With Rails 2.0, you can implement that same route with:


ActionController::Routing::Routes.draw do |map|
  map.namespace(:admin) do |admin|
    admin.resources :articles
  end
end

One of the things that really got me though, was the change to named routes and how they are called from within the application. With the above example and using version 1.2.6, I would write: admin_new_article_path, but this generates an undefined method error. The correct way in Rails 2.0 is new_admin_article_path. The same applies for edit actions. You can always run rake routes to see a listing of all the routes in your application, which is an extremely helpful rake task (although I'm sure a plugin for this existed out there before 2.0, just never really looked for it).

If you had err.the_blog's Sexy Migrations plugin released in March, you know how nice it was to write much simpler migrations. Shortly after the plugin was released, it was added to the edge Rails trunk. Now, migrations are much DRYer and more efficient to write:


class CreatePages < ActiveRecord::Migration
  def self.up
    create_table :pages do |t|
      t.integer :position, :version
      t.string :title, :limit => 150
      t.string :keywords, :limit =>250
      t.text :content

      t.timestamps
    end
  end
end

Notice how you can put 2 column names on one line? And the t.timestamps declaration will automatically add created_at and updated_at fields. Ahh…

Hopefully you've been keeping up over the last couple months since the preview release was announced on September 30th. If not, head on over to PeepCode and pick up the Rails 2 PDF written by Ryan Daigle (who also has a pretty sweet list of major changes and features on his site).

:attachment_fu and Amazon’s S3 For File Uploading

Monday, July 30th, 2007

Most of the Rails sites I have been developing lately have been using a plugin called attachment_fu by Rick Olson - one of the core developers of Rails. attachment_fu is easily and effortless - allowing me to add the ability to add "attachments" or uploads to any model within my application. After reading the excellent tutorial by Mike Clark, I was up and running in no time.


class Download < ActiveRecord::Base
  has_attachment :storage => :file_system,
    :path_prefix => "/public/downloads/",
    :max_size => 30.megabytes
end

And in my view(s):

 
<td class="input_field">
  < %= f.file_field :uploaded_data %>
</td>
 

But after a while, downloads started to build up in size consumed, and backups were using valuable bandwidth (since backups moved the files off the server). I remember reading on the excellent tutorial by Mike Clark how easy it looked to change the storage to use Amazon's Simple Storage Service (S3). I installed the AWS::S3 Ruby library, and even played around with s3sh - the interactive shell utility that comes with the AWS::S3 library.

Revisiting the tutorial, I changed my Download model to:


class Download < ActiveRecord::Base
  has_attachment :storage => :s3, :max_size => 30.megabytes
end

And created my config/amazon_s3.yml configuration file:

 
production:
  bucket_name: my_bucket
  access_key_id: XXXXXX
  secret_access_key: XXXXXX
 

After a few quick tests, everything was working perfectly. Now the only problem I had was moving the existing downloads into the S3 account. I figured the best way to go was to integrate a rake task to help me out.


desc "Move all downloads to Amazon's S3"
task :move_to_s3 => [:environment] do |e|
  require 'rubygems'
  require 'aws/s3'

  AWS::S3::Base.establish_connection!(
    :access_key_id     => 'XXXXXX',
    :secret_access_key => 'XXXXXXX'
  )

  Download.find(:all).each do |d|
    AWS::S3::S3Object.store("/path_on_s3/to_new/download",
      open("/path/to/where/current/file/is_located"),
      'my_bucket',
      :access => :public_read)
  end
end

All done. Total cost to move all this stuff over to S3 was about $.10 .

Also, if you're interested, check out S3Fox - a plugin for Mozilla Firefox that allows you to browse, organize, and store your files into your S3 account - all through Firefox.

Latest Comments

andrew:
hey mike -- thanks for the reply, let me clarify what i mean.... I know that PHP fu...

nick:
Hi Jeff, Thanks for the heads up on the link. It's all fixed now and you should...

Jeff:
I would love to try your plugin, but the download link appears to be dead again. Ca...

mike:
@Denise: 1. The image is selected randomly each time the code is run. So normally ...

andrew:
hey -- great plugin and would like to use on several different pages, not just the ...

Categories Archives