0

Dynamic placeholder images in Paperclip

Tonight I was integrating Thoughtbot’s Paperclip plugin as I have done many times before. Only this time I realized what a pain it really is to deal with missing images. Paperclip does a pretty good job at this by looking for a “default_url” which you can set in your model. This is good, until you’re more than a few models in. Then I found this solution. Now we’re getting somewhere. But I still thought, why can’t this be completely dynamic? Well it can, and pretty easily I might add.

First go get mdarby’s placeholder on the github. This gem makes use of placehold.it‘s super easy api for creating, you guessed it, placeholder images. It’s actually quite useful.

Next we have to monkey patch paperclip. Create an initializer file called paperclip.rb and drop in the following.

class Paperclip::Attachment
  def url(style_name = default_style, use_timestamp = @use_timestamp)
    url = original_filename.nil? ? get_placeholder(style_name) : interpolate(@url, style_name)
    use_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url
  end  
 
  def get_placeholder(style_name)
    Placeholder.new(styles[style_name].geometry, :text => "Placeholder").url
  end
 
end

Basically we’re just overriding this method to generate a placehold.it url when the attachment is not present. One downside to this method is that it overrides is globally. You will no longer be able to set the “default_url” in your model. But hey that’s the whole point of this anyway right? Right. I plan to fork paperclip and try to get this in as an actual option, but we’ll see how long that takes. Anyway, enjoy!

1

10 Things Android wishes it did better than iPhone OS

I read on Gizmodo today 10 Things Android Does Better Than iPhone OS and it made me very unhappy. As an owner of both systems I think I have some insight into how things really are.

Maybe in a perfect world all those things would actually make the phone better, but here in the real world, they just make the phone slower. Widgets, multitasking, social networking and notifications really just get in the way of doing useful things. Proof of this is the fact that I have to have an app just to kill all the apps that stay running after I close them. And the mysterious apps that start whenever they feel like it. Also the fact that I have to restart my phone every day to keep it running at top performance (like actually being able to make and receive phone calls). I would leave my iPhone on for MONTHS without any loss of performance.

To say that the App Store is inferior to the Android Marketplace is just ridiculous. The truth is on either market the ratio of total crap to anything useful is about 1000:1. Therefore, since the App Store has roughly 3 times as many apps, you’re bound to get better options.

Both systems look stunning in pictures and on feature lists. But the iPhone clearly stands alone in the real world. Yeah you can’t have a calendar widget on the iPhone. But on the iPhone when you open apps, you don’t have to sit there waiting for them. They open quickly and let you get on with your life.

0

25 questions every programmer should ask on a job interview

The last few jobs I’ve had proved to be somewhat disappointing. What I realized after a few months was that it was mostly my fault for not asking the right questions.  In a way a job interview is really for both parties. Not only do they have to think that you fit in, but you have to think that you’ll enjoy working there. These 25 questions are what I should have asked. Hopefully they can spur you on to write your own, or you can just take these. Either way it’s important to figure out what you’re looking for in a job or career.

General Questions:

  1. Is there a dress code?
  2. What are the normal business hours?
  3. Who would I report to, and what are his/her hours?
  4. Do you offer benefits?
  5. What is the waiting period for benefits?
  6. What is your policy on working from home?
  7. Wow much vacation/sick time would I get?
  8. What is the average length of employment?
  9. What is a typical day/week like?
  10. What is an atypical day/week like?
  11. What would my workstation be like?
  12. How many people are on the team?

Technical Questions:

  1. What version of ruby/rails are you currently running?
  2. Do you plan to upgrade?
  3. How closely do you follow ruby/rails conventions? (MVC, db schema, etc.)
  4. Do you work as a team?
  5. Does code get reviewed before it can be committed? By Who?
  6. How much test coverage does the project have?
  7. What is your deployment strategy? (shell script, capistrano, CI, etc.)
  8. Do you use git? svn? other?
  9. Do you have a development strategy? What is it?
  10. Do you have a regular release cycle?
  11. What platform would I be developing on? (mac, linux, windows)
  12. Would I be able to bring my own computer?
  13. How to you project the application will progress in 6 months? 1 year, 5 years?

You can obviously swap ruby/rails for whatever you’re working with. The point is that you know what you’re getting yourself into. Remember, just as you are putting your best foot forward, so are they. Not that they are intentionally deceiving you, but they probably won’t naturally offer this information. So it’s up to you to find out.

1

Tabbed Interface – A new rails plugin

It’s not like we really need a new rails plugin, but this one I really saw the need for. Tabbed interfaces can really improve how your website looks and flows. Here’s a great article from smashingmagazine about how, when and why to use tabbed interfaces. Here’s a quick rundown of the plugin. Let me know what you think!

