My Octopress Blog

A blogging framework for hackers.

Important to Be Nice

It’s nice to be important, but it’s more important to be nice.

Elements of Style

Spontaneous Me” sang Whitman and, in his innocence, let loose the hordes of uninspired scribblers who would one day confuse spontaneity with genius.
—Excerpt from “The Elements of Style - Fourth Edition” Section 5.9

Hilarious Editorial Cartoons

Posted on my blog on August 2rd, 2005


I was searching google images for “gift wrapping” of all things, and I stumbled upon a set of editorial cartoons. They were done by a man named Clay Bennett. These are a few of my favorites: On Security: Sacrifice for Security by Clay Bennett On Medicine and Politics: Political blockades on Medical Advancement On the inability for the CIA and FBI to work together: Our protectors View the entire collection of editorial cartoons, or just check out Clay Bennett’s website And my Number 1 favorite: Amateur Theif I’d love to hear what your favorites are. :)

Useful Webdesign HTML Codes and Symbols

I keep looking these up.. so here they are for reference.

» — »
« — «
© — ©
> — >
&lt; — <
&quot; — “
&amp; — &

Good Will Hunting and Gas Prices

Meanwhile he realizes the only reason he was over there in the first place was so we could install a government that would sell us oil at a good price. And of course the oil companies used the skirmish over there to scare up domestic oil prices. A cute little ancillary benefit for them but it ain’t helping my buddy at two-fifty a gallon.
—It’s amazing how accurate this quote is, coming from a movie made in 1997:
Good Will Hunting

Using Attachment_fu From Script Console

I recently had to migrate a whole bunch of photos from one app to another, and since I’m using AttachmentFu for the image attachments I just opened up script/console so do the dirty work.

However, the dirty work didn’t work.

With FileColumn, I could just go object.filename = “directory/#{filename}”, but that didn’t seem to do the trick with AttachmentFu. I needed to provide size and content_type.

I added an extension to the File class called content_type:

class File
  # your expected mime types here
  FILE_EXTENSION_MIME_TYPES = {
    'jpg'  => 'image/jpeg',
    'png' => 'image/png',
    'gif' => 'image/gif'
  }
  def content_type
    FILE_EXTENSION_MIME_TYPES[File.extname(self.path).downcase.gsub(/^\./,'')] || 'application/octet-stream'
  end
end

and then had to use this call to create the new attachment_fu object:

f = File.open(file)

Photo.create(
   :temp_data => IO.read(file),
   :content_type => f.content_type,
   :filename => file.split('/').last
)

and now in action:

Property.find(:all).each do |p|
  next if p.photos.length > 0
  default = true
  Dir.glob("ap/#{p.internal_code}/*").each do |f|
    file = File.open(f)
    PropertyPhoto.create(:property_id => p.id, :temp_data => IO.read(f), :content_type => file.content_type, :filename => f.split('/').last, :default => default)
    default = false
  end;nil
end;nil

Hope that helps a few of you figure it out faster than I did.

Handling Multiple Object Types With the Same Partial

I’m currently using a single form partial to handle multiple objects that are all using STI. This usually isn’t a problem because the form_for helper lets you keep the actual object anonymous, like this:

<p><b>Name</b><br /><%= region_form.text_field :name %></p>

But what happens when you want to use a helper that doesn’t work so well with the

region_form.text_field
objects?

I did a little bit of digging into the default object that is passed into the partial, and this is what I found:

region_form.object_name

This lets me implement my multiple select for every type of object, without having to specify anything explicitly.

Example:

In the view:

<% form_for(:province, :url => provinces_path) do |f| %>
 <%= render :partial => 'shared/region_form', :object => f %>
 <p><%= submit_tag "Create" %> or <%= link_to 'Cancel', provinces_path %></p>
<% end %>

In the helper shared/_region_form.html:

<p><b>Name</b><br /><%= region_form.text_field :name %></p>
<p><label for="alias_region_id">Aliased By:</label><br />
<%= select_tag(
               "#{region_form.object_name}[aliased_by][]",
               options_from_collection_for_select(
                 Region.find(:all),
                 'id',
                 'title',
                 instance_variable_get("@#{region_form.object_name}").aliased_by_ids),
               :multiple => true
             )
%></p>

So, we use a combination of region_form.object name and instance_variable_get to make the form handle whatever object we throw at it.

Yay ruby!