.:: Run Fat Boy .net ::.

Fitness for the buffet enthusiast.

Choose a Topic:

Clueless about getting into shape? Try RunFatBoy.

Top Posts:

Wed
28
May '08

Uncle Bob

My Uncle Bob bought my family growing up their first ever telescope, a telescope so big that at the time it stood taller than me. I can remember watching the stars with the neighbors on a few summers nights.

He took us on a tour of StorageTek in 1988 showing off proudly the systems that they had built and the parts that he had individually machined.

He garnered a respect in myself for the sciences and engineering disciplines that still lives with me today. And along with his adeptness for engineering precision and measurement, he was a really nice guy.

Our lives are given meaning through others. We live through others, we live for others.

Our individual lives are a manifestation of all that who have come before us. And it’s only through giving back to future generations do we give thanks to those that helped us along the way.

Thank-you Uncle Bob. Rest in Peace.

Uncle Bob
Bob Sedlak April 2nd, 1950 - May 20th, 2008

Fri
16
May '08

Managing Multiple Images for Multimodel Forms

So, I have this web app that I am creating for a friend that helps with managing inventory for eBay auctions.

When a user created a product, my intention was to allow them to upload as many images as they would like to be associated with that product. When they would go to edit the inventory item I would like for them to be able to delete any previously uploaded images and upload new images if they like.

Here’s what my interface looks like.

Interface screenshot.

So I have a good idea that I will probably use attachment_fu to manage the uploading of images. But wait, things are slightly more complicated because I will have one model that manages the product attributes (Products) and then another model that manages the uploaded images (ProductImages).

As usual, Ryan Bates to the rescue with his Railscast “Complex Forms Part 3″. Much of the following code will be familiar if you’ve watched this Railscast; I am just going to demonstrate the usage of attachment_fu with a multi-model setup.

So, within the product new/edit form I have a div defined where my images will be displayed.

  ........
  <p>
    Wholesale Price<br />
    < %= f.text_field :wholesale_price %>
  </p>
	
  <div id="images">
    Images
      < % @product.product_images.in_groups_of(3) do |group| %>
          < %= render :partial => 'product_image', :collection => group %>
          <p style="clear: both"/>
      < % end %>
  </div>
	
  <p style="clear: both"/>
	
  < %= link_to_function image_tag('add.png', :border => 0, :alt => "Add image.") + "&nbsp;Add Image" do |page|
    page.insert_html :bottom, :images, :partial => "product_image", :o bject => ProductImage.new
  end %>
  <p>
	
</p>

My product_image partial looks like this :

    < % if product_image != nil %>
        < % fields_for "product[image_attributes][]", product_image do |f| %>
          < % if product_image.new_record? %>
            <div class="newimage">
               <p>
                 < %= f.file_field :uploaded_data, :class => "formField", :index => nil %>
                 < %= link_to_function "Remove", "$(this).up('.newimage').remove()" %>
               </p>
            </div>
        < % else  %>
          <div class="image">
            < %= image_tag(product_image.public_filename(:stamp), :border => 0) %>
	    < %= f.file_field :uploaded_data , :index => nil, :style => 'display:none' %>
	    < %= link_to_function image_tag('cancel.png', :border => 0, :alt => "Delete image."), "mark_for_destroy(this)" %>
	
            < %= f.hidden_field :id, :index => nil %>
            < %= f.hidden_field :should_destroy, :index => nil, :class => 'should_destroy' %>
          </div>
      < % end %>
    < % end %>
  < % end %>

Areas of interest : I’m using the fields_for form helper because our images are part of another model. There’s a set of empty brackets after the definition

product[image_attributes][]

to tell Rails that we’re passing an array (in this case multiple images).

The call to the mark_for_destroy method. This allows me to directly hide the display of the image and mark a form flag that tells the system to destroy it once the form has been submitted. This technique is outlined in Ryan’s railscast.

In application.js


function mark_for_destroy(element) {
	$(element).next('.should_destroy').value = 1;
	$(element).up('.image').hide();
}

I have a product_image model that manages the attachments using attachment_fu. It has an attribute should_destroy which allows an image to be marked for deletion from the form.

class ProductImage < ActiveRecord::Base
  has_attachment :content_type => :image,
                 :storage => :file_system,
                 :max_size => 2000.kilobytes,
                 :resize_to => '320x200>',
                 :thumbnails => { :thumb => '150x150>', :stamp => '75x75>' },
                 :processor => :ImageScience   
	
  validates_as_attachment
	
  attr_accessor :should_destroy
	
  def should_destroy?
    should_destroy.to_i == 1
  end
	
end

For the products model, we want to setup a virtual attribute called image_attributes (remember the array of images that we defined in our form above?) that will handle the saving/deletion of product images.

class Product < ActiveRecord::Base
  belongs_to :vendor
  belongs_to :product_status
  belongs_to :ebay_category
  has_many   :product_images, :dependent => :destroy
	
  after_update :save_images
	
  def image_attributes=(image_attributes)
    image_attributes.each do |attributes|
      if attributes[:id].blank?
        product_images.build(attributes)
      else
        product_image = product_images.detect { |t| t.id == attributes[:id].to_i}
        product_image.attributes = attributes
      end
    end
  end
	
  def save_images
    product_images.each do |image|
      if image.should_destroy?
        image.destroy
      else
        image.save(false)
      end
    end
  end
	
end
Tue
13
May '08

Recording Type Definitions From Unit Tests For Autocompletion

I just got done reading Steve Yegge’s “Dyanmic Languages Strike Back”. He notes that one of the perceived problems with dynamic languages is the lack of tools, (he notes specifically the autocomplete feature).

While the thought of type declarations or any sort of annotations makes me cringe, is there any useful type data that could be recorded while running the unit tests that could later be used for the autocompletion of methods from within your Favorite Editor (I Textmate)?

While this may be cheating by deriving types at runtime, it certainly seems simpler than the probabilistic methods Steve outlines for determing types in dynamic languages. And it would certainly encourage the act of writing tests if you could get the added productivity boost of autocomplete.

Sat
3
May '08

Lower Blood Pressure and Seasonal Changes

Ever since Jr. High I looked forward to the winter time. I loved the way the cool breezes hit against my face. I loved the fact that I could wear a long sleeve shirt and not worry about breaking into a sweat or develop pit stains. Winter was the fat man’s paradise where the lower temperatures gave me a sense of freedom that no other season provided. I was in a seasonal wonderland where my above average body heat didn’t control me and I could finally wear and act how I wanted without the embarrassment of my overly-sweating body showing itself.

But winter was no friend and he was only acting as a mask to further deep rooted problems. Most people who chronically sweat are probably over weight and I was no exception. Furthermore, I had high blood pressure as a result. And the high body temps were probably a result of this. Who wouldn’t be hot when your heart is working twice as hard as the average individual?

In May of 2006 I was hospitalized for my high blood pressure and as a result, I have been taking three different blood pressure medications. Atentolol, a beta blocker, Avalid, a diuretic, and Norvasc, a vasodilator.

My entire perception has changed. With a normal blood pressure, I now enjoy the sun and the heat and have even worn a long sleeve shirt when it’s 80 degrees out. I actually have days where I want to feel the warmth of the sun against my neck and feel the summer’s breeze blow against my shirt.

It’s such a strange perception shift. This has to be the feeling that one would feel when observing an indigenous tribe for the first time or experiencing weightlessness in space. While before others would tell me that they “feel cold”, I didn’t understand. But now I feel their cold and I feel their discomfort, and for once my perception of temperature seems right in line with those around me. And I am finally figuring out that I was living in a whole different world these past few years.