TabbedInterface

Build a tabbed interface very easily. Requires Prototype.

see a working example at http://tab-interface.heroku.com

Install

./script/plugin install git://github.com/jondruse/tabbed_interface.git

Move the ajax-loader.gif from the resources folder to your public images folder, or go make one at http://www.ajaxload.info/. Whatever you do, just make sure you call it ajax-loader.gif.

An example css file is also included in the resources folder.

Example

In this simple example we setup a TabbedInterface with two tabs.

<% tabbed_content do |box| %>

  # You don't have to include these.  They are the defaults, but this is how you would change them.
  <% box.content_wrapper = "tabbed_content" %>
  <% box.navigation_wrapper = "tabbed_navigation" %>
  <% box.tab_tag = :li %>
  <% box.tabs_tag = :ul %>

  # Let's add some tabs!
  # All you need is a title and a url. Also accepts two options hashes that get passed directly to the link_to_remote
  call.
  <% box.tab("Test One", test_one_path, {:method => :post}, {:class => "different-class"}) %>
  <% box.tab("Test Two", test_two_path) %>
 
  <% box.content = capture do %>
    # Put your default content here (probably the content for the the first tab, but doesn't have to be)
    Default text
  <% end %>

<% end %>

The tabbed_content helper yields an object that has two main methods.

#tab

This method will setup a new tab header. Just pass the title and the url to call and update the main content area.

#content

The only requirement is that you use capture (as shown above). This sets the default content for the interface. Then when you click on a different tab, the content will be updated. You can put anything in here, but it will probably be a partial, being that the links use link_to_remote to update the main content area. There are no limitations on how any tabs you can have, or how many interfaces you can have on a page.

Enjoy!

0

Pesky form error markup in Rails

Have you ever spent hours coding up the most awesome form you’ve ever seen only to have Rails break it with it’s error markup? I have, and it really sucks. So today I set out on a mission to solve this ever annoying problem. Here’s how I did it.

Step 1. Find the source.

If you’ve ever gone looking through Rails source, you know this wasn’t a very easy thing to do. But after some digging I found what was causing me so much grief.

# active_record_helper.rb in action_pack/action_view/helpers
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>" }

Step 2. Figure out what you want.

I found that the problem was that it was using a div to wrap the incorrect fields. This didn’t work for me. I needed a span so it wouldn’t break the layout. So what do you need? Maybe a div works, but you want to use a different class name? Whatever it is, figure it out. Firebug may be of some help to you.

Step 3. Patch Rails.

I love how easy this step is. Put a file in RAILS_ROOT/config/initializers called rails_ext.rb. You can really name this file whatever you want, but that’s what mine is called.

Next add these lines.

module ActionView
  class Base
    @@field_error_proc = Proc.new{ |html_tag, instance| "<span class=\"fieldWithErrors\">#{html_tag}</span>" }
    cattr_accessor :field_error_proc
  end
end

Now restart your server and enjoy.

Closing Thoughts.

Now I know what you’re thinking. Why don’t I just edit the css? Well here it is. My markup is a big deal. If I want a span I should have a span. I guess it really comes down to preference. Take it or leave it.

0

Leveraging the power of inheritance in Rails.

Here’s a neat trick.  It’s pretty simple but really helpful.

Problem: You’re always setting up the same stuff in every controller. Layouts, helper methods and so on.

Solution: Set up your own controller to inherit from.

Throw this into a new file in the controllers folder called base_controller.rb.

class BaseController < ApplicationController
 
  layout "base"
  helper :my_cool_helper  

end

Now take any controller and change the first line like below. Now anything in BaseController you get for free. So in the example below, PostsController’s layout will be “base” and will have the MyCoolHelper included also.

class PostsController < BaseController

  def list
  end

end

Wow, that was easy. Now it’s much easier to make (and change) default behaviors in your app. Hope this helps you, it sure helped me.

3

Git Documentation for Apple Dictionary

A couple months ago I found Priit Haamer’s blog post about the Ruby Dictionary for Mac OS X. So now I’ve ported the Git Documentation to Dictionary.app also. Please let me know of any bugs.  Enjoy!

Download

  1. Download gitdictionary file (<1MB, works only with 10.5 Leopard) and proceed to installation instructions.
Installation

  1. Unzip downloaded file into ~/Library/Dictionaries folder if you want to keep this dictionary only for yourself. You may need to create this folder if you haven’t installed any dictionaries before.
  2. To make the searching with Spotlight work, open Dictionary.app preferences pane and drag “Git” from the dictionaries list from bottom to the top because only the first one in this list will be searched from Spotlight.
  3. To make it available for all users on your computer, drop the Git.dictionary folder into /Library/Dictionaries folder.