January 2012
1 post
“When the poet’s sentiments are overly visible, the audience may become...”
– Kyoshi Takahama
Jan 20th
December 2011
3 posts
A shell script to bootstrap Textmate
I put together a little bash script that downloads all the bundles and themes I like to use. That way I don’t have to start from scratch the next time I’m installing Textmate on my machine.
Dec 20th
Improved Javascript Logging Function
This a a javascript function for console logging that accounts for browsers that don’t have window.console. To understand where arguments comes from, see my previous post. Props to Will Fitzgerald for opening my eyes.
Dec 2nd
“arguments is the name of a local, array-like object available inside every...”
– http://www.sitepoint.com/arguments-a-javascript-oddity/
Dec 2nd
June 2011
1 post
Ruby syntactic sugar: Object.in?
Jun 4th
November 2010
1 post
Close a jQuery autocomplete on ENTER keypress.
jQuery UI’s autocomplete opens up with suggestions, but maybe you don’t want them. By default you can hide the suggestions by tabbing away or hitting ESC, but the ENTER key does nothing. This snippet remedies that..
Nov 8th
July 2010
2 posts
Rake Task: Configatron to Javascript!
Configatron is a “super cool, simple, and feature rich configuration system for Ruby apps”. It’s a great tool for keeping your apps DRY and your configuration free-spirited. But today it gets even better! This rake task exports your configatron config to a JSON hash and sticks in a native JS object, making it easy to give your static javascript files access to your app’s...
Jul 22nd
4 tags
Rails View Helper for Placehold.it
I just found placehold.it, a quick and simple image placeholder service. The requirements are a little touchy and it’s easy to construct a bad URL, so I wrote a little view helper for Rails that makes it a bit easier to use.
Jul 13th
June 2010
1 post
Dynamic background-image Strings with Sass 3 and...
I was having trouble defining a dynamic background image path in a Rails app using Sass 3 and Compass, because the quotes were getting encoded somewhere along the line during the conversion from Sass to CSS (presumably by Compass, as the sass-convert binary itself was not encoding the quotes). To solve the problem I started used Compass’s nifty image_url function..
Jun 17th
May 2010
1 post
Human-friendly model attribute labels with i18n...
I’ve been using the awesome Formtastic FormBuilder DSL in all my Rails projects for a while now. It’s semantic nature makes it a great tool for generating HTML forms that are consistent and easy to style. Today I discovered a new feature: you can define custom labels/names for your model attributes by adding a snippet like the one below to your project’s en.yml (or other...
May 5th
April 2010
1 post
Rails on a Plane!
Sometimes I’m coding on the move. During these lapses in internet connectivity I’ve typically had to comment out application features that require an internet connection, like a map or a Google Ajax Libraries API call. Today I whipped up a little solution to that issue. Drop the snippet below into application_helper.rb. To pull up a web interface to the rdocs of all your installed...
Apr 6th
March 2010
3 posts
5 tags
Rails HTTP Basic Auth with Cookies on Heroku
Heroku uses config vars to keep sensitive data out of your code. Here’s a little setup I just made to get simple HTTP authentication into an app, with persistence across page views using the session. If you’ve set a heroku config var for the admin password, it will be used. Otherwise the default password is ‘secret’.
Mar 22nd
1 note
3 tags
Quickly Clone and View Github Repos
Mar 16th
1 note
3 tags
Preventing Jumpy/Buggy jQuery Animations
Ever noticed how sometimes you’ll use a jQuery animation effect like slideToggle() and the DOM element kinda chops its way through the animation? It turns out that elements you’re animating need to have their widths and heights hard-coded in order to animate predictably. Here’s my solution:
Mar 11th
1 note
December 2009
1 post
Encouraging Users to Drop IE
After a few days struggling to make a recent project look right on Internet Explorer, I set about trying to find a graceful way of notifying users that they need to upgrade. I played with a shit-ton of pre-existing solutions, but in the end I decided to whip up something much more basic that I can easily integrate into any Rails app:
Dec 2nd
November 2009
1 post
“If you’re not familiar with what makes a valid Rack application it basically...”
– Pancake Documentation
Nov 30th
October 2009
8 posts
Mac Users: Extract Frames from Animated GIFs with...
Animated GIFs are kind of a relic these days, but when you need to crack one open and get at its individual frames, you might think Photoshop is that way to go. Alas, Photoshop doesn’t support animated GIFs (any more). But! It turns out Preview.app does. You just open the GIF and drag the frames right out of it. Handy.
Oct 30th
Testing HTTP Basic Auth in Sinatra
Snagged from http://www.sinatrarb.com/faq.html#test_http_auth
Oct 30th
HTML Form Submission with button/image Combo
When you submit an HTML form using <input type="image">, the x and y coordinates of the image get passed along as GET parameters in the URL. I can never seem to remember how to avoid this, so here’s a snippet for reference:
Oct 29th
script/console for Sinatra
Wishing you had a Rails-like console for your Sinatra app? Welp, zapnap has implemented a nice solution into his vanilla sinatra-template on github, based on code from barking iguana. Happy consolation! Update: Check out racksh, a console for Rack-based ruby web applications, including Rails, Merb, Sinatra, Camping, Ramaze, or even your own framework.
Oct 26th
Lessons learned on my first date with DataMapper
Today I started playing around with DataMapper, an up-and-coming ORM that’s popular among the Merb/Sinatra kids, and one that’s making its way into Rails 3 as an alternative to ActiveRecord. Here are some notes from day one: Different Database Column Defaults: The default String size is 50 characters, unlike ActiveRecord which defaults to 255 (for MySQL, anyway) Strategic Eager...
Oct 22nd
Sinatra Extension: Ratpack
As soon as I started using Sinatra, I felt naked without those handy ActionView helpers from Rails like stylesheet_link_tag, javascript_include_tag, link_to, etc. So today I read up a little on how to write Sinatra Extensions and put together the beginnings of a gem to address the need. Here are the methods implemented so far.. content_tag convert_to_list_items image_tag ...
Oct 22nd
“Criticism may not be agreeable, but it is necessary. It fulfills the same...”
– Winston Churchill
Oct 12th
Access the 'clicked' item within a jQuery...
I always seem to forget this, but the proper syntax is $(this):
Oct 11th
September 2009
2 posts
New Rails Plugin: Autoform
I’m currently working on a Rails project that has lots of models that all need basic CRUDability, but the forms and views don’t need to be fancy, as the app is used primarily as an XML webservice. We need the CRUD so we can tweak content here and there, but it’s not public-facing. I got tired of having to update the forms every time I wrote a migration, so I created Autoform. ...
Sep 12th
ListenListen
Sep 5th
August 2009
3 posts
5 tags
A Coupla Handy Tools for working with XML
Camino If you’re on a Mac, the Camino web browser is a good buddy. It’s great for those times when you want a fast, lean browser without the frills. One of its many perks is that it displays XML nicely (unlike Firefox and Safari), with syntax highlighting and little toggle buttons for expanding and collapsing elements. hurl One of the coolest things to come out of this year’s...
Aug 30th
5 tags
Adding non-columnar data to Rails XML and JSON...
ActiveRecord offers handy to_xml and to_json methods for converting AR objects to XML or JSON format. Today I discovered that you can easily add values from model methods to the output, like so.. See Module::ActiveRecord::Serialization for more info.
Aug 29th
6 tags
How to Suppress Adobe Debugger's Exception Alerts
If you’re a Flash developer and have the Debugger Version of Adobe Flash Player installed, you’ve probably noticed that it periodically pops up annoying alerts when you browse the web. They are the result of poor exception handling by the Actionscript/Flex programmers of the world. To disable the alerts, open up your mm.cfg and add this line: SuppressDebuggerExceptionDialogs=1 ...
Aug 24th
June 2009
2 posts
4 tags
A new twist on 'link_to_unless_current'
I’ve been thinking it would be nice to have a smarter alternative to Rails’ link_to_unless_current view helper: rather than being relegated to plain old text, the active link should remain a link and get a CSS class of active tacked onto it, so it can be styled to stand out from its siblings. It’s more intuitive for a link to always be a link, even if it’s a link to the...
Jun 24th
Request methods in Rails
I seem to have a hard time remembering all the request methods in Rails. For anyone else who has the same problem, you might wanna bookmark the Rails API page on Class ActionController::AbstractRequest And here’s a handy helper method for getting your application’s base URL:
Jun 2nd
May 2009
3 posts
Specifying CSS Class for a Textile list
A nice little trick I learned today. Works for ordered lists too..
May 27th
Manipulating Checkboxes with jQuery
When selecting elements with jQuery, you can’t simply append .checked to a checkbox element to determine whether or not its checked; you have to use the attr array or the is expression evaluator, like so.. Find more info on this at Electric Toolbox
May 27th
jQuery fadeToggle() Function
This little function works just like jQuery’s built-in slideToggle(). It’s a nice alternative to the toggle(‘normal’) function, which expands and collapses elements by squishing/stretching them horizontally and vertically.
May 26th
April 2009
8 posts
Simplify Your Dev Environment With Passenger Pane →
For the UI-oriented Mac/Rails crowd..
Apr 29th
A Nice Little View Helper for Generating List...
Give this helper an array, and get back a string of <li> elements. The first item gets a class of first and the last, well.. last. This makes it easier to apply CSS styles to lists, be they ordered or unordered.
Apr 20th
ListenListen
Apr 18th
Creating Polymorphic Columns in a Rails (2+)...
This is a small convenience, but I just discovered it so I thought I’d share. The following migration will add an attachment_id column and a string attachment_type column with a default value of ‘Photo’. You can also use belongs_to instead of references if that seems more intuitive; It will have the same effect. For more info see the Rails Guide on Migrations or this slightly more in-depth...
Apr 16th
Submitting a Rails Ajax form with Javascript or a...
When using remote_form_for or its relatives, it won’t work to use javascript can’t call .submit() on the form; that will treat is a regular form submit. The following is an example of what not to do. (By the way, I use HAML and jQuery in my code samples.) And here’s the workaround I’m using..
Apr 15th
Recent Gems and Plugins I've Authored
Monkey Patches is a ruby gem that extends String, Arrary, and Object to provide handy methods like those found in ActiveSupport::CoreExtensions. Some goodies include truncate_preserving_words, ellipsize, permalinkify, replace_wonky_characters_with_ascii, remove_http_and_www, etc. HAMLize Views is a ruby gem that uses the html2haml command line utility to convert rails’ ERB view files to HAML. ...
Apr 10th
ActiveRecord Associations: Count vs. Size vs....
Short answer: use size. It will give you a count of the objects without actually loading them, and it’s smart enough to avoid hitting the database if the association has already been loaded. Furthermore, it picks up on counter caches when you follow naming conventions. More info at Robot Has No Heart.
Apr 8th
4 tags
How to Validate Rails' Ajax Forms with Javascript
When using Rails’ Ajax helpers like remote_form_for and form_remote_tag, I like to do Javascript validation on the user’s input before submitting the form. This makes it easier to avert issues before they happen, and takes stress off the server. Getting this to work is a little hacky, though. The trick is to put the call to the validating javascript function in the :before callback,...
Apr 7th
March 2008
1 post
“You are not here merely to make a living. You are here in order to allow the...”
– Woodrow Wilson
Mar 5